简体   繁体   中英

may i inflate two list layout in one RecycleView Adapter?

what I am trying to do? I have one SQLite data table and from that data table, I want to show a different column to the different activities. for that, I use recycle view and for that recycle view I create two model list view layout. then create one RecylerView Adapter. In onCreateViewHolder method, I want to inflate the two model list view layout. here is the code what I mean

you can try following recyclerview multipleview type you host two layouts in one adapter class

recyclerview multipleviewtype

Yes, you can do that.

thanks to the getItemViewType method you can return different values for view type that can be caught at onCreateViewHolder and depend on the value you can inflate different layout.

I was enabled to create one adapter for chat messages whether is sent or received text/image I inflate different layouts depend on view typeviewType

code for specified viewType value

@Override
public int getItemViewType(int position) {
    /**
     * check the user id to detect if a message sent or received
     * then check if a message is a text or image
     */

    String userID = Constant.itemUser.getId();
    MessageModel message = messages.get(position);

    if (userID.equals(message.getUserId())) {

        if (message.getType().equals("1")) {
            return MessageType.SENT_TEXT;
        } else if (message.getType().equals("2")) {
            return MessageType.SENT_IMAGE;
        }

    } else {

        if (message.getType().equals("1")) {
            return MessageType.RECEIVED_TEXT;
        } else if (message.getType().equals("2")) {
            return MessageType.RECEIVED_IMAGE;
        }

    }
    return super.getItemViewType(position);
}

and in a method named onCreateViewHolder

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    /**
     * check the type of view and return holder
     */
    if (viewType == MessageType.SENT_TEXT) {
        return new SentTextHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.row_sent_message_text, parent, false));
    } else if (viewType == MessageType.SENT_IMAGE) {
        return new SentImageHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.row_sent_message_img, parent, false));
    } else if (viewType == MessageType.RECEIVED_TEXT) {
        return new ReceivedTextHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.row_received_message_text, parent, false));
    } else if (viewType == MessageType.RECEIVED_IMAGE) {
        return new ReceivedImageHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.row_received_message_img, parent, false));
    }

    return null;
}

please don't forget to handle holders in onBindViewHolder

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder mHolder, int position) {

    int type = getItemViewType(position);
    MessageModel message = messages.get(position);
    /**
     * check message type and init holder to user it and set data in the right place for every view
     */
    if (type == MessageType.SENT_TEXT) {
       // case of message sent of type text
    } else if (type == MessageType.SENT_IMAGE) {
       // case of message sent of type img
    } else if (type == MessageType.RECEIVED_TEXT) {
        ReceivedTextHolder holder = (ReceivedTextHolder) mHolder;
        // case of the message received of type text

    } else if (type == MessageType.RECEIVED_IMAGE) {
        // etc
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM