首页 热点专区 小学知识 中学知识 出国留学 考研考公
您的当前位置:首页正文

TabLayout+Fragment+ListView使用

2024-12-16 来源:要发发知识网

1.实现适配器

public class FragAdapter extends FragmentPagerAdapter {    
private List<Fragment> list_fragment;    
private List<String> list_title;    
public FragAdapter(FragmentManager fm,List<Fragment> list_fragment,List<String> list_title) {        
super(fm);       
 this.list_fragment=list_fragment;    
    this.list_title=list_title; 
   }   
 @Override   
 public Fragment getItem(int position) {      
  return list_fragment.get(position);  
  }   
 @Override    
public int getCount() {      
  return list_title.size();   
 }    
@Override  
  public CharSequence getPageTitle(int position) {     
   return list_title.get(position%list_title.size());    }}

2.按照自己的需求创建Fragment

public class Fragment2 extends Fragment {   
 @Nullable   
 @Override   
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {      
  View view=inflater.inflate(R.layout.frag2,container,false);        List<ItemBean> list=new ArrayList<>();      
  for(int i=0;i<200;i++){           
 list.add(new ItemBean(           
         R.mipmap.ic_launcher,            
        "标题",              
      "内容"            ));      
  }       
 MyAdapter adapter=new MyAdapter(getActivity(),list);     
   ListView listView=(ListView)view.findViewById(R.id.listview);
   listView.setAdapter(adapter);      
  return view;    }}

需要注意的是:
MyAdapter adapter=new MyAdapter(getActivity(),list);
这里通过getActivity()获取页面的context
3.设置ListView的适配器和容器
之前已经写过类似的所以不再重复

4.主界面

public class MainActivity extends FragmentActivity {    
private TabLayout tab_FindFragment_title;    
private ViewPager vp_FindFragment_pager;   
 private FragAdapter fAdapter;   
 private List<Fragment> list_fragment;   
 private List<String> list_title;  
  private Fragment1 fragment1;   
 private Fragment2 fragment2;  
  @Override   
 protected void onCreate(Bundle savedInstanceState) {        
super.onCreate(savedInstanceState);        
setContentView(R.layout.activity_main);      
tab_FindFragment_title=(TabLayout)findViewById(R.id.tab_FindFragment_title);        vp_FindFragment_pager=(ViewPager)findViewById(R.id.vp_FindFragment_pager);        
    fragment1=new Fragment1();   
    fragment2=new Fragment2();     
    list_fragment=new ArrayList<>();            
    list_fragment.add(fragment1);     
    list_fragment.add(fragment2);     
    list_title=new ArrayList<>();     
    list_title.add("aaa");      
    list_title.add("bbb");        
tab_FindFragment_title.setTabMode(TabLayout.MODE_FIXED);        
tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(0)));        
tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(1)));      
 fAdapter=new 
FragAdapter(getSupportFragmentManager(),list_fragment,list_title);        
vp_FindFragment_pager.setAdapter(fAdapter);        
tab_FindFragment_title.setupWithViewPager(vp_FindFragment_pager);    }}

步骤:
1.添加fragment对象到fragment列表
2.添加title到title列表
3.从title列表中取出内容放到tabLayout容器中
4.创建并初始化适配器
5.为viewPager设定适配器
6.通过tab_FindFragment_title.setupWithViewPager(vp_FindFragment_pager
将TabLayout和ViewPager关联起来

效果截图:

ViewPager
显示全文