Viewpager实现今日头条顶部导航的功能

小鱼儿 2022-09-22 01:16 225阅读 0赞

利用简单的Textview 和Viewpager实现滑动、点击换页的效果,效果图如下:

行走的那些事   行走的那些事

先上布局文件代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical" >
  3. <LinearLayout android:background="@color/red_base" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp">
  4. </LinearLayout>
  5. <!-- 选项卡 -->
  6. <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:background="@color/white" android:orientation="horizontal" android:weightSum="5" >
  7. <FrameLayout android:id="@+id/rim_tab1_fl" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:clickable="true" android:gravity="center" >
  8. <TextView android:id="@+id/rim_tab1_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="全部" android:textColor="@color/red_base" android:textSize="16sp" />
  9. </FrameLayout>
  10. <View android:layout_width="0.5dp" android:layout_height="match_parent" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:background="@color/divider_gray_line" />
  11. <FrameLayout android:id="@+id/rim_tab2_fl" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:clickable="true" android:gravity="center" >
  12. <TextView android:id="@+id/rim_tab2_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="周边" android:textColor="@color/text_gray_4" android:textSize="16sp" />
  13. </FrameLayout>
  14. <View android:layout_width="0.5dp" android:layout_height="match_parent" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:background="@color/divider_gray_line" />
  15. <FrameLayout android:id="@+id/rim_tab3_fl" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:clickable="true" android:gravity="center" >
  16. <TextView android:id="@+id/rim_tab3_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="应援" android:textColor="@color/text_gray_4" android:textSize="16sp" />
  17. </FrameLayout>
  18. <View android:layout_width="0.5dp" android:layout_height="match_parent" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:background="@color/divider_gray_line" />
  19. <FrameLayout android:id="@+id/rim_tab4_fl" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:clickable="true" android:gravity="center" >
  20. <TextView android:id="@+id/rim_tab4_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="话题" android:textColor="@color/text_gray_4" android:textSize="16sp" />
  21. </FrameLayout>
  22. <View android:layout_width="0.5dp" android:layout_height="match_parent" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:background="@color/divider_gray_line" />
  23. <FrameLayout android:id="@+id/rim_tab5_fl" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:clickable="true" android:gravity="center" >
  24. <TextView android:id="@+id/rim_tab5_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="明星" android:textColor="@color/text_gray_4" android:textSize="16sp" />
  25. </FrameLayout>
  26. </LinearLayout>
  27. <LinearLayout android:layout_width="match_parent" android:layout_height="3dp" android:background="@color/white" >
  28. <ImageView android:id="@+id/rim_cursor" android:layout_width="80dp" android:layout_height="3dp" android:layout_marginTop="0dip" android:background="@color/title_bar_blue" />
  29. </LinearLayout>
  30. <View android:layout_width="match_parent" android:layout_height="0.1dp" android:background="@color/btn_bg_gray" />
  31. <!-- 选项卡内容显示区域 -->
  32. <View android:layout_width="match_parent" android:layout_height="10dp" android:background="@color/bg_light_gray" />
  33. <android.support.v4.view.ViewPager android:id="@+id/rim_third_vp" android:layout_width="match_parent" android:layout_height="match_parent" />
  34. </LinearLayout>

上面红色指示器的view的初始化

  1. private int screenWidth;//指示器
  2. private ImageView cursorImg;
  3. private LinearLayout.LayoutParams lp;
  4. private void initViews(){
  5. WindowManager wm = (WindowManager)
  6. getSystemService(Context.WINDOW_SERVICE);
  7. int width = wm.getDefaultDisplay().getWidth();
  8. screenWidth=width/5;
  9. cursorImg = (ImageView) findViewById(R.id.rim_cursor);
  10. lp = (LinearLayout.LayoutParams) cursorImg.getLayoutParams();
  11. lp.width = screenWidth;
  12. cursorImg.setLayoutParams(lp);
  13. leftMargin = lp.leftMargin;
  14. }

