码迷,mamicode.com
首页 > 其他好文 > 详细

ViewPager实现页面切换

时间:2019-10-15 19:13:07      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:orm   display   etc   from   world   设置   efault   set   extend   

上图先:

技术图片

采用3个布局文件的形式设置滑动页面

xml布局:

 1  <android.support.v4.view.ViewPager
 2         android:id="@+id/vp_robot_main"
 3         android:layout_width="match_parent"
 4         android:layout_height="236dp">
 5 
 6     </android.support.v4.view.ViewPager>
 7 
 8     <!--小圆点-->
 9     <LinearLayout
10         android:id="@+id/viewGroup"
11         android:layout_width="match_parent"
12         android:layout_height="33dp"
13         android:layout_alignParentBottom="true"
14         android:gravity="center"
15         android:orientation="vertical">
16 
17         <LinearLayout
18             android:layout_width="wrap_content"
19             android:layout_height="wrap_content"
20             android:layout_marginTop="3dip"
21             android:orientation="horizontal">
22 
23 
24             <View
25                 android:id="@+id/dot_1"
26                 android:layout_width="10dp"
27                 android:layout_height="10dp"
28                 android:layout_marginLeft="4dip"
29                 android:layout_marginRight="4dip"
30                 android:background="@drawable/dot_normal" />
31 
32             <View
33                 android:id="@+id/dot_2"
34                 android:layout_width="10dp"
35                 android:layout_height="10dp"
36                 android:layout_marginLeft="4dip"
37                 android:layout_marginRight="4dip"
38                 android:background="@drawable/dot_normal" >
39 
40             </View>
41 
42             <View
43                 android:id="@+id/dot_3"
44                 android:layout_width="10dp"
45                 android:layout_height="10dp"
46                 android:layout_marginLeft="4dip"
47                 android:layout_marginRight="4dip"
48                 android:background="@drawable/dot_normal" />
49         </LinearLayout>
50     </LinearLayout>

绑定子页面:viewpager_1, viewpager_2, viewpager_3 这三个子布局需要另外创建;

以及添加小圆点至list里面。

 1  //设置viewpager布局
 2         vp_robot_main = contentView.findViewById(R.id.vp_robot_main);
 3 
 4         views = new ArrayList<>();
 5         LayoutInflater mLI = LayoutInflater.from(this);
 6         View view1 = mLI.inflate(R.layout.viewpager_1,null);
 7         View view2 = mLI.inflate(R.layout.viewpager_2,null);
 8         View view3 = mLI.inflate(R.layout.viewpager_3,null);
 9         views.add(view1);
10         views.add(view2);
11         views.add(view3);
12         //小圆点
13         dots = new ArrayList<>();
14         dots.add(findViewById(R.id.dot_1));
15         dots.add(findViewById(R.id.dot_2));
16         dots.add(findViewById(R.id.dot_3));

 

 1  /**
 2      * 配置主页adapter
 3      */
 4     private void setMainAdapter() {
 5 
 6         MyViewPagerAdapter myViewPagerAdapter = new MyViewPagerAdapter(this,views,mainItemModelList);
 7         vp_robot_main.setAdapter(myViewPagerAdapter);
 8 
 9         //设置指示器状态
10         //小圆点选中状态
11         dots.get(0).setBackgroundResource(R.drawable.dot_focused);
12         vp_robot_main.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
13             @Override
14             public void onPageScrolled(int i, float v, int i1) {
15 
16             }
17 
18             @Override
19             public void onPageSelected(int position) {
20 
21                 dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);
22                 dots.get(position).setBackgroundResource(R.drawable.dot_focused);
23                 oldPosition = position;
24             }
25 
26             @Override
27             public void onPageScrollStateChanged(int i) {
28 
29             }
30         });
31    }

