27
2017
09

简单的ViewPager+Fragment+小圆点的实现

红点(http://img.blog.csdn.net/20170926151350811?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvUm9va2llemJi/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
白点(http://img.blog.csdn.net/20170926151431837?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvUm9va2llemJi/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)

首先是布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bawei.lx_lxtwo.MainActivity">

   <android.support.v4.view.ViewPager  android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_alignParentStart="true">

   </android.support.v4.view.ViewPager>

   <LinearLayout  android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="10dp" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content">
      <ImageView  android:id="@+id/img1" android:src="@drawable/point_selected" android:layout_marginRight="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" />
      <ImageView  android:id="@+id/img2" android:layout_marginLeft="5dp" android:src="@drawable/point_mormal" android:layout_width="wrap_content" android:layout_height="wrap_content" />
   </LinearLayout>

</RelativeLayout>

然后我们新建两个fragment

package com.bawei.lx_lxtwo.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bawei.lx_lxtwo.R;

/** * Created by Zhang on 2017/9/26. */

public class HelloFragment extends Fragment {
    private View view;
    private TextView mHelloTv;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.hello, container, false);
        initView(v);
        mHelloTv.setText(" 欢迎使用\n北京共享单车");
        return v;
    }

    private void initView(View v) {
        mHelloTv = (TextView) v.findViewById(R.id.hello_tv);
    }
}

fragment的布局里只有一个textview,再建一个一样的。
然后就是mainactivity

package com.bawei.lx_lxtwo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.bawei.lx_lxtwo.fragments.GoFragment;
import com.bawei.lx_lxtwo.fragments.HelloFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager mVp;
    private List<Fragment> flist = new ArrayList<>();
    private ImageView[] imgs;
    private ImageView mImg1;
    private ImageView mImg2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化数据
        initView();
        //设置数据
        getview();
        imgs = new ImageView[]{mImg1,mImg2};
    }

    private void getview() {
        flist.add(new HelloFragment());
        flist.add(new GoFragment());
        mVp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return flist.get(position);
            }

            @Override
            public int getCount() {
                return flist.size();
            }
        });

    //这里用viewpager的OnPageChangeListener方法设置小圆点的颜色
        mVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                getpoint(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void getpoint(int index) {
    //这里判断哪个是红点
        for (int i = 0; i < imgs.length; i++) {
            if(i==index){
                imgs[i].setImageResource(R.drawable.point_selected);
            }else{
                imgs[i].setImageResource(R.drawable.point_mormal);
            }
        }
    }

    private void initView() {
        mVp = (ViewPager) findViewById(R.id.vp);
        mImg1 = (ImageView) findViewById(R.id.img1);
        mImg2 = (ImageView) findViewById(R.id.img2);
    }
}

这是效果图

上一篇:Android ViewPager的无限循环与自动滚动实现 下一篇:epoll高级应用之同时处理 tcp/udp 的服务器