ViewPager和Testview结合使用,点击切换和滑动切换

老规矩,第一先上图,这样是为了让大家能知道是不是想要的效果。

开始上代码了:

首先是Activity的xml:

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


    <RelativeLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="@dimen/dp_16"
        android:background="@drawable/shape_director_total_earning_bg"
        android:gravity="center">

        <TextView
            android:id="@+id/tv_total_money_desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="累计收益"
            android:textColor="@color/white"
            android:textSize="@dimen/dimen_12sp" />

        <TextView
            android:id="@+id/tv_total_money"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_total_money_desc"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/dp_5"
            android:text="0元"
            android:textColor="@color/white"
            android:textSize="@dimen/dimen_24sp" />

    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <View style="@style/Line.Horizontal" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@color/white"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/linear1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                android:paddingBottom="@dimen/dimen_5dp"
                android:paddingTop="@dimen/dimen_5dp">

                <TextView
                    android:id="@+id/picture_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="@dimen/dimen_3dp"
                    android:layout_weight="1"
                    android:text="3元"
                    android:textColor="@color/tc_gray_3"
                    android:textSize="@dimen/dimen_16sp" />

                <TextView
                    android:id="@+id/picture_text1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:text="赏金在路上"
                    android:textColor="@color/tc_gray_6"
                    android:textSize="@dimen/dimen_14sp" />

            </LinearLayout>

            <View
                android:layout_width="0.5dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:background="@color/tc_gray_9" />

            <LinearLayout
                android:id="@+id/linear2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:orientation="vertical"
                android:paddingBottom="@dimen/dimen_5dp"
                android:paddingTop="@dimen/dimen_5dp"

                >

                <TextView
                    android:id="@+id/movie_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="@dimen/dimen_3dp"
                    android:layout_weight="1"
                    android:text="0元"
                    android:textColor="@color/tc_gray_3"
                    android:textSize="@dimen/dimen_16sp" />

                <TextView
                    android:id="@+id/movie_text1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:text="已获得赏金"
                    android:textColor="@color/tc_gray_6"
                    android:textSize="@dimen/dimen_14sp" />
            </LinearLayout>

            <View
                android:layout_width="0.5dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:background="@color/tc_gray_9" />

            <LinearLayout
                android:id="@+id/linear3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:orientation="vertical"
                android:paddingBottom="@dimen/dimen_5dp"
                android:paddingTop="@dimen/dimen_5dp">

                <TextView
                    android:id="@+id/music_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="@dimen/dimen_3dp"
                    android:layout_weight="1"
                    android:text="3个"
                    android:textColor="@color/tc_gray_3"
                    android:textSize="@dimen/dimen_16sp" />

                <TextView
                    android:id="@+id/music_text1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:text="成功邀请"
                    android:textColor="@color/tc_gray_6"
                    android:textSize="@dimen/dimen_14sp" />

            </LinearLayout>


        </LinearLayout>

        <View style="@style/Line.Horizontal" />


        <android.support.v4.view.ViewPager
            android:id="@+id/vPager"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_weight="1.0"
            android:background="@color/white"
            android:flipInterval="30"
            android:persistentDrawingCache="animation" />

    </LinearLayout>

    <View style="@style/Line.Horizontal" />
</LinearLayout>

然后是Activity:

扫描二维码关注公众号,回复: 4564305 查看本文章
package com.dyh.drivingschool.ui.director;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dyh.drivingschool.R;
import com.dyh.drivingschool.ui.director.adapter.MFragmentPagerAdapter;
import com.dyh.drivingschool.ui.director.fragment.ProfitRewardFragment;
import com.dyh.library.base.TitleFragmentActivity;

import java.util.ArrayList;

/**
 * Created by ljg on 2018/7/26.
 * 累计收益
 */

public class ProfitRewardActivity extends TitleFragmentActivity {

    //图片
    private TextView mTv_reward_road_money;
    //电影
    private TextView mTv_get_reward_money;
    //音乐
    private TextView mTv_number;
    //图片
    private TextView mTv_reward_road;
    //电影
    private TextView mTv_get_reward;
    //音乐
    private TextView mTv_success_invitation;

    private LinearLayout mLinear1;
    private LinearLayout mLinear2;
    private LinearLayout mLinear3;

    //实现Tab滑动效果
    private ViewPager mViewPager;

