我一直在寻找教程一段时间,但我找不到任何有用的东西,以扩大两个布局,一个聊天泡泡在" SENDER"并且另一个接受"收到"
我尝试下面的代码,但它给了我一个nullPointerException。
public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
private List<Chat> mDataSet;
private String mId;
private static final int CHAT_RIGHT = 1;
private static final int CHAT_LEFT = 2;
/**
* Inner Class for a recycler view
*/
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) itemView.findViewById(R.id.tvMessage);
}
}
/**
* Called when a view has been clicked.
*
* @param dataSet Message list
* @param id Device id
*/
public ChatAdapter(List<Chat> dataSet, String id) {
mDataSet = dataSet;
mId = id;
}
@Override
public ChatAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
if (viewType == CHAT_RIGHT) {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_chat_right, parent, false);
} else {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_chat_left, parent, false);
}
return new ViewHolder(v);
}
@Override
public int getItemViewType(int position) {
if (mDataSet.get(position).getId().equals(mId))
return CHAT_RIGHT;
return CHAT_LEFT;
}
@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();
}
}
我也试过这个来扩充RecyclerView,但它也不起作用:
mFirebaseRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot != null && dataSnapshot.getValue() != null) {
try{
Chat model = dataSnapshot.getValue(Chat.class);
mChats.add(model);
mRecyclerView.scrollToPosition(mChats.size() - 1);
mAdapter.notifyItemInserted(mChats.size() - 1);
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
以下代码也不起作用:
if(key.equals(id)){
FirebaseRecyclerAdaper.inflate(LISTRIGHT)
}else{
FirebaseRecyclerAdaper.inflate(LISTRIGHT)
bla bla bla....
}
它只是膨胀相同的列表。我不知道该怎么做。