十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
viewpager怎么在android中使用?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
创新互联企业建站,十多年网站建设经验,专注于网站建设技术,精于网页设计,有多年建站和网站代运营经验,设计师为客户打造网络企业风格,提供周到的建站售前咨询和贴心的售后服务。对于网站设计制作、做网站中不同领域进行深入了解和探索,创新互联在网站建设中充分了解客户行业的需求,以灵动的思维在网页中充分展现,通过对客户行业精准市场调研,为客户提供的解决方案。
(1)简单写一个主界面的布局activity_main.xml
(2)设置viewpager的适配器:FragmentAdapter
public class FragmentAdapter extends FragmentPagerAdapter { private ArrayListlist; FragmentManager fm; public FragmentAdapter(FragmentManager fm, ArrayList list){ super(fm); this.fm = fm; this.list = list; } @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } }
(3)然后设置三个fragment,因为有三个选项卡,所以我们新建三个fragment,分别是OneFragment、TwoFragment 、ThreeFragment ,布局的话也需要新建三个,跟fragment一一对应,因为布局过于简单,这里就不写了,简单写一点fragment的代码吧
public class OneFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_one,null); return view; } }
(4)在MainActivity中,设置fragment的适配器,设置显示内容,并且做viewpager的事件监听
public class MainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener,View.OnClickListener{ private TextView tab1Tv; private TextView tab2Tv; private TextView tab3Tv; private View cursor; private ViewPager thirdVp; private ArrayListfragmentlist; private int offset = 0; private int screenWidth = 0; private int screenl_3; private LinearLayout.LayoutParams lp; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { 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); //获取屏幕宽度 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); screenWidth = dm.widthPixels; screenl_3 = screenWidth/3; //裁剪3分之1 lp = (LinearLayout.LayoutParams)cursor.getLayoutParams(); fragmentlist = new ArrayList<>(); fragmentlist.add(new OneFragment()); fragmentlist.add(new TwoFragment()); fragmentlist.add(new ThreeFragment()); thirdVp.setAdapter(new FragmentAdapter(getSupportFragmentManager(),fragmentlist)); thirdVp.setCurrentItem(0); thirdVp.setOffscreenPageLimit(2); thirdVp.setOnPageChangeListener(this); tab1Tv.setOnClickListener(this); tab2Tv.setOnClickListener(this); tab3Tv.setOnClickListener(this); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { offset = (screenl_3-cursor.getLayoutParams().width)/2; Log.d("TAG", "111----"+position + "--" + positionOffset + "--" + positionOffsetPixels); final float scale = getResources().getDisplayMetrics().density; if (position == 0){ lp.leftMargin = (int)(positionOffsetPixels/3)+offset; }else if(position ==1){ lp.leftMargin = (int)(positionOffsetPixels/3)+screenl_3+offset; } cursor.setLayoutParams(lp); upTextcolor(position); } private void upTextcolor(int position){ 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)); }else if(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)); }else if(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 public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { } @Override public void onClick(View view) { switch (view.getId()) { case R.id.tab1_tv: thirdVp.setCurrentItem(0); break; case R.id.tab2_tv: thirdVp.setCurrentItem(1); break; case R.id.tab3_tv: thirdVp.setCurrentItem(2); break; } } }
看完上述内容,你们掌握viewpager怎么在android中使用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!