情诗网 >高端情话 > 正文

TabLayout高端用法(三)

来源:情诗网    2021-01-13    分类:高端情话

Paste_Image.png

拿标准的Tablayout就套不进去了,首先它没有tabIndicator,关于如何去除tabIndicator在第一篇中已经讲过,这里不再赘述。其次这两个Tab一个是有icon,一个是没有icon的。当然这个需求本身还是很简单的,假如不用Tablayout也无非就是写个布局,切换viewpager的时候对应的状态改变。但这里我就非要用Tablayout,通过这个例子来说明我们要讲的问题。

对于上面的需求,我们可以自定义TabItem来实现,这个算是比较简单的需求,有时候可能会更复杂,我们都可以通过自定义来达到想要的效果。这是我们需要的layout,一个线性布局,左边是文字,右边是icon。

<?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:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/txt_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="14sp" />

    <ImageView
        android:id="@+id/img_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp" />

</LinearLayout>

TabLayout有一个方法叫setCustomView(),通过调用这个方法可以把我们自定义的布局塞进去。那具体的调用就是这样:

mTabLayout.getTabAt(0).setCustomView(getTabView(0));
mTabLayout.getTabAt(1).setCustomView(getTabView(1));

这个getTabView(int i)方法是我们自己写的,用于初始化自定义Item的一些数据,如下:

 public View getTabView(final int position) {
        final View view = LayoutInflater.from(this).inflate(R.layout.item_tab, null);
        TextView txtTitle = (TextView) view.findViewById(R.id.txt_title);
        final ImageView imgTitle = (ImageView) view.findViewById(R.id.img_title);
        imgTitle.setImageResource(tabIcons[position]);
        txtTitle.setText(titles.get(position));
        if (position == 0) {
            txtTitle.setTextColor(Color.parseColor("#057523"));
        } else {
            imgTitle.setVisibility(View.INVISIBLE);
            txtTitle.setTextColor(Color.parseColor("#ced0d3"));
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startPropertyAnim(imgTitle);
                mViewPager.setCurrentItem(position);
            }
        });
        return view;
    }

没准切换的时候,产品经理让icon还要带点动画效果,所以这里我们随便来了个属性动画,在点击和切换的时候触发:

  private void startPropertyAnim(ImageView v) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(v, "rotation", 180f, 360f);
        anim.setDuration(500);
        anim.start();
    }

这个切换或者点击时候的事件就要我们自己实现了,所以我们要实现addOnTabSelectedListener这个接口,并在onTabSelectedonTabUnselected里做出相应的改变:

  mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                changeTabStatus(tab, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                changeTabStatus(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

选中和未选中的时候我们分别调用下changeTabStatus,这个是我们自己写的状态改变的方法:

 private void changeTabStatus(TabLayout.Tab tab, boolean selected) {
        View view = tab.getCustomView();
        final ImageView imgTitle = (ImageView) view.findViewById(R.id.img_title);
        TextView txtTitle = (TextView) view.findViewById(R.id.txt_title);
        imgTitle.setVisibility(View.VISIBLE);
        if (selected) {
            txtTitle.setTextColor(Color.parseColor("#0EA73C"));
            startPropertyAnim(imgTitle);
        } else {
            txtTitle.setTextColor(Color.parseColor("#7f7f7f"));
            imgTitle.setVisibility(View.INVISIBLE);
        }
    }

最后给出完整代码:

public class MainActivity extends AppCompatActivity {


    ViewPager mViewPager;

    ListFragment mFragment1;

    ListFragment mFragment2;

    PagerAdapter mPagerAdapter;

    private TabLayout mTabLayout;

    private ArrayList<String> titles = new ArrayList<>();

    private boolean isSelected = false;

    private int[] tabIcons = {
            R.mipmap.icon,
            R.mipmap.icon,
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView(savedInstanceState);
    }

    private void initView(Bundle savedInstanceState) {

        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mViewPager.setOffscreenPageLimit(2);
        mTabLayout = (TabLayout) findViewById(R.id.toolbar_tab);

        if (savedInstanceState == null) {
            mFragment1 = new ListFragment();
            mFragment1.initData('a', 'z');
            mFragment2 = new ListFragment();
            mFragment2.initData('A', 'Z');
        }
        titles.add("我加入的");
        titles.add("推荐");

        mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.getTabAt(0).setCustomView(getTabView(0));
        mTabLayout.getTabAt(1).setCustomView(getTabView(1));
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                changeTabStatus(tab, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                changeTabStatus(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    private void changeTabStatus(TabLayout.Tab tab, boolean selected) {
        View view = tab.getCustomView();
        final ImageView imgTitle = (ImageView) view.findViewById(R.id.img_title);
        TextView txtTitle = (TextView) view.findViewById(R.id.txt_title);
        imgTitle.setVisibility(View.VISIBLE);
        if (selected) {
            txtTitle.setTextColor(Color.parseColor("#0EA73C"));
            startPropertyAnim(imgTitle);
        } else {
            txtTitle.setTextColor(Color.parseColor("#7f7f7f"));
            imgTitle.setVisibility(View.INVISIBLE);
        }
    }

    public View getTabView(final int position) {
        final View view = LayoutInflater.from(this).inflate(R.layout.item_tab, null);
        TextView txtTitle = (TextView) view.findViewById(R.id.txt_title);
        final ImageView imgTitle = (ImageView) view.findViewById(R.id.img_title);
        imgTitle.setImageResource(tabIcons[position]);
        txtTitle.setText(titles.get(position));
        if (position == 0) {
            txtTitle.setTextColor(Color.parseColor("#057523"));
        } else {
            imgTitle.setVisibility(View.INVISIBLE);
            txtTitle.setTextColor(Color.parseColor("#ced0d3"));
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startPropertyAnim(imgTitle);
                mViewPager.setCurrentItem(position);
            }
        });
        return view;
    }

    private void startPropertyAnim(ImageView v) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(v, "rotation", 180f, 360f);
        anim.setDuration(500);
        anim.start();
    }


    public class PagerAdapter extends FragmentPagerAdapter {
        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return mFragment1;
            } else if (position == 1) {
                return mFragment2;
            }

            return null;
        }
        @Override
        public int getCount() {
            return 2;
        }

    }
}

再来看下运行效果

c.gif

是不是很简单!

有同学说Popupwindow的弹出事件与ViewPager的切换事件冲突了,其实这个稍加调整就可以了,我们定义个变量selectedPosition 用来标识当前被选中的位置,然后比较下当前点击的这个位置如果已经是被选中状态,则弹出Popupwindow,否则就切换ViewPager。

 private int selectedPosition = 0;

 public View getTabView(final int position) {
        final View view = LayoutInflater.from(this).inflate(R.layout.item_tab, null);
        TextView txtTitle = (TextView) view.findViewById(R.id.txt_title);
        final ImageView imgTitle = (ImageView) view.findViewById(R.id.img_title);
        imgTitle.setImageResource(tabIcons[position]);
        txtTitle.setText(titles.get(position));

        if (position == 0) {
            txtTitle.setTextColor(Color.parseColor("#057523"));
        } else {
            imgTitle.setVisibility(View.INVISIBLE);
            txtTitle.setTextColor(Color.parseColor("#ced0d3"));
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (selectedPosition == position) {
                    showPopup();
                } else {
                    mViewPager.setCurrentItem(position);
                }
            }
        });
        return view;
    }
3.gif

完整代码:GitHub

热门文章