RecyclerView的多部局应用

需要依赖一个recyderView包

implementation 'com.android.support:recyclerview-v7:26.1.0'


先写布局这个是自定义布局

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text_Title"
        />
</android.support.constraint.ConstraintLayout>


在适配器中使用布局一种应用


public class ViewHolder_1 extends RecyclerView.ViewHolder{
    private TextView textView;
    private Context context;

    public ViewHolder_1(View itemView, Context context) {
        super(itemView);
        this.context = context;
    }

    public ViewHolder_1(View itemView) {
        super(itemView);
        textView = itemView.findViewById(R.id.text_Title);
    }
    public void rendr(String text){
        textView.setText(text);
    }
}



下面是适配器


public class MyAdapter extends SectionedRecyclerViewAdapter<ViewHolder_1,ViewHolder_2,ViewHolder_3>{
    private Context context;

    public MyAdapter(Context context) {
        this.context = context;
    }
    
    //一共有几个item
    @Override
    protected int getSectionCount() {
        return 5;
    }
    
    
    @Override
    protected int getItemCountForSection(int section) {
        return section+1;//表示条目
    }
    //返回true表示含有脚视图
    @Override
    protected boolean hasFooterInSection(int section) {
        return true;
    }
    
    //头部的布局
    @Override
    protected ViewHolder_1 onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.viewholder_1,parent,false);
        return new ViewHolder_1(view);
    }
    
    //脚部的布局
    @Override
    protected ViewHolder_3 onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.viewholder_3,parent,false);
        return new ViewHolder_3(view);
    }
    
    //中间的布局
    @Override
    protected ViewHolder_2 onCreateItemViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.viewholder_2,parent,false);
        return new ViewHolder_2(view);
    }
    
    //传向布局一的数据
    @Override
    protected void onBindSectionHeaderViewHolder(ViewHolder_1 holder, int section) {
        holder.rendr("section"+(section+1));
    }
    //传向脚部布局的数据
    @Override
    protected void onBindSectionFooterViewHolder(ViewHolder_3 holder, int section) {
        holder.rendr("section"+(section+1));
    }
    
    //下面是中间的数据
    protected int[] colors = new int[]{0xfff44336, 0xff2196f3, 0xff009688, 0xff8bc34a, 0xffff9800};
    @Override
    protected void onBindItemViewHolder(ViewHolder_2 holder, int section, int position) {
        holder.rendr(String.valueOf(position + 1), colors[section]);
    }
}



下面是主部局进行适配器绑定


public class MainActivity extends AppCompatActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        //创建适配器
        MyAdapter myAdapter = new MyAdapter(this);
        GridLayoutManager grid = new GridLayoutManager(this,2);
        //设置列的宽度
        SectionedSpanSizeLookup se = new SectionedSpanSizeLookup(myAdapter,grid);
        grid.setSpanSizeLookup(se);
        //设置布局管理器
        recyclerView.setLayoutManager(grid);
        //并设置适配器
        recyclerView.setAdapter(myAdapter);

    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值