我的活动中有一个RecyclerView
。它的方向是“垂直”。RecyclerView项有两个不同的ViewTypes
。当API响应为JSONArray
时,我需要在RecyclerView中使用CardViews
和LayoutManager作为Horizontal设置条目。
这是我的EntryType的代码
public abstract class ChatObject {
public static final int INPUT_OBJECT = 0;
public static final int RESPONSE_OBJECT = 1;
public static final int FOODIE_CARDS = 2;
private String text;
private String name, phone, city;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@NonNull
public String getText() {
return text;
}
public void setText(@NonNull String text) {
this.text = text;
}
public abstract int getType();
}
这是我的BaseViewHolder的代码
public abstract class BaseViewHolder extends RecyclerView.ViewHolder {
public BaseViewHolder(View itemView) {
super(itemView);
}
public abstract void OnBindView(ChatObject object);
这是ChatAdapter的代码
public class ChatAdapter extends RecyclerView.Adapter<BaseViewHolder> {
private ArrayList<ChatObject> chatObjects;
public ChatAdapter(ArrayList<ChatObject> chatObjects){
this.chatObjects=chatObjects;
}
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater=LayoutInflater.from(parent.getContext());
View itemView;
switch (viewType){
case ChatObject.INPUT_OBJECT:
itemView=inflater.inflate(R.layout.input,parent,false);
return new ChatInputView(itemView);
case ChatObject.RESPONSE_OBJECT:
itemView=inflater.inflate(R.layout.response_text,parent,false);
return new ChatOutputView(itemView);
case ChatObject.FOODIE_OBJECT:
itemView=inflater.inflate(R.layout.foodie_detail,parent,false);
return new FoodieView(itemView);
default:
itemView=inflater.inflate(R.layout.response_text,parent,false);
return new ChatOutputView(itemView);
}
}
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
holder.OnBindView(chatObjects.get(position));
}
@Override
public int getItemViewType(int position) {
return chatObjects.get(position).getType();
}
@Override
public int getItemCount() {
return chatObjects.size();
}
}
这是MainActivity的代码
RecyclerView recyclerView;
EditText input;
ChatAdapter adapter;
ChatPresenter chatPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=(RecyclerView)findViewById(R.id.rv_chat);
input=(EditText)findViewById(R.id.et_search_box);
input.setOnEditorActionListener(searchListener);
this.chatPresenter=new ChatPresenter();
chatPresenter.attachView(this);
this.adapter=new ChatAdapter(chatPresenter.getChatObjects());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
}
那么如何将LayoutManager设置为不同ViewType的水平?
答案 0 :(得分:0)
我认为可以通过重写适配器中的getItem(int position)方法并在活动/片段中调用回调来实现。这将设置不同的布局管理器。
答案 1 :(得分:-1)
mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,true));
使用此工具可以使recyclerview具有水平视图。