Android中ViewPager例子

示例效果如下:



 

 代码如下:

1、

/**
 * 我的特权
 * Created by dwen on 2018/3/31.
 */

public class MyPrivilegeActivity extends BaseActivity {

    private ViewPager viewPager;
    private List<Fragment> list;
    private LinearLayout tab1,tab2;
    private ImageView line1,line2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_privilege);
        initUI();
        initViewPager();
    }

    /**
     * 初始化UI
     */
    private void initUI(){
        TextView textView = (TextView) this.findViewById(R.id.top_title);
        textView.setText("我的特权");
        //Tab line
        line1 = (ImageView)findViewById(R.id.privilege_tequan_line1);
        line2 = (ImageView)findViewById(R.id.privilege_tequan_line2);
        line2.setVisibility(View.GONE);
        //Tab
        tab1 = (LinearLayout)findViewById(R.id.privilege_tequan_tab1);
        tab2 = (LinearLayout)findViewById(R.id.privilege_tequan_tab2);
        tab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0);
                line1.setVisibility(View.VISIBLE);
                line2.setVisibility(View.GONE);
                //request date
            }
        });
        tab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1);
                line1.setVisibility(View.GONE);
                line2.setVisibility(View.VISIBLE);
            }
        });
    }

    /**
     * 初始化ViewPager
     */
    private void initViewPager(){
        //ViewPager
        viewPager = (ViewPager)findViewById(R.id.my_privilege_viewPager);

        //Set List
        list = new ArrayList<>();
        MyPrivilegeFragment1 fragment1 = new MyPrivilegeFragment1();
        list.add(fragment1);
        MyPrivilegeFragment2 fragment2 = new MyPrivilegeFragment2();
        list.add(fragment2);
        //Adapter
        MyPrivilegeAdapter adapter = new MyPrivilegeAdapter(getSupportFragmentManager(),list);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
        //event listener
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                Log.i("onPageSelected::",position+"");
                if (position==0){
                    viewPager.setCurrentItem(0);
                    line1.setVisibility(View.VISIBLE);
                    line2.setVisibility(View.GONE);
                }else {
                    viewPager.setCurrentItem(1);
                    line1.setVisibility(View.GONE);
                    line2.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 分享
     */
    private void actionShare(){
        //TODO ............

    }

}

2、

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/title2"></include>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:orientation="horizontal"
        android:layout_marginTop="45dp">
        <LinearLayout
            android:id="@+id/privilege_tequan_tab1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:textSize="15dp"
                android:textColor="@color/text_color"
                android:text="推荐特权"/>
            <ImageView
                android:id="@+id/privilege_tequan_line1"
                android:layout_width="40dp"
                android:layout_height="2dp"
                android:layout_marginTop="16dp"
                android:background="@color/color_45bf23"/>

        </LinearLayout>
        <LinearLayout
            android:id="@+id/privilege_tequan_tab2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:textSize="15dp"
                android:textColor="@color/default_bt_tx_color"
                android:text="已开通特权"/>
            <ImageView
                android:id="@+id/privilege_tequan_line2"
                android:layout_width="40dp"
                android:layout_height="2dp"
                android:layout_marginTop="16dp"
                android:background="@color/color_45bf23"/>
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="99dp"
            android:background="@color/eeeeee_color"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/my_privilege_viewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


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

   


</RelativeLayout>

3、fragment_1

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/my_privilege_listV1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp">


    </ListView>
</RelativeLayout>

 4、fragment_2

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/my_privilege_listV2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp">


    </ListView>

</RelativeLayout>

5、 item_my_privilege.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/item_privilege_img"
        android:layout_width="33dp"
        android:layout_height="33dp"
        android:src="@mipmap/zuanshi"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="15dp"/>

    <TextView
        android:id="@+id/item_privilege_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/item_privilege_txt2"
        android:layout_alignBottom="@+id/item_privilege_txt2"
        android:layout_alignLeft="@+id/item_privilege_txt3"
        android:text="钻石会员"
        android:textColor="@color/text_color"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/item_privilege_txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/item_privilege_txt"
        android:textSize="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp"
        android:textColor="@color/color_ff7901"
        android:text="6980元" />

    <TextView
        android:id="@+id/item_privilege_txt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/item_privilege_img"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="35dp"
        android:textSize="12dp"
        android:textColor="@color/default_bt_tx_color"
        android:text="产品排名优先,专属于客服"/>
    
    <Button
        android:id="@+id/item_privilege_btn"
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:background="@drawable/shape_corner3"
        android:textColor="@color/white"
        android:textSize="13dp"
        android:layout_alignParentRight="true"
        android:layout_marginTop="15dp"
        android:layout_marginRight="15dp"
        android:text="立即开通"/>

    <!-- line 1-->
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/eeeeee_color"
        android:layout_marginTop="63dp"/>

</RelativeLayout>

6、Fragment1

/**
 * 我的特权- 已开通特权
 * Created by dwen on 2018/3/31.
 */

public class MyPrivilegeFragment2 extends Fragment {

    private List<MyPrivilegeModel> privilegeList = new ArrayList<>();
    private ListView listView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_privilege2,null);
        listView = (ListView) view.findViewById(R.id.my_privilege_listV2);
        this.initPrivilegeList();
        //TODO adapter......
        MyPrivilegeListAdapter adapter = new MyPrivilegeListAdapter(privilegeList,getActivity());
        listView.setAdapter(adapter);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    private void initPrivilegeList(){
        MyPrivilegeModel model = new MyPrivilegeModel();
        model.setImgTag(R.mipmap.zuanshi);
        model.setName("钻石会员");
        model.setDesc("2018-12-19到期");
        model.setApplyTxt("立即续费");
        privilegeList.add(model);
        MyPrivilegeModel model2 = new MyPrivilegeModel();
        model2.setImgTag(R.mipmap.huangjin);
        model2.setName("黄金会员");
        model2.setDesc("2018-12-19");
        model2.setApplyTxt("立即续费");
        privilegeList.add(model2);
        MyPrivilegeModel model3 = new MyPrivilegeModel();
        model3.setImgTag(R.mipmap.dangbao);
        model3.setName("担保认证");
        model3.setDesc("可随时申请退还");
        model3.setApplyTxt("申请退还");
        privilegeList.add(model3);

    }
}

