I have a chat screen where i can chat with other user, i am sending chat data (message,time and sender via List) to RecyclerAdapter which populate chat views with data.Now i have one more List which has data with different layout. Like this
Here is my method from where i am calling second arraylist into RecyclerAdapter
public void TransferResultTo_Activity(List<Image_data_Wrapper> list) {
Log.d(TAG,"Here is data Result From AsyncTask "+list.size());
getResult=list;
Image_data_Wrapper Image=getResult.get(0);
Log.d(TAG,"Result from Image data "+Image.getPage_Title());
adapter=new Chat_Adapter(this,message,getResult);
adapter.notifyDataSetChanged();
}
As in above method when i try to call adapter by recyclerView.setAdapter(adapter);
it remove all messages from screen and shows my image data. but i want to keep all messages and show image with data via new list
Here is my Chat Adapter
public class Chat_Adapter extends RecyclerView.Adapter<Chat_Adapter.ViewHolder> {
private String UserID;
private Context context;
//TAG FOR TRACKING SELF MESSAGE
private int Self_Msg=0;
//ARRAYLIST OF MESSAGES OBJECT CONTAINING ALL THE MESSAGES IN THE THREAD
private List<Chat_Wrapper> arrayList_message;
public static final String TAG="###CHAT_ADAPTER###";
private List<Image_data_Wrapper> data_Result;
boolean valtype;
public Chat_Adapter(Context context, List<Chat_Wrapper> message) {
//UserID = userID;
this.context = context;
this.arrayList_message = message;
Log.d(TAG,"Chat Adapter Calling");
}
public Chat_Adapter(Context context, List<Image_data_Wrapper> result,boolean val) {
this.context = context;
this.data_Result = result;
this.valtype=val;
Log.d(TAG,"Image data Chat Adapter Calling");
}
public Chat_Adapter() {
}
@Override
public Chat_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
Log.d(TAG,"On Create View Holder Calling ");
if (viewType==Self_Msg){
itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_screen_message_item,parent,false);
Log.d(TAG,"On Create View Holder Calling View Type is "+itemView);
}
else if (valtype==true){
itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.Image_data_layout,parent,false);
Log.d(TAG,"ON CREATE VIEW HOLDER RUNNING AND data RESULT VIEW TYPE IS RUNNING "+viewType);
}
else {
itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_screen_message_item,parent,false);
Log.d(TAG,"On Create View Holder Calling View Type is "+itemView);
}
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(Chat_Adapter.ViewHolder holder, int position) {
if (valtype==true){
Image_data_Wrapper wrapper=data_Result.get(position);
holder.data_title.setText(wrapper.getPage_Title());
holder.data_link.setText(wrapper.getPage_Link());
holder.data_snippet.setText(wrapper.getPage_Desc());
Picasso.with(context).load(wrapper.getPage_ImageThumb()).into(holder.Image_Image);
valtype=false;
}
else {
Log.d(TAG,"On Bind VIew Holder Context "+context);
Chat_Wrapper wrapper=arrayList_message.get(position);
Log.d(TAG,"On Bind VIew Holder Chat Wrapper "+wrapper);
holder.Message.setText(wrapper.getMessage());
holder.TimeStamp.setText(wrapper.getTimestamp());
}
}
@Override
public int getItemCount() {
Log.d(TAG,"Get Item Count Running");
int items;
if (valtype==true){
Log.d(TAG,"data Result Array is not empty");
items=data_Result.size();
Log.d(TAG,"Total Number Of Item "+items);
return items;
}
else {
Log.d(TAG,"ARRAY SIZE AT GETITEM COUNT "+arrayList_message.size());
return arrayList_message.size();
}
}
@Override
public int getItemViewType(int position) {
Log.d(TAG,"Get Item View Type Running");
return position;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView Message,TimeStamp,data_title,data_link,data_snippet;
ImageView User_image,Image_Image;
ImageButton data_SendButton;
public ViewHolder(View itemView) {
super(itemView);
Message= (TextView) itemView.findViewById(R.id.Single_Item_Chat_Message);
TimeStamp= (TextView) itemView.findViewById(R.id.Single_Item_Chat_TimeStamp);
User_image= (ImageView) itemView.findViewById(R.id.Single_Item_Chat_ImageView);
if (valtype==true){
Log.d(TAG,"Setting View For Image data ");
data_title= (TextView) itemView.findViewById(R.id.Image_data_Title);
data_link= (TextView) itemView.findViewById(R.id.Image_data_Link);
data_snippet= (TextView) itemView.findViewById(R.id.Image_data_Snippet);
Image_Image= (ImageView) itemView.findViewById(R.id.Image_data_Image);
data_SendButton= (ImageButton) itemView.findViewById(R.id.Image_data_SendButton);
}
}
}
public void updateDataWithdataResult(List<Image_data_Wrapper> list,boolean setValue){
Log.d(TAG,"Update Data Method Calling");
this.data_Result=list;
this.valtype=setValue;
}
}
The issues i am facing with this code
- With notifiydatasetchanged only Adapter constructor is calling(Correct one) but never getItemCount()
- Is sending boolean value with data is right way to check, If calls from ImageArrayList only then enter in its related data or views?
- What is the best way to get both List into single RecyclerView with their own layout
UPDATE : When i call adapter by recyclerView.setAdapter(adapter)
it worked as i want but adapter.notifyDataSetChanged
make call stuck at adapter constructor (Thanks to DanielLaneDC). So can i leave it like that or adapter.notifyDataSetChanged
should work?
It sounds like you want to show two different lists of items in the same RecyclerView using the same RecyclerView.Adapter. Thankfully, this is the kind of thing RecyclerView.Adapter can handle really nicely.
Here's how your RecyclerView.Adapter should look:
Here's what your
Activity
is going to need:When you want to update the data, just call
addMessage(ChatWrapper)
orremoveMessage(ChatWrapper)
for messages andaddImage(ImageDataWrapper)
orremoveImage(ImageDataWrapper)
for images.Some key points:
valType
variable,getItemCount()
should always return the size of both lists combined, etc.)I had your same problem. You can create a Pojo class if you need and enter the association for example a chat can have multiple images. Then build the chat object and insert a list of images in it. Before passing the list to the adapter, populate it with the original data. I know this may not be a nice solution for your needs, but remember that you can have full control, without segmenting the lists.
}
First you need to combine both array in single arraylist like.. firsArrlist.addAll(secondArrList);
and pass into recyclerview adapter and migrate with some flag and take another viewholder for particular flag to set that view in recyclerview.
And total count is must both firstArrlist.size + secondArrList.size