简体   繁体   中英

How can I use two layouts when using Android FirebaseListAdapter?

I am making an Android chat app that uses Cloud Firebase. In populating the listview with messages, I want to use two layouts, one for the incoming messages, and one for the going out messages. When following a tutorial that did something like this,

( http://www.devexchanges.info/2016/12/simple-chat-application-using-firebase.html )

they make their own messageAdapter class which introduces a getView function that will return one of the two views depending on the message. in the constructor for this class though, the super() statement is not working for me. here is the constructor:

public MessageAdapter(Lobby activity, Class<ChatMessage> modelClass, int modelLayout, DatabaseReference ref) {

        super(activity, modelClass, modelLayout, ref);
        this.activity = activity;
    }

this is exactly like the tutorial uses it, but for some reason in the context of my own application it is not working. Is there something I have to do with the FirebaseListAdapter class to allow this action? Any help would be appreciated, if you need to see any of the other code in my project I'll be happy to edit to include it. Thanks.

Try using a RecyclerView and RecyclerView.Adapter instead of a FirebaseListAdapter, like this:

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
    private static final int CHAT_END = 1;
    private static final int CHAT_START = 2;

    private List<Chat> mDataSet;
    private String mId;

    /**
     * Called when a view has been clicked.
     *
     * @param dataSet Message list
     * @param id      Device id
     */
    ChatAdapter(List<Chat> dataSet, String id) {
        mDataSet = dataSet;
        mId = id;
    }

    @Override
    public ChatAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v;

        if (viewType == CHAT_END) {
            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_chat_end, parent, false);
        } else {
            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_chat_start, parent, false);
        }

        return new ViewHolder(v);
    }

    @Override
    public int getItemViewType(int position) {
        if (mDataSet.get(position).getId().equals(mId)) {
            return CHAT_END;
        }

        return CHAT_START;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Chat chat = mDataSet.get(position);
        holder.mTextView.setText(chat.getMessage());
    }

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

    /**
     * Inner Class for a recycler view
     */
    class ViewHolder extends RecyclerView.ViewHolder {
        TextView mTextView;

        ViewHolder(View v) {
            super(v);
            mTextView = (TextView) itemView.findViewById(R.id.tvMessage);
        }
    }
}

Please see more at https://github.com/thaleslima/firebase-chat-sample .

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