我想问一下如何使用recyclerview实现多种布局,数据是获取本地的json数据,每个json数据的长度是不同的
json数据格式:

adapter:
package adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.testindexfunction.R;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
import json.IndexFunctionJsonBean;
import viewholder.IndexMessageViewHolder;
import viewholder.IndexTitleViewHolder;
public class IndexAdapter extends RecyclerView.Adapter {
public static final int FUNCTION_TITLE=0;
public static final int FUNCTION_MESSAGE=1;
private LayoutInflater layoutInflater;
private Context context;
private List<IndexFunctionJsonBean.IndexBean> indexBeanLists=new ArrayList<>();
public IndexAdapter(Context context,List<IndexFunctionJsonBean.IndexBean> indexBeanLists){
layoutInflater=LayoutInflater.from(context);
this.context=context;
this.indexBeanLists=indexBeanLists;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if(viewType == FUNCTION_TITLE){
return new IndexTitleViewHolder(layoutInflater.inflate(R.layout.index_function_titel,parent,false));
}else if(viewType == FUNCTION_MESSAGE){
return new IndexMessageViewHolder(layoutInflater.inflate(R.layout.index_function_message,parent,false));
}
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
IndexFunctionJsonBean.IndexBean indexBean = indexBeanLists.get(position);//得到index里面4个对象的值
List<IndexFunctionJsonBean.IndexBean.FunctionBean> indexFunction = indexBean.getFunction();//得到function里面的内容
int viewType = getItemViewType(position);
switch(viewType){
case FUNCTION_TITLE:
IndexTitleViewHolder indexTitleViewHolder= (IndexTitleViewHolder) holder;
for(int i=0;i<indexBeanLists.size();i++){
indexTitleViewHolder.functionTitle.setText(indexBean.getTitle());
}
break;
case FUNCTION_MESSAGE:
IndexMessageViewHolder indexMessageViewHolder=(IndexMessageViewHolder)holder;
for (int i = 0; i <indexFunction.size(); i++) {
indexMessageViewHolder.functionName.setText(indexFunction.get(i).getName());
Picasso.with(context).load(indexFunction.get(i).getImgurl()).into(indexMessageViewHolder.functionImage);
}
break;
}
}
@Override
public int getItemCount() {
return indexBeanLists.size();
}
@Override
public int getItemViewType(int position) {
if(position == 0){
return FUNCTION_TITLE;
}else if(position == 1){
return FUNCTION_MESSAGE;
}
return 0;
}
}
与其说,多布局,还不如先给我看看,你的需要的效果图。
数据的结构,可以根据效果图,自己重新组装一次。
为了我们更好的实现出效果。不一定要使用原来的结构。
也不是上来,就是多布局。而是要看你需要什么效果。
https://www.sunofbeach.net/a/1298898423868559360