    //当前页卡编号
    private int currIndex = 0;

    //存放Fragment
    private ArrayList<Fragment> fragmentArrayList;

    //管理Fragment
    private FragmentManager fragmentManager;

    public Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addLayout(R.layout.activity_profit_reward);
        context = this;

        initView();


    }

    private void initView() {
        //初始化TextView
        InitTextView();

        //初始化Fragment
        InitFragment();

        //初始化ViewPager
        InitViewPager();

    }
//
//    @Override
//    public View onCreateView(String name, Context context, AttributeSet attrs) {
//
//        return super.onCreateView(name, context, attrs);
//    }
//
//    @Override
//    protected void onResume() {
//        /**
//         * 设置为竖屏
//         */
//        if(getRequestedOrientation()!= ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//        }
//        super.onResume();
//    }

    /**
     * 初始化头标
     */
    private void InitTextView(){

        //金额
        mTv_reward_road_money = (TextView)findViewById(R.id.tv_reward_road_money);
        //赏金在路上
        mTv_reward_road = (TextView)findViewById(R.id.tv_reward_road);

        //金额
        mTv_get_reward_money = (TextView) findViewById(R.id.tv_get_reward_money);
        //已获得赏金
        mTv_get_reward = (TextView) findViewById(R.id.tv_get_reward);

        //几个
        mTv_number = (TextView)findViewById(R.id.tv_number);
        //成功邀请
        mTv_success_invitation = (TextView)findViewById(R.id.tv_success_invitation);


        mLinear1 = (LinearLayout)findViewById(R.id.linear1);
        mLinear2 = (LinearLayout)findViewById(R.id.linear2);
        mLinear3 = (LinearLayout)findViewById(R.id.linear3);

        //添加点击事件
        mLinear1.setOnClickListener(new MyOnClickListener(0));
        mLinear2.setOnClickListener(new MyOnClickListener(1));
        mLinear3.setOnClickListener(new MyOnClickListener(2));
    }

    /**
     * 初始化页卡内容区
     */
    private void InitViewPager() {

        mViewPager = (ViewPager) findViewById(R.id.vPager);
        mViewPager.setAdapter(new MFragmentPagerAdapter(fragmentManager, fragmentArrayList));

        //让ViewPager缓存2个页面
        mViewPager.setOffscreenPageLimit(2);

        //设置默认打开第一页
        mViewPager.setCurrentItem(0);

        //将顶部文字恢复默认值
        resetTextViewTextColor();
        mLinear1.setBackgroundResource(R.color.app_title_brown);
        mTv_reward_road_money.setTextColor(getResources().getColor(R.color.white));
        mTv_reward_road.setTextColor(getResources().getColor(R.color.white));

        //设置viewpager页面滑动监听事件
        mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }

    /**
     * 初始化Fragment,并添加到ArrayList中
     */
    private void InitFragment(){
        fragmentArrayList = new ArrayList<Fragment>();
        fragmentArrayList.add(new ProfitRewardFragment());
        fragmentArrayList.add(new ProfitRewardFragment());
        fragmentArrayList.add(new ProfitRewardFragment());
        fragmentManager = getSupportFragmentManager();

    }

    /**
     * 头标点击监听
     * @author weizhi
     * @version 1.0
     */
    public class MyOnClickListener implements View.OnClickListener{
        private int index = 0 ;
        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mViewPager.setCurrentItem(index);
        }
    }

    /**
     * 页卡切换监听
     * @author weizhi
     * @version 1.0
     */
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageSelected(int position) {
//
            switch (position){

                //当前为页卡1
                case 0:
                    //从页卡1跳转转到页卡2
                    if(currIndex == 1){
                        resetTextViewTextColor();
                        mTv_reward_road_money.setTextColor(getResources().getColor(R.color.white));
                        mTv_reward_road.setTextColor(getResources().getColor(R.color.white));
                        mLinear1.setBackgroundResource(R.color.app_title_brown);
                        mLinear2.setBackgroundResource(R.color.white);
                        mLinear3.setBackgroundResource(R.color.white);
                    }else if(currIndex == 2){//从页卡1跳转转到页卡3
                        resetTextViewTextColor();
                        mTv_reward_road_money.setTextColor(getResources().getColor(R.color.white));
                        mTv_reward_road.setTextColor(getResources().getColor(R.color.white));
                        mLinear1.setBackgroundResource(R.color.app_title_brown);
                        mLinear2.setBackgroundResource(R.color.white);
                        mLinear3.setBackgroundResource(R.color.white);
                    }
                    break;

                //当前为页卡2
                case 1:
                    //从页卡1跳转转到页卡2
                    if (currIndex == 0) {
                        resetTextViewTextColor();
                        mTv_get_reward_money.setTextColor(getResources().getColor(R.color.white));
                        mTv_get_reward.setTextColor(getResources().getColor(R.color.white));
                        mLinear2.setBackgroundResource(R.color.app_title_brown);
                        mLinear1.setBackgroundResource(R.color.white);
                        mLinear3.setBackgroundResource(R.color.white);
                    } else if (currIndex == 2) { //从页卡1跳转转到页卡2
                        resetTextViewTextColor();
                        mTv_get_reward_money.setTextColor(getResources().getColor(R.color.white));
                        mTv_get_reward.setTextColor(getResources().getColor(R.color.white));
                        mLinear2.setBackgroundResource(R.color.app_title_brown);
                        mLinear1.setBackgroundResource(R.color.white);
                        mLinear3.setBackgroundResource(R.color.white);
                    }
                    break;

                //当前为页卡3
                case 2:

                    //从页卡1跳转转到页卡2
                    if (currIndex == 0) {
                        resetTextViewTextColor();
                        mTv_success_invitation.setTextColor(getResources().getColor(R.color.white));
                        mTv_number.setTextColor(getResources().getColor(R.color.white));
                        mLinear3.setBackgroundResource(R.color.app_title_brown);
                        mLinear2.setBackgroundResource(R.color.white);
                        mLinear1.setBackgroundResource(R.color.white);
                    } else if (currIndex == 1) {//从页卡1跳转转到页卡2
                        resetTextViewTextColor();
                        mTv_success_invitation.setTextColor(getResources().getColor(R.color.white));
                        mTv_number.setTextColor(getResources().getColor(R.color .white));
                        mLinear3.setBackgroundResource(R.color.app_title_brown);
                        mLinear2.setBackgroundResource(R.color.white);
                        mLinear1.setBackgroundResource(R.color.white);
                    }
                    break;
            }
            currIndex = position;
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };


    /**
     * 将顶部文字恢复默认值
     */
    private void resetTextViewTextColor(){

        mTv_reward_road_money.setTextColor(getResources().getColor(R.color.tc_gray_3));
        mTv_reward_road.setTextColor(getResources().getColor(R.color.tc_gray_3));
        mTv_get_reward.setTextColor(getResources().getColor(R.color.tc_gray_3));
        mTv_get_reward_money.setTextColor(getResources().getColor(R.color.tc_gray_3));
        mTv_number.setTextColor(getResources().getColor(R.color.tc_gray_3));
        mTv_success_invitation.setTextColor(getResources().getColor(R.color.tc_gray_3));
    }

    @Override
    protected void initTitileBar() {
        titleLayout.showCenterLeftIconView("累计收益");
    }
}

