viewpager怎么在android中使用
作者
viewpager怎么在android中使用?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
(1)简单写一个主界面的布局activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="true" android:fitsSystemWindows="true" android:background="@color/bg_color"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/bag_gray" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="36dp" android:background="@android:color/white" android:orientation="horizontal" android:weightSum="3"> <TextView android:id="@+id/tab1_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="商品" android:textColor="@color/title_bag" android:textSize="18sp"/> <TextView android:id="@+id/tab2_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="评价" android:textColor="@color/text_color_context" android:textSize="18sp"/> <TextView android:id="@+id/tab3_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="详情" android:textColor="@color/text_color_context" android:textSize="18sp"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="@color/text_color_context"/> <View android:id="@+id/cursor" android:layout_width="50dp" android:layout_height="2dp" android:layout_marginLeft="40dp" android:layout_marginTop="0dip" android:background="@color/title_bag" /> <android.support.v4.view.ViewPager android:id="@+id/thire_vp" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> </LinearLayout>
(2)设置viewpager的适配器:FragmentAdapter
publicclassFragmentAdapterextendsFragmentPagerAdapter{ privateArrayList<Fragment>list; FragmentManagerfm; publicFragmentAdapter(FragmentManagerfm,ArrayList<Fragment>list){ super(fm); this.fm=fm; this.list=list; } @Override publicFragmentgetItem(intposition){ returnlist.get(position); } @Override publicintgetCount(){ returnlist.size(); } }
(3)然后设置三个fragment,因为有三个选项卡,所以我们新建三个fragment,分别是OneFragment、TwoFragment 、ThreeFragment,布局的话也需要新建三个,跟fragment一一对应,因为布局过于简单,这里就不写了,简单写一点fragment的代码吧
publicclassOneFragmentextendsFragment{ @Nullable @Override publicViewonCreateView(LayoutInflaterinflater,@NullableViewGroupcontainer,@NullableBundlesavedInstanceState){ Viewview=inflater.inflate(R.layout.fragment_one,null); returnview; } }
(4)在MainActivity中,设置fragment的适配器,设置显示内容,并且做viewpager的事件监听
publicclassMainActivityextendsFragmentActivityimplementsViewPager.OnPageChangeListener,View.OnClickListener{ privateTextViewtab1Tv; privateTextViewtab2Tv; privateTextViewtab3Tv; privateViewcursor; privateViewPagerthirdVp; privateArrayList<Fragment>fragmentlist; privateintoffset=0; privateintscreenWidth=0; privateintscreenl_3; privateLinearLayout.LayoutParamslp; @Override protectedvoidonCreate(@NullableBundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_product); //绑定控件 tab1Tv=(TextView)findViewById(R.id.tab1_tv); tab2Tv=(TextView)findViewById(R.id.tab2_tv); tab3Tv=(TextView)findViewById(R.id.tab3_tv); cursor=(View)findViewById(R.id.cursor); thirdVp=(ViewPager)findViewById(R.id.thire_vp); //获取屏幕宽度 DisplayMetricsdm=newDisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); screenWidth=dm.widthPixels; screenl_3=screenWidth/3;//裁剪3分之1 lp=(LinearLayout.LayoutParams)cursor.getLayoutParams(); fragmentlist=newArrayList<>(); fragmentlist.add(newOneFragment()); fragmentlist.add(newTwoFragment()); fragmentlist.add(newThreeFragment()); thirdVp.setAdapter(newFragmentAdapter(getSupportFragmentManager(),fragmentlist)); thirdVp.setCurrentItem(0); thirdVp.setOffscreenPageLimit(2); thirdVp.setOnPageChangeListener(this); tab1Tv.setOnClickListener(this); tab2Tv.setOnClickListener(this); tab3Tv.setOnClickListener(this); } @Override publicvoidonPageScrolled(intposition,floatpositionOffset,intpositionOffsetPixels){ offset=(screenl_3-cursor.getLayoutParams().width)/2; Log.d("TAG","111----"+position+"--"+positionOffset+"--" +positionOffsetPixels); finalfloatscale=getResources().getDisplayMetrics().density; if(position==0){ lp.leftMargin=(int)(positionOffsetPixels/3)+offset; }elseif(position==1){ lp.leftMargin=(int)(positionOffsetPixels/3)+screenl_3+offset; } cursor.setLayoutParams(lp); upTextcolor(position); } privatevoidupTextcolor(intposition){ if(position==0){ tab1Tv.setTextColor(getResources().getColor(R.color.title_bag)); tab2Tv.setTextColor(getResources().getColor(R.color.text_color_context)); tab3Tv.setTextColor(getResources().getColor(R.color.text_color_context)); }elseif(position==1){ tab1Tv.setTextColor(getResources().getColor(R.color.text_color_context)); tab2Tv.setTextColor(getResources().getColor(R.color.title_bag)); tab3Tv.setTextColor(getResources().getColor(R.color.text_color_context)); }elseif(position==2){ tab1Tv.setTextColor(getResources().getColor(R.color.text_color_context)); tab2Tv.setTextColor(getResources().getColor(R.color.text_color_context)); tab3Tv.setTextColor(getResources().getColor(R.color.title_bag)); } } @Override publicvoidonPageSelected(intposition){ } @Override publicvoidonPageScrollStateChanged(intstate){ } @Override publicvoidonClick(Viewview){ switch(view.getId()){ caseR.id.tab1_tv: thirdVp.setCurrentItem(0); break; caseR.id.tab2_tv: thirdVp.setCurrentItem(1); break; caseR.id.tab3_tv: thirdVp.setCurrentItem(2); break; } } }
看完上述内容,你们掌握viewpager怎么在android中使用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注恰卡编程网行业资讯频道,感谢各位的阅读!
目录
推荐阅读
-
怎么使用Android基准配置文件Baseline Profile方案提升启动速度
-
HTML5如何实现禁止android视频另存为
-
学java好还是学php好?
-
Android如何实现多点触控功能
-
android怎么实现多点触摸应用
-
Android怎么实现手势划定区域裁剪图片
-
android怎么实现简单的矩形裁剪框
-
Android单选多选按钮怎么使用
-
Android中如何利用oncreate获取控件高度或宽度
Android中如何利用oncreate获取控件高度或宽度本篇内容...
-
Android中怎么使用onSaveInstanceState()方法
Android中怎么使用onSaveInstanceState()方法...
0 条评论
本站已关闭游客评论,请登录或者注册后再评论吧~