android tablayout怎么使用代码切换tab
2个回答
展开全部
android tablayout怎么使用代码切换tab
重新设置点击事件
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
if (tab != null) {
tab.setCustomView(pagerAdapter.getTabView(i));
if (tab.getCustomView() != null) {
View tabView = (View) tab.getCustomView().getParent();
tabView.setTag(i);
tabView.setOnClickListener(mTabOnClickListener);
}
}
}
viewPager.setCurrentItem(1);
处理点击事件
private View.OnClickListener mTabOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = (int) view.getTag();
if (pos == 0) {
Toast.makeText(BottomTabActivity.this, "您还没有登录", Toast.LENGTH_SHORT).show();
//TODO 跳转到登录界面
} else {
TabLayout.Tab tab = tabLayout.getTabAt(pos);
if (tab != null) {
tab.select();
}
}
}
};
重新设置点击事件
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
if (tab != null) {
tab.setCustomView(pagerAdapter.getTabView(i));
if (tab.getCustomView() != null) {
View tabView = (View) tab.getCustomView().getParent();
tabView.setTag(i);
tabView.setOnClickListener(mTabOnClickListener);
}
}
}
viewPager.setCurrentItem(1);
处理点击事件
private View.OnClickListener mTabOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = (int) view.getTag();
if (pos == 0) {
Toast.makeText(BottomTabActivity.this, "您还没有登录", Toast.LENGTH_SHORT).show();
//TODO 跳转到登录界面
} else {
TabLayout.Tab tab = tabLayout.getTabAt(pos);
if (tab != null) {
tab.select();
}
}
}
};
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class MainActivity extends AppCompatActivity{
private TabLayout tablayout;
private ViewPager vp;
private ArrayList<Fragment> list1;
private ArrayList<String> list2;
private FragmentManager fm;
private MyAdapter myadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tablayout = (TabLayout) findViewById(R.id.tablayout);
vp = (ViewPager) findViewById(R.id.vp);
fm = getSupportFragmentManager();
list1 = new ArrayList<>();
list2 = new ArrayList<>();
AFragment aFragment = new AFragment();
BFragment bFragment = new BFragment();
CFragment cFragment = new CFragment();
DFragment dFragment = new DFragment();
EFragment eFragment = new EFragment();
list1.add(aFragment);
list1.add(bFragment);
list1.add(cFragment);
list1.add(dFragment);
list1.add(eFragment);
list2.add("1");
list2.add("2");
list2.add("3");
list2.add("4");
list2.add("5");
myadapter = new MyAdapter(fm,list1,list2);
vp.setAdapter(myadapter);
tablayout.addTab(tablayout.newTab().setText(list2.get(0)));
tablayout.addTab(tablayout.newTab().setText(list2.get(1)));
tablayout.addTab(tablayout.newTab().setText(list2.get(2)));
tablayout.addTab(tablayout.newTab().setText(list2.get(3)));
tablayout.addTab(tablayout.newTab().setText(list2.get(4)));
tablayout.setupWithViewPager(vp);
}
}
public class MyAdapter extends FragmentPagerAdapter{
private ArrayList<Fragment> list1;
private ArrayList<String> list2;
public MyAdapter(FragmentManager fm, ArrayList<Fragment> list1, ArrayList<String> list2) {
super(fm);
this.list1 = list1;
this.list2 = list2;
}
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return list1.get(position);
}
@Override
public int getCount() {
return list2.size();
}
@Override
public CharSequence getPageTitle(int position) {
return list2.get(position);
}
}
private TabLayout tablayout;
private ViewPager vp;
private ArrayList<Fragment> list1;
private ArrayList<String> list2;
private FragmentManager fm;
private MyAdapter myadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tablayout = (TabLayout) findViewById(R.id.tablayout);
vp = (ViewPager) findViewById(R.id.vp);
fm = getSupportFragmentManager();
list1 = new ArrayList<>();
list2 = new ArrayList<>();
AFragment aFragment = new AFragment();
BFragment bFragment = new BFragment();
CFragment cFragment = new CFragment();
DFragment dFragment = new DFragment();
EFragment eFragment = new EFragment();
list1.add(aFragment);
list1.add(bFragment);
list1.add(cFragment);
list1.add(dFragment);
list1.add(eFragment);
list2.add("1");
list2.add("2");
list2.add("3");
list2.add("4");
list2.add("5");
myadapter = new MyAdapter(fm,list1,list2);
vp.setAdapter(myadapter);
tablayout.addTab(tablayout.newTab().setText(list2.get(0)));
tablayout.addTab(tablayout.newTab().setText(list2.get(1)));
tablayout.addTab(tablayout.newTab().setText(list2.get(2)));
tablayout.addTab(tablayout.newTab().setText(list2.get(3)));
tablayout.addTab(tablayout.newTab().setText(list2.get(4)));
tablayout.setupWithViewPager(vp);
}
}
public class MyAdapter extends FragmentPagerAdapter{
private ArrayList<Fragment> list1;
private ArrayList<String> list2;
public MyAdapter(FragmentManager fm, ArrayList<Fragment> list1, ArrayList<String> list2) {
super(fm);
this.list1 = list1;
this.list2 = list2;
}
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return list1.get(position);
}
@Override
public int getCount() {
return list2.size();
}
@Override
public CharSequence getPageTitle(int position) {
return list2.get(position);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询