自定义viewpager的adapter:

  1 public class MyViewPagerAdapter extends PagerAdapter implements View.OnClickListener {
  2     private Context mContext;
  3     private List<View> views;
  4     private List<MainItemModel> mainItemModels = new ArrayList<>();
  5     //主页中的9个item
  6     private ImageView image1,image2,image3,image4
  7             ,image5,image6,image7,image8,image9;
  8 
  9     public MyViewPagerAdapter(Context context, List<View> views, List<MainItemModel> mainItemModels){
 10         this.mContext = context;
 11         this.views = views;
 12         this.mainItemModels = mainItemModels;
 13 
 14     }
 15     @Override
 16     public int getCount() {
 17         return views.size();
 18     }
 19 
 20     @Override
 21     public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
 22         return view == object;
 23     }
 24 
 25     @NonNull
 26     @Override
 27     public Object instantiateItem(@NonNull ViewGroup container, int position) {
 28 
 29         KLog.d("这是instantiateItem");
 30         container.addView(views.get(position));
 31         switch (position){
 32             case 0:
 33                 KLog.d("000");
 34                 image1 = views.get(0).findViewById(R.id.iv_image1);
 35                 image2 = views.get(0).findViewById(R.id.iv_image2);
 36                 image3 = views.get(0).findViewById(R.id.iv_image3);
 37                 image4 = views.get(0).findViewById(R.id.iv_image4);
 38 
 39                 image1.setTag(0);
 40                 image1.setOnClickListener(this);
 41                 image2.setTag(1);
 42                 image2.setOnClickListener(this);
 43                 image3.setTag(2);
 44                 image3.setOnClickListener(this);
 45                 image4.setTag(3);
 46                 image4.setOnClickListener(this);
 47 
 48                 break;
 49 
 50             case 1:
 51                 KLog.d("111");
 52                 image5 = views.get(1).findViewById(R.id.iv_image5);
 53                 image6 = views.get(1).findViewById(R.id.iv_image6);
 54                 image7 = views.get(1).findViewById(R.id.iv_image7);
 55                 image8 = views.get(1).findViewById(R.id.iv_image8);
 56                 image5.setTag(4);
 57                 image5.setOnClickListener(this);
 58                 image6.setTag(5);
 59                 image6.setOnClickListener(this);
 60                 image7.setTag(6);
 61                 image7.setOnClickListener(this);
 62                 image8.setTag(7);
 63                 image8.setOnClickListener(this);
 64                 break;
 65             case 2:
 66                 KLog.d("222");
 67                 image9 = views.get(2).findViewById(R.id.iv_image9);
 68                 image9.setTag(8);
 69                 image9.setOnClickListener(this);
 70                 break;
 71             default:
 72                 break;
 73         }
 74         return views.get(position);
 75     }
 76 
 77     @Override
 78     public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
 79         container.removeView(views.get(position));
 80     }
 81 
 82     public void skipActivity(Class clz) {
 83         if (!MyUtil.isFastDoubleClick()) {
 84             startActivity(new Intent(mContext, clz));
 85         }
 86     }
 87 
 88     public void onClick(View view){
 89         int tag = (int) view.getTag();
 90         switch(tag){
 91             case 0:
 92                 if (AIChatManager.isQrDreamInitSuccess) {
 93                     AIChatManager.getInstance().startTTS("同步课堂");
 94                 }
 95                 skipActivity(SyncClassActivity.class);
 96                 break;
 97             case 1:
 98                 if (AIChatManager.isQrDreamInitSuccess) {
 99                     AIChatManager.getInstance().startTTS("幼儿启蒙");
100                 }
101                 skipActivity(ChildEnlightenActivity.class);
102                 break;
103             case 2:
104                 if (AIChatManager.isQrDreamInitSuccess) {
105                     AIChatManager.getInstance().startTTS("文学乐园");
106                 }
107                 skipActivity(LiteratureWorldActivity.class);
108                 break;
109             case 3:
110                 if (AIChatManager.isQrDreamInitSuccess) {
111                     AIChatManager.getInstance().startTTS("绘本识图");
112                 }
113                 CSZJUtil.skipOtherApp(mContext, CSZJUtil.PICTUREBOOK_PACKAGE_NAME);
114                 break;
115             case 4:
116                 if (AIChatManager.isQrDreamInitSuccess) {
117                     AIChatManager.getInstance().startTTS("数学乐园");
118                 }
119                 skipActivity(MathWorldActivity.class);
120                 break;
121             case 5:
122                 if (AIChatManager.isQrDreamInitSuccess) {
123                     AIChatManager.getInstance().startTTS("英语乐园");
124                 }
125                 Intent inetnt = new Intent(mContext, NetworkResDisplayActivity.class);
126                         inetnt.putExtra(AppConstants.NETWORK_RES_INTENT_KEY, AppConstants.NETWORK_RES_ENGLISH_SONG);
127                         startActivity(inetnt);
128                 break;
129             case 6:
130                 if (AIChatManager.isQrDreamInitSuccess) {
131                     AIChatManager.getInstance().startTTS("科学天地");
132                 }
133                 skipActivity(ScienceActivity.class);
134                 break;
135             case 7:
136                 if (AIChatManager.isQrDreamInitSuccess) {
137                     AIChatManager.getInstance().startTTS("娱乐天地");
138                 }
139                 skipActivity(RecreationWorldActivity.class);
140                 break;
141             case 8:
142                 if (AIChatManager.isQrDreamInitSuccess) {
143                     AIChatManager.getInstance().startTTS("百宝箱");
144                 }
145                 skipActivity(TreasureBoxActivity.class);
146                 break;
147             default:
148                 break;
149 
150         }
151     }
152 }

 

ViewPager实现页面切换

标签:orm   display   etc   from   world   设置   efault   set   extend   

原文地址:https://www.cnblogs.com/ken9527just/p/11446776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!