不调用RecyclerView onCreateViewHolder

时间:2018-05-23 18:43:16

标签: android android-recyclerview android-adapter

RecyclerView setAdapter但是调用方法ChatAdapter,registerAdapterDataObserver,onAttachedToRecyclerView,getItemCount 5调用

xml layout_heightmatch_parent

getItemCount 5不是null,但onCreateViewHolder未被调用。

MyActivity.java:

List<ChatMessage> messages = new ArrayList<ChatMessage>();
LinearLayoutManager layoutManager =  new LinearLayoutManager(getApplicationContext());

reyclerview_message_list.setAdapter(new ChatAdapter(messages, R.layout.chat_send));
reyclerview_message_list.setLayoutManager(new LinearLayoutManager(this));
reyclerview_message_list.setItemAnimator(new DefaultItemAnimator());
reyclerview_message_list.addItemDecoration(new DividerItemDecoration(getApplicationContext(), layoutManager.getOrientation()));

Adapter.java:

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {

private List<ChatMessage> itemList;
private int itemLayout;


public ChatAdapter(List<ChatMessage> items, int itemLayout){
    this.itemList = items;
    this.itemLayout = itemLayout;

}


@Override
public ChatAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View view  = LayoutInflater.from(viewGroup.getContext()).inflate(itemLayout, viewGroup, false);
    return new ChatAdapter.ViewHolder(view);
}

@Override
public void onBindViewHolder(ChatAdapter.ViewHolder viewholder, int position) {

    ChatMessage item  = itemList.get(position);
    viewholder.messageBody.setText(item.getMessageBody());
    viewholder.messageTime.setText(item.getMessageTime());
    viewholder.messageState.setText(item.getMessageState());
    viewholder.itemView.setTag(item);
}



public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView messageBody, messageTime, messageState;

    public ViewHolder(View itemView) {
        super(itemView);

        messageBody = (TextView)itemView.findViewById(R.id.text_message_body);
        messageTime= (TextView)itemView.findViewById(R.id.text_message_time);
        messageState= (TextView)itemView.findViewById(R.id.text_message_state);
    }
}

@Override
public int getItemCount() {
    return itemList.size();
}

为什么onCreateViewHolder没有被调用?

2 个答案:

答案 0 :(得分:0)

我只是更新您的适配器代码及其运行良好。

    public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {

    private List<ChatMessage> itemList;
    private int itemLayout;
    private Context mContext;

    public ChatAdapter(List<ChatMessage> items, int itemLayout, Context context){
        this.itemList = items;
        this.itemLayout = itemLayout;
        this.mContext = context;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view  = LayoutInflater.from(mContext).inflate(itemLayout, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewholder, int position) {

        ChatMessage item  = itemList.get(position);
        viewholder.messageBody.setText(item.getMessageBody());
        viewholder.messageTime.setText(item.getMessageTime());
        viewholder.messageState.setText(item.getMessageState());
        viewholder.itemView.setTag(item);
    }



    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView messageBody, messageTime, messageState;

        public ViewHolder(View itemView) {
            super(itemView);

            messageBody = (TextView)itemView.findViewById(R.id.text_message_body);
            messageTime= (TextView)itemView.findViewById(R.id.text_message_time);
            messageState= (TextView)itemView.findViewById(R.id.text_message_state);
        }
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }
}

你的Activity或Fragment首先得到RecyclerView,然后像这样设置适配器:

 RecyclerView reyclerview_message_list = findViewById(R.id.recyclerView);

    reyclerview_message_list.setAdapter(new ChatAdapter(messages, R.layout.list_row, this)); // passed another parameter Context
    reyclerview_message_list.setLayoutManager(new LinearLayoutManager(this));
    reyclerview_message_list.setItemAnimator(new DefaultItemAnimator());
    reyclerview_message_list.addItemDecoration(new DividerItemDecoration(getApplicationContext(), layoutManager.getOrientation()));

希望这能解决您的问题。

答案 1 :(得分:0)

设置值后,您没有通知适配器。设置适配器messages时为空。使用messagesaddAll(message_objects)中添加值时,ArrayList中会添加这些数据,但您必须使用notifyDataSetChanged()通知适配器。在类级别声明适配器,然后在recyclerview中设置适配器之前创建适配器对象,并在ArrayList中添加对象后通知适配器。

private ChatAdapter chatAdapter;//declare it globally(class level)

chatAdapter = new ChatAdapter(messages, R.layout.chat_send);
reyclerview_message_list.setAdapter(chatAdapter);
reyclerview_message_list.setLayoutManager(new LinearLayoutManager(this));
reyclerview_message_list.setItemAnimator(new DefaultItemAnimator());
reyclerview_message_list.addItemDecoration(new DividerItemDecoration(getApplicationContext(), layoutManager.getOrientation()));
messages.addAll(ChatMessage_Objects_you_getting_from_web_service);
chatAdapter.notifyDataSetChanged();