recyclerview adapter

时间:2016-12-10 09:13:19

标签: java android android-layout

我希望有一个如快照

所示的布局

绿色是to_message_item.xml布局,白色是from_message_item.xml布局

desired behavior

我的Message.class是

public class Message {
    private String message;
    private String sentAt;
    private boolean isSend;

    public Message(String message, String sentAt, boolean isSend){
        this.message = message;
        this.sentAt = sentAt;
        this.isSend = isSend;
    }

    public String getMessage() {
        return message;
    }

    public String getSentAt() {
        return sentAt;
    }
    public boolean getSend() {
        return isSend;
    }
}

MessageHolder.class是

public class MessageHolder extends ViewHolder {

    public ImageView mToImageView;
    public TextView mToMessageTextView, mToSentAtTextView;

    public MessageHolder(View itemView) {
        super(itemView);
        mToImageView = (ImageView) itemView.findViewById(R.id.avatar_iv);
        mToMessageTextView = (TextView) itemView.findViewById(R.id.message_tv);
        mToSentAtTextView = (TextView) itemView.findViewById(R.id.sent_at_tv);
    }
}

MessageAdapter.class是

public class MessageAdapter extends RecyclerView.Adapter<ViewHolder> {

    private List<Message> mMessageList;
    private Context mContext;

    public MessageAdapter(Context mContext, List<Message> mMessageList){
        this.mContext = mContext;
        this.mMessageList = mMessageList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView;
        Message data = mMessageList.get(viewType);
        Log.e("MESSAGE", " Booleans are " + data.getSend());
        if (data.getSend()) {
            rootView = View.inflate(mContext, R.layout.to_message_item, null);
        } else {
            rootView = View.inflate(mContext, R.layout.from_message_item, null);
        }

        return new MessageHolder(rootView);


    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MessageHolder messageHolder = (MessageHolder) holder;
        messageHolder.mToImageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_choose_image));
        messageHolder.mToMessageTextView.setText(mMessageList.get(position).getMessage());
        messageHolder.mToSentAtTextView.setText(mMessageList.get(position).getSentAt());
    }

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

MainActivity.class

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMessageRecyclerView = (RecyclerView) findViewById(R.id.message_rv);
        mMessageAdapter = new MessageAdapter(MainActivity.this, dummyMessages());
        mMessageRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        mMessageRecyclerView.setAdapter(mMessageAdapter);
    }

private List<Message> dummyMessages(){

        Message message1 = new Message("A", "B", true);
        Message message2 = new Message("A", "B", false);
        Message message3 = new Message("A", "B", true);
        Message message4 = new Message("A", "B", false);
        Message message5 = new Message("A", "B", true);
        Message message6 = new Message("A", "B", false);
        Message message7 = new Message("A", "B", true);
        Message message8 = new Message("A", "B", false);

        mMessageList = new CopyOnWriteArrayList<>();
        mMessageList.add(message1);
        mMessageList.add(message2);
        mMessageList.add(message3);
        mMessageList.add(message4);
        mMessageList.add(message5);
        mMessageList.add(message6);
        mMessageList.add(message7);
        mMessageList.add(message8);

        return mMessageList;
    }

使用上面的代码将输出作为

weird behavior

更新

在Adapter类中进行的更改

 @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView;
        Message data = mMessageList.get(viewType);
        Log.e("MESSAGE", " Booleans are " + data.getSend());
        if (data.getSend()) {
            rootView = View.inflate(mContext, R.layout.to_message_item, null);
        } else {
            rootView = View.inflate(mContext, R.layout.from_message_item, null);
        }
        return new MessageHolder(rootView);
    }

    @Override
    public int getItemViewType(int position) {
        if (mMessageList.get(position).getSend()){
            return 0;
        } else {
            return 1;
        }

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MessageHolder messageHolder = (MessageHolder) holder;
        messageHolder.mToImageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_choose_image));
        messageHolder.mToMessageTextView.setText(mMessageList.get(position).getMessage());
        messageHolder.mToSentAtTextView.setText(mMessageList.get(position).getSentAt());
    }

0 个答案:

没有答案