最后是 FragmentPagerAdapter:

package com.dyh.drivingschool.ui.director.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import java.util.ArrayList;

/**
 * Fragment适配器
 * @author weizhi
 * @version 1.0
 * Created by ljg on 2018/8/3.
 */
public class MFragmentPagerAdapter extends FragmentPagerAdapter {

    //存放Fragment的数组
    private ArrayList<Fragment> fragmentsList;

    public MFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragmentsList) {
        super(fm);
        this.fragmentsList = fragmentsList;
    }

    @Override
    public Fragment getItem(int position) {

        return fragmentsList.get(position);
    }

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

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    }
}

最后是几个颜色值:

<color name="tc_gray_3">#333333</color>
<color name="white">#FFFFFF</color>
<color name="dividerColor">#cfcfcf</color>
<dimen name="dividerWidth">0.5dp</dimen> 

横线:

<style name="Line.Horizontal" parent="@style/EmptyWidget">
  <item name="android:layout_width">match_parent</item>
  <item name="android:layout_height">@dimen/dividerWidth</item>
  <item name="android:background">@color/dividerColor</item>
</style>
shape文件:一个圆
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">

    <solid android:color="#C6A36B"/>

</shape>

以上就是全部代码

猜你喜欢

转载自blog.csdn.net/zhangkaiyazky/article/details/81390829