初始化indicater

  1. private void initViewPager() {
  2. viewPager = (ViewPager) findViewById(R.id.rim_third_vp);
  3. fragmentsList = new ArrayList<Fragment>();
  4. fragment1 = new RimFragment();
  5. fragmentsList.add(fragment1);
  6. fragmentsList.add(fragment2);
  7. fragmentsList.add(fragment3);
  8. fragmentsList.add(fragment4);
  9. fragmentsList.add(fragment5);
  10. viewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(),
  11. fragmentsList));
  12. viewPager.setCurrentItem(0);
  13. viewPager.setOnPageChangeListener(this);
  14. }

设置上面选项卡的点击事件

  1. @Override
  2. public void onClick(View v) {
  3. switch (v.getId()){
  4. case R.id.rim_tab1_fl:
  5. viewPager.setCurrentItem(0);
  6. num_tab1_tv.setTextColor(getResources().getColor(R.color.red_base));
  7. num_tab2_tv.setTextColor(getResources().getColor(R.color.text_gray_4));
  8. num_tab3_tv.setTextColor(getResources().getColor(R.color.text_gray_4));
  9. num_tab4_tv.setTextColor(getResources().getColor(R.color.text_gray_4));
  10. num_tab5_tv.setTextColor(getResources().getColor(R.color.text_gray_4));
  11. fragment1.setMsgName("1","周边");//周边的官方和会员的接口参数,官方
  12. break;

设置viewpager 滑动事件

  1. @Override
  2. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  3. offset = (screenWidth - cursorImg.getLayoutParams().width) / 5;
  4. hidePoint(position, positionOffsetPixels);//设置红色指示器的位置
  5. cursorImg.setLayoutParams(lp);
  6. currentIndex = position;
  7. }
  8. @Override
  9. public void onPageSelected(int position) {
  10. switch (position){
  11. //设置点击事件
  12. case 0:
  13. fragment1.setMsgName("1","周边");
  14. num_tab1_tv.setTextColor(getResources().getColor(R.color.red_base));
  15. num_tab2_tv.setTextColor(getResources().getColor(R.color.text_gray_4));
  16. num_tab3_tv.setTextColor(getResources().getColor(R.color.text_gray_4));
  17. num_tab4_tv.setTextColor(getResources().getColor(R.color.text_gray_4));
  18. num_tab5_tv.setTextColor(getResources().getColor(R.color.text_gray_4));
  19. break;
  20. }
  21. if (position==0)
  22. {
  23. }else {
  24. }
  25. }
  26. @Override
  27. public void onPageScrollStateChanged(int state) {
  28. }
  29. //设置指示器的位置
  30. private void hidePoint(int position, int positionOffsetPixels) {
  31. if (position == 0) {
  32. // 0<->1
  33. lp.leftMargin = (int) (positionOffsetPixels / 5) + offset;
  34. } else if (position == 1) {
  35. // 1<->2
  36. lp.leftMargin = (int) (positionOffsetPixels / 5) + screenWidth
  37. + offset;
  38. }else if(position==2){
  39. lp.leftMargin = (int) (positionOffsetPixels / 5) + screenWidth*2
  40. + offset;
  41. }
  42. else if(position==3){
  43. lp.leftMargin = (int) (positionOffsetPixels / 5) + screenWidth*3
  44. + offset;
  45. }
  46. else if(position==4){
  47. lp.leftMargin = (int) (positionOffsetPixels / 5) + screenWidth*4
  48. + offset;
  49. }
  50. }

代码并不难,希望通过我这个例子,一是巩固自己的学习和理解,二是希望更多的人更好的学习,我会再接再厉,写更多的博文。

源码下载

发表评论

表情:
评论列表 (有 0 条评论,225人围观)

还没有评论,来说两句吧...

相关阅读

    相关 爬虫--今日

    1、分析今日头条   在看头条的时候可以发现展示出来的页面的数据都是一些封装过的js代码或者css代码,所以这时候就需要考虑页面的数据是不是封装在cookie里面了   回

    相关 今日Android面试

    点击关注异步图书,置顶公众号 每天与你分享 IT好书 技术干货 职场知识 首先说一下,今日头条的面试主要分为三轮到四轮,如果是旺季面三轮,首先是基础面试,基本面试