VerticalTabLayout(垂直ToolBar)和RecyclerView(套用FlowLayout流式布局)的用法及联动

布局文件

//RecyclerView和VerticalTabLayout布局
   
   

   
 

Activity或Fragment使用过程

public class MoveFragment extends BaseFragment implements MoveV {
   private static final String TAG = "MoveFragment";
   @BindView(R.id.tab)
   VerticalTabLayout tab;
   @BindView(R.id.rlv)
   RecyclerView rlv;
   @BindView(R.id.flbtn)
   FloatingActionButton flbtn;
   private LinearLayoutManager mLayoutManager;
   private NavigationAdapter mRecyclerAdapter;
   private RadioGroup rgtbn;


   protected void initData() {
       presenter.getdata();
   }

   @Override
   protected MoveP initpresenter() {
       return new MoveP();
   }

   @Override
   protected int getlayout() {
       return R.layout.move;
   }

   @Override
   public void show(MoveBean bean) {
       final List list = bean.getData();
       Log.i(TAG,list.toString());
       //创建布局管理器
       mLayoutManager = new LinearLayoutManager(getContext());
       //设置布局管理器
       rlv.setLayoutManager(mLayoutManager);
       //创建适配器(适配器代码在下面)
       mRecyclerAdapter = new NavigationAdapter(list, getContext());
       //绑定适配器
       rlv.setAdapter(mRecyclerAdapter);

       //绑定VerticalTabLayout适配器
       tab.setTabAdapter(new TabAdapter() {
           @Override
           public int getCount() {
               return list.size();
           }

           @Override
           public ITabView.TabBadge getBadge(int position) {
               return null;
           }

           @Override
           public ITabView.TabIcon getIcon(int position) {
               return null;
           }

           @Override
           public ITabView.TabTitle getTitle(int position) {
               return new ITabView.TabTitle.Builder()
                       .setContent(list.get(position).getName())
                       .setTextColor(0xFF36BC9B, 0xFF757575)
                       .build();
           }

           @Override
           public int getBackground(int position) {
               return 0;
           }
       });

       //RecyclerView和tab栏联动
       rlv.addOnScrollListener(new RecyclerView.OnScrollListener() {
           @Override
           public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
               super.onScrollStateChanged(recyclerView, newState);
               LinearLayoutManager manager = (LinearLayoutManager) rlv.getLayoutManager();
               tab.setTabSelected(manager.findFirstVisibleItemPosition());
           }
       });
       //tab栏和RecyclerView联动
       tab.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
           @Override
           public void onTabSelected(TabView tab, int position) {
               LinearLayoutManager layoutManager = (LinearLayoutManager) rlv.getLayoutManager();
               layoutManager.scrollToPositionWithOffset(position, 0);
           }

           @Override
           public void onTabReselected(TabView tab, int position) {

           }
       });
   }

}

适配器

public class NavigationAdapter extends RecyclerView.Adapter {

   public List list;
   private Context context;
   private List articles;

   public NavigationAdapter(List list, Context context) {
       this.list = list;
       this.context = context;
   }

   @NonNull
   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
       View inflate = LayoutInflater.from(context).inflate(R.layout.navigationitem, null);
       ViewHolder viewHolder = new ViewHolder(inflate);
       return viewHolder;
   }

   @Override
   public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
       ViewHolder viewHolder1 = (ViewHolder) viewHolder;
       viewHolder1.tv.setText(list.get(i).getName());
       articles = list.get(i).getArticles();
       for (int j = 0; j < articles.size(); j++) {
           TextView inflate = (TextView) View.inflate(context, R.layout.textview_item, null);
           //设置流式布局颜色变化
           Random random = new Random();
           int randomColor = 0x00ffffff | random.nextInt(0xffffff00);
           inflate.setBackgroundColor(randomColor);
           final String link = articles.get(j).getLink();
           final String title = articles.get(j).getTitle();
           inflate.setText(title);
           //添加流式布局
           viewHolder1.tabflowlayout.addView(inflate);
           //点击时间
           inflate.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   Intent intent = new Intent(context, MoveWebActivity.class);
                   intent.putExtra("link", link);
                   intent.putExtra("title", title);
                   context.startActivity(intent);
               }
           });
       }

   }

   @Override
   public int getItemCount() {
       return list.size();
   }

   public void addData(List bean1) {
       //添加数据并刷新
       list.addAll(bean1);
       notifyDataSetChanged();
   }

   public static class ViewHolder extends RecyclerView.ViewHolder {
       public View rootView;
       public TextView tv;
       public FlowLayout tabflowlayout;

       public ViewHolder(View rootView) {
           super(rootView);
           this.rootView = rootView;
           this.tv = (TextView) rootView.findViewById(R.id.tv);
           this.tabflowlayout = (FlowLayout) rootView.findViewById(R.id.tabflowlayout);
       }
   }

}

navigationitem布局代码(流式布局)


textview_item布局代码


你可能感兴趣的:(VerticalTabLayout(垂直ToolBar)和RecyclerView(套用FlowLayout流式布局)的用法及联动)