布局文件
//RecyclerView和VerticalTabLayout布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<q.rorbin.verticaltablayout.VerticalTabLayout
android:id="@+id/tab"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:scrollIndicators="bottom"
android:fitsSystemWindows="true"
app:indicator_corners="5dp"
app:indicator_width="2dp"
android:background="@color/cececce"
app:indicator_color="#a41d1d"
app:indicator_gravity="left"
app:tab_height="50dp"
app:tab_mode="scrollable" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rlv"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="match_parent"/>
</LinearLayout>
Activity或Fragment使用过程
public class MoveFragment extends BaseFragment<MoveP, MoveV> 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<MoveBean.DataBean> 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<MoveBean.DataBean> list;
private Context context;
private List<MoveBean.DataBean.ArticlesBean> articles;
public NavigationAdapter(List<MoveBean.DataBean> 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<MoveBean.DataBean> 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布局代码(流式布局)
<com.example.dell.big_wanandroid.utils.FlowLayout
android:id="@+id/tabflowlayout"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
textview_item布局代码
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="@dimen/dp_8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:maxLines="1"
android:ellipsize="end"
android:gravity="center"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:background="@drawable/textshape"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:textColor="@color/gray"
android:textSize="14sp"/>
网友评论