昨天写过一个RecyclerView的简单用法,今天给大家写一下RecyclerView的多布局用法
首先在我们的数据源中,定义一个int类型或其他类型的参数,用于判断布局类型
我这里简单写一个Bean类 用于存储数据
public class Bean {
String id;
String title;
int i;
public Bean(String id, String title,int i) {
this.id = id;
this.title = title;
this.i=i;
}
}
然后 在适配器中重写三个昨天的方法,再重写一个getItemViewType方法 该方法的返回值是onCreateViewHolder方法的int类型参数
具体代码如下:
public class RVAdapter_Duo extends RecyclerView.Adapter<RVAdapter_Duo.RVHolder> {
Context context;
ArrayList<Bean> datas;
public RVAdapter_Duo(Context context, ArrayList<Bean> datas) {
this.context = context;
this.datas = datas;
}
@NonNull
@Override
public RVHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
Log.d("###", "onCreateViewHolder: "+datas.get(i).title+"---"+datas.get(i).i);
if(datas.get(i).i%2==0){ //通过getItemViewType返回的参数进行判断 分别给布局
Log.d("###", "onCreateViewHolder: "+1);
View view= LayoutInflater.from(context).inflate(R.layout.item2,viewGroup,false);
return new RVHolder(view);
}else {
Log.d("###", "onCreateViewHolder: "+2);
View view= LayoutInflater.from(context).inflate(R.layout.item,viewGroup,false);
return new RVHolder(view);
}
}
@Override
public int getItemViewType(int position) {
//可以在这个方法中进行判断 也可以像我写的这样返回给onCreateViewHolder中判断
return position;
}
@Override
public void onBindViewHolder(@NonNull RVHolder rvHolder, final int i) {
rvHolder.textView.setText(datas.get(i).title);
Glide.with(context).load(datas.get(i).id).into(rvHolder.imageView);
}
@Override
public int getItemCount() {
return datas.size();
}
class RVHolder extends RecyclerView.ViewHolder{
private TextView textView;
private ImageView imageView;
public RVHolder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.wenziyi);
textView=itemView.findViewById(R.id.wenzier);
}
}
}
Activity中代码不变,不会的朋友可以查找我前一天博客
效果:
首先封装两个类 :
public class BaseRVHolder extends RecyclerView.ViewHolder {
SparseArray<View> view=new SparseArray<>();
public BaseRVHolder(@NonNull View itemView) {
super(itemView);
}
public View getView(int id){
View subview = view.get(id);
if(subview==null){
subview=itemView.findViewById(id);
view.put(id,subview);
}
return subview;
}
}
public abstract class BaseRVAdapter<T> extends RecyclerView.Adapter<BaseRVHolder> {
ArrayList<T> datas= new ArrayList<>();
Context context;
public BaseRVAdapter(ArrayList<T> datas, Context context) {
this.datas = datas;
this.context = context;
}
public abstract int getLayoutId();
public abstract void convertView(BaseRVHolder baseRVHolder,int i);
@NonNull
@Override
public BaseRVHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = LayoutInflater.from(context).inflate(getLayoutId(), viewGroup, false);
return new BaseRVHolder(inflate);
}
@Override
public void onBindViewHolder(@NonNull BaseRVHolder baseRVHolder, int i) {
convertView(baseRVHolder,i);
}
@Override
public int getItemCount() {
return datas.size();
}
然后重写一个类 继承BaseRVHolder 类
public class RVAdapter<Bean> extends BaseRVAdapter {
ArrayList<com.example.work6_9.Bean> datas;
Context context;
public RVAdapter(ArrayList datas, Context context) {
super(datas, context);
this.context=context;
this.datas=datas;
}
@Override
public int getLayoutId() {
return R.layout.item;
}
@Override
public void convertView(BaseRVHolder baseRVHolder, int i) {
ImageView imageView= (ImageView) baseRVHolder.getView(R.id.wenziyi);
Glide.with(context).load(datas.get(i).id).into(imageView);
TextView textView = (TextView) baseRVHolder.getView(R.id.wenzier);
textView.setText(datas.get(i).title);
}
}
导依赖 :
implementation ‘com.android.support:recyclerview-v7:28.0.0’
api(‘com.jcodecraeer:xrecyclerview:1.5.9’) {
exclude group: ‘com.android.support’
}
写布局文件
通过setLoadingListener方法监听下拉刷新上拉加载事件
其他用法与RecyclerView一样
public class MainActivity extends AppCompatActivity {
XRecyclerView recyclerView;
ArrayList<Bean> list;
RVAdapter adapter;
RVAdapter_Duo adapter2;
int flag=1;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.d("##","刷新");
recyclerView.refreshComplete();
adapter2.notifyDataSetChanged(); //刷新适配器
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=findViewById(R.id.recycler); //初始化布局
final LinearLayoutManager manager=new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager); //设置布局
DividerItemDecoration dividerItemDecoration=new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
dividerItemDecoration.setDrawable(getDrawable(R.drawable.divider)); //设置分割线
recyclerView.addItemDecoration(dividerItemDecoration);
list=new ArrayList<>();
myThread();
// adapter=new RVAdapter(list,this);
adapter2=new RVAdapter_Duo(this,list);
recyclerView.setAdapter(adapter2); //设置适配器
recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
flag=1;
myThread();
}
@Override
public void onLoadMore() {
flag++;
myThread();
}
});
}
public void myThread(){
new Thread(new Runnable() { //下载数据
@Override
public void run() {
String gson = HttpLoad.getGson("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page="+flag);
try {
JSONObject jsonObject=new JSONObject(gson);
JSONArray data = jsonObject.getJSONArray("data");
for(int i = 0 ; i < data.length();i++){
JSONObject jsonObject1 = data.getJSONObject(i);
String id = jsonObject1.getString("pic");
String title = jsonObject1.getString("title");
Log.d("##",title);
Bean bean = new Bean(id, title,i);
list.add(bean);
}
Message obtain = Message.obtain();
handler.sendMessage(obtain); //发送线程
} catch (JSONException e) {
e.printStackTrace();
}
}
}).start();
}
}