欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android顶部三个TabLayout,滑动到某条目之后就会切换到相应的TabLayout(代码实现)

程序员文章站 2022-09-02 18:55:58
android顶部三个tablayout,滑动到某条目之后就会切换到相应的tablayout(代码实现) xml布局:...

android顶部三个tablayout,滑动到某条目之后就会切换到相应的tablayout(代码实现)


xml布局:



 

  

  

  
 
 
mainactivity中:
public class mainactivity extends appcompatactivity implements abslistview.onscrolllistener {

 private tablayout tb;
 private listview lv;
 private arraylist list;
 private int position;
 private customadapter adapter1;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  list = new arraylist<>();
  for (int i = 0; i <200; i++) {
list.add("龙雀" + i);
  }
  initview();

 }

 private void initview() {
  tb = (tablayout) findviewbyid(r.id.tb);
  lv = (listview) findviewbyid(r.id.lv);
  adapter1 = new customadapter(list,this);
  lv.setadapter(adapter1);
  lv.setonscrolllistener(this);
  for (int i = 0; i < tb.gettabcount(); i++) {
tablayout.tab tab = tb.gettabat(i);
if (tab == null)
 return;

class c = tab.getclass();
try {

 field field = c.getdeclaredfield("mview");

 field.setaccessible(true);
 final view view = (view) field.get(tab);
 if (view == null)
  return;
 view.settag(i);
 view.setonclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view v) {
position = (int) view.gettag();
switch (position) {

 case 0:
  lv.setselection(0);
  break;
 case 1:

  lv.setselection(50);


  break;
 case 2:
  lv.setselection(70);
  break;
}

  }
 });
} catch (exception e) {
 e.printstacktrace();
}
  }
 }

 int post;
 @override
 public void onscrollstatechanged(abslistview view, int scrollstate) {
  switch (scrollstate) {
case 0:
 switch (post) {
  case 0:

tablayout.tab tab = tb.gettabat(0);
tab.select();
break;

  case 50:
tablayout.tab tab2 = tb.gettabat(1);
tab2.select();

break;
  case 70:
tablayout.tab tab3 = tb.gettabat(2);
tab3.select();

break;


 }
 toast.maketext(this, "我现在处于不滑动状态", toast.length_short).show();
 break;
case 1:
 toast.maketext(this, "我现在处于滑动状态", toast.length_short).show();
 break;
  }

 }

 @override
 public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {
  switch (firstvisibleitem) {

case 0:
 post = 0;
 break;

case 50:
 post = 50;
 break;
case 70:
 post = 70;
 break;
case 49:
 post = 0;
 break;

case 69:
 post = 50;
 break;

  }
 }
}

适配器:

public class customadapter extends baseadapter {
 private arraylist list = new arraylist<>();
 private context context;

 public customadapter(arraylist list, context context) {
  this.list = list;
  this.context = context;
 }

 @override
 public int getcount() {
  return list.size();
 }

 @override
 public object getitem(int position) {
  return list.get(position);
 }

 @override
 public long getitemid(int position) {
  return position;
 }

 @override
 public view getview(int position, view convertview, viewgroup parent) {
  viewholder viewholder = null;
  if (convertview == null) {
convertview = layoutinflater.from(context).inflate(r.layout.item, null);
viewholder = new viewholder(convertview);
convertview.settag(viewholder);
  }else {
viewholder = (viewholder)convertview.gettag();
  }
  viewholder.tv.settext(list.get(position));
  return convertview;
 }

 public static class viewholder {
  public view rootview;
  public textview tv;

  public viewholder(view rootview) {
this.rootview = rootview;
this.tv = (textview) rootview.findviewbyid(r.id.tv);
  }
 }
}