7、Fragment2

/**
 * 我的特权- 已开通特权
 * Created by dwen on 2018/3/31.
 */

public class MyPrivilegeFragment2 extends Fragment {

    private List<MyPrivilegeModel> privilegeList = new ArrayList<>();
    private ListView listView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_privilege2,null);
        listView = (ListView) view.findViewById(R.id.my_privilege_listV2);
        this.initPrivilegeList();
        //TODO adapter......
        MyPrivilegeListAdapter adapter = new MyPrivilegeListAdapter(privilegeList,getActivity());
        listView.setAdapter(adapter);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    private void initPrivilegeList(){
        MyPrivilegeModel model = new MyPrivilegeModel();
        model.setImgTag(R.mipmap.zuanshi);
        model.setName("钻石会员");
        model.setDesc("2018-12-19到期");
        model.setApplyTxt("立即续费");
        privilegeList.add(model);
        MyPrivilegeModel model2 = new MyPrivilegeModel();
        model2.setImgTag(R.mipmap.huangjin);
        model2.setName("黄金会员");
        model2.setDesc("2018-12-19");
        model2.setApplyTxt("立即续费");
        privilegeList.add(model2);
        MyPrivilegeModel model3 = new MyPrivilegeModel();
        model3.setImgTag(R.mipmap.dangbao);
        model3.setName("担保认证");
        model3.setDesc("可随时申请退还");
        model3.setApplyTxt("申请退还");
        privilegeList.add(model3);

    }
}

8、PrivilegeAdapter

/**
 * 我的特权
 * Created by dwen on 2018/3/31.
 */

public class MyPrivilegeAdapter extends FragmentPagerAdapter {

    private FragmentManager fragmentManager;
    private List<Fragment> fList;

    public MyPrivilegeAdapter(FragmentManager fm, List<Fragment> fList) {
        super(fm);
        this.fList = fList;
    }


    @Override
    public Fragment getItem(int position) {
        return this.fList.get(position);
    }

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



}

9、PrivilegeListAdapter

/**
 * 我的特权-列表
 * Created by dwen on 2018/4/3.
 */

public class MyPrivilegeListAdapter extends BaseAdapter {

    List<MyPrivilegeModel> list = new ArrayList<MyPrivilegeModel>();
    Context context;

    public MyPrivilegeListAdapter(List<MyPrivilegeModel> list, Context context) {
        this.list = list;
        this.context = context;
    }

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

    @Override
    public Object getItem(int position) {
        return this.list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        MyPrivilegeModel model = this.list.get(position);
        if (null == convertView){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_my_privilege,null);
            holder = new ViewHolder(convertView);
            holder.txtName = convertView.findViewById(R.id.item_privilege_txt);
            holder.txtPrice = convertView.findViewById(R.id.item_privilege_txt2);
            holder.txtDesc = convertView.findViewById(R.id.item_privilege_txt3);
            holder.btnPrivilege = convertView.findViewById(R.id.item_privilege_btn);
            holder.imgV = convertView.findViewById(R.id.item_privilege_img);
            //Set Value
            holder.txtName.setText(model.getName());
            holder.txtPrice.setText(model.getPrice());
            holder.txtDesc.setText(model.getDesc());
            holder.btnPrivilege.setText(model.getApplyTxt());
            holder.imgV.setImageResource(model.getImgTag());
            this.goAction(holder);
            convertView.setTag(holder);
        }else {
            //直接通过holder获取子控件,不必使用findviewbyid,加快了 UI 的响应速度
            holder = (ViewHolder)convertView.getTag();
            //Set Value
            holder.txtName.setText(model.getName());
            holder.txtPrice.setText(model.getPrice());
            holder.txtDesc.setText(model.getDesc());
            holder.btnPrivilege.setText(model.getApplyTxt());
            holder.imgV.setImageResource(model.getImgTag());
            this.goAction(holder);
        }
        return convertView;
    }

    private  void  goAction(ViewHolder holder){
        //立即开通
        holder.btnPrivilege.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    static class ViewHolder{
        ImageView imgV;
        TextView txtName,txtPrice,txtDesc;
        Button btnPrivilege;

        public ViewHolder(){

        }
        public ViewHolder(View view){

        }
    }

}

 10、

/**
 * 我的特权
 * Created by dwen on 2018/4/3.
 */

public class MyPrivilegeModel {

    private int imgTag;
    private String name;
    private String price;
    private String desc;
    private String applyTxt;

    public int getImgTag() {
        return imgTag;
    }

    public void setImgTag(int imgTag) {
        this.imgTag = imgTag;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getName() {
        return name;
    }

    public String getPrice() {
        return price;
    }

    public String getDesc() {
        return desc;
    }

    public void setApplyTxt(String applyTxt) {
        this.applyTxt = applyTxt;
    }

    public String getApplyTxt() {
        return applyTxt;
    }
}

猜你喜欢

转载自wenxin2009.iteye.com/blog/2415397