Im trying to create a small conversational UI, but my Recycler View adapter doesnt seem to be working, or its not getting called properly
(I tested this by LOGGING when the onCreateViewHolder
is called but it did not log anything).
The chat_layout is the only thing i see, the recycle view is completely emtpy. What am i doing wrong?
public class chatBot extends Fragment {
View myView;
public static String access_token;
private List<populate_messages> listItems;
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private SwipeRefreshLayout mSwipeRefreshLayout;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.chat_layout, container, false);
getActivity().setTitle("Home Chat");
SharedPreferences prefData = getActivity().getApplicationContext().getSharedPreferences("app-data", MODE_PRIVATE);
if(prefData.contains("access_token")){
access_token=prefData.getString("access_token", null);
}else{
access_token="null";
}
Log.v("CHATBOT_access", access_token);
listItems = new ArrayList<>();
recyclerView = (RecyclerView) myView.findViewById(R.id.reyclerview_message_list);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
prepareData();
adapter = new populateMessages_adapter(listItems, myView.getContext());
recyclerView.setAdapter(adapter);
return myView;
}
private void prepareData(){
populate_messages populate = new populate_messages("Hello, how are you","15:30","username","sent");
listItems.add(populate);
populate = new populate_messages("I am fine","15:30","bot","received");
listItems.add(populate);
}}
public class populate_messages {
private String messageText;
private String time;
private String sender;
private String type;
public populate_messages(String messageText, String time, String sender, String type){
this.messageText = messageText;
this.time = time;
this.sender=sender;
this.type = type;
}
public String getMessageText() {
return messageText;
}
public String getTime() {
return time;
}
public String getSender() {
return sender;
}
public String getType() {
return type;
}
}
public class populateMessages_adapter extends RecyclerView.Adapter<populateMessages_adapter.ViewHolder> {
private List<populate_messages> listItems;
private Context context;
public String imageType;
private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
public populateMessages_adapter(List<populate_messages> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.v("are you created ", "yes");
View view;
if (viewType == VIEW_TYPE_MESSAGE_SENT) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.message_sent, parent, false);
return new ViewHolder(view);
} else {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.message_received, parent, false);
return new ViewHolder(view);
}
}
@Override
public int getItemViewType(int position) {
populate_messages message = listItems.get(position);
if (message.getType().equalsIgnoreCase("sent")) {
return VIEW_TYPE_MESSAGE_SENT;
} else {
return VIEW_TYPE_MESSAGE_RECEIVED;
}
}
@Override
public void onBindViewHolder(populateMessages_adapter.ViewHolder holder, int position) {
populate_messages listItem = listItems.get(position);
Log.v("MESSAGES ", listItem.getMessageText());
Log.v("MESSAGES ",listItem.getTime());
Log.v("MESSAGES ", listItem.getSender());
holder.messageText.setText(listItem.getMessageText());
holder.timeText.setText(listItem.getTime());
holder.nameText.setText(listItem.getSender());
}
@Override
public int getItemCount() {
return listItems.size();
}
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null;
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView messageText, timeText, nameText;
// ImageView profileImage;
public ViewHolder(final View itemView) {
super(itemView);
messageText = (TextView) itemView.findViewById(R.id.text_message_body);
timeText = (TextView) itemView.findViewById(R.id.text_message_time);
nameText = (TextView) itemView.findViewById(R.id.text_message_name);
// profileImage = (ImageView) itemView.findViewById(R.id.image_message_profile);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/reyclerview_message_list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
</android.support.v7.widget.RecyclerView>
<View
android:layout_width="0dp"
android:layout_height="2dp"
android:background="#dfdfdf"
android:layout_marginBottom="0dp"
app:layout_constraintBottom_toTopOf="@+id/layout_chatbox"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<LinearLayout
android:id="@+id/layout_chatbox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:minHeight="48dp"
android:background="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<EditText
android:id="@+id/edittext_chatbox"
android:hint="Enter message"
android:background="@android:color/transparent"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:maxLines="6"/>
<Button
android:id="@+id/button_chatbox_send"
android:text="SEND"
android:textSize="14dp"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:layout_width="64dp"
android:layout_height="48dp"
android:gravity="center"
android:layout_gravity="bottom" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp">
<ImageView
android:id="@+id/image_message_profile"
android:layout_width="32dp"
android:layout_height="32dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:id="@+id/text_message_name"
android:text="John Doe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
app:layout_constraintLeft_toRightOf="@+id/image_message_profile"
android:layout_marginLeft="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="4dp" />
<TextView
android:id="@+id/text_message_body"
android:text="hi man, how are you?"
android:background="@drawable/chat_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="240dp"
android:padding="8dp"
android:textColor="#ffffff"
android:layout_marginTop="4dp"
app:layout_constraintTop_toBottomOf="@+id/text_message_name"
app:layout_constraintLeft_toRightOf="@+id/image_message_profile"
android:layout_marginLeft="8dp" />
<TextView
android:id="@+id/text_message_time"
android:text="11:40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
app:layout_constraintLeft_toRightOf="@+id/text_message_body"
android:layout_marginLeft="4dp"
app:layout_constraintBottom_toBottomOf="@+id/text_message_body" />
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp">
<TextView
android:id="@+id/text_message_body"
android:text="hello, hello!"
android:background="@drawable/chat_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="240dp"
android:padding="8dp"
android:textColor="#ffffff"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_message_time"
android:text="11:40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:layout_marginRight="4dp"
app:layout_constraintBottom_toBottomOf="@+id/text_message_body"
app:layout_constraintRight_toLeftOf="@+id/text_message_body" />
</android.support.constraint.ConstraintLayout>
PS : Sorry for the really long code.
答案 0 :(得分:1)
RecyclerView
height is 0,update the code chat_layout.xml
with below code
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/reyclerview_message_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_marginBottom="0dp"
android:background="#dfdfdf"
app:layout_constraintBottom_toTopOf="@+id/layout_chatbox"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<LinearLayout
android:id="@+id/layout_chatbox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#ffffff"
android:minHeight="48dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<EditText
android:id="@+id/edittext_chatbox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="@android:color/transparent"
android:hint="Enter message"
android:maxLines="6" />
<Button
android:id="@+id/button_chatbox_send"
android:layout_width="64dp"
android:layout_height="48dp"
android:layout_gravity="bottom"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:gravity="center"
android:text="SEND"
android:textSize="14dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
OR second way if you don't want RecyclerView
to match_parent
set ConstraintLayout
properly
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/view"
android:layout_width="384dp"
android:layout_height="2dp"
android:background="#dfdfdf"
app:layout_constraintBottom_toTopOf="@+id/layout_chatbox"
tools:layout_editor_absoluteY="517dp" />
<LinearLayout
android:id="@+id/layout_chatbox"
android:layout_width="384dp"
android:layout_height="wrap_content"
android:background="#ffffff"
android:minHeight="48dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteY="519dp">
<EditText
android:id="@+id/edittext_chatbox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="@android:color/transparent"
android:hint="Enter message"
android:maxLines="6" />
<Button
android:id="@+id/button_chatbox_send"
android:layout_width="64dp"
android:layout_height="48dp"
android:layout_gravity="bottom"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:gravity="center"
android:text="SEND"
android:textSize="14dp" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/reyclerview_message_list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>