trying to build a chat bot, but how to alternate the TextView
in RecyclerView
's adapter. See the screenshot below:
As you can see, all the responses are currently aligned to the start, I'd like to move the reply from one person to the other side from the adapter of the RecyclerView
. Here is the code to the adapter:
CustomAdapter.class
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.FeedsViewHolder>
{
DataHolder d1 = new DataHolder();
public class FeedsViewHolder extends RecyclerView.ViewHolder
{
private TextView chat;
private Typeface face;
FeedsViewHolder(View itemView)
{
super(itemView);
chat = (TextView)itemView.findViewById(R.id.chatMessage);
face = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Roboto-Regular.ttf");
chat.setTypeface(face);
}
}
private static class DataHolder
{
List<Text> feeds;
}
CustomAdapter(List<Text> feeds)
{
this.d1.feeds = feeds;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public FeedsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chat_message, viewGroup, false);
FeedsViewHolder pvh = new FeedsViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(FeedsViewHolder feedViewHolder, int i)
{
if(d1.feeds.get(i).isMachineOrHuman())
{
feedViewHolder.chat.setBackgroundResource(R.drawable.user);
feedViewHolder.chat.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
}
else
feedViewHolder.chat.setBackgroundResource(R.drawable.ais);
feedViewHolder.chat.setText(d1.feeds.get(i).getMessage());
}
@Override
public int getItemCount()
{
if(d1.feeds!=null)
{
return d1.feeds.size();
}
else
{
return 0;
}
}
}
chat_message.xml
<TextView android:id="@+id/chatMessage"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />
Now, I made an alternative solution, that is by keeping two TextView
s in chat_message.xml
and changing the visibility settings of the unused TextView
. But what I am looking for is whether is it possible to do it with a single TextView
. Also, the background of the TextView
is a 9-patch-image
, which is assigned as a background resource from the adapter, so when I move the TextView
it shouldn't also spoil the background. Any thoughts would be much appreciated. :)
@Override
public void onBindViewHolder(FeedsViewHolder feedViewHolder, int i)
{
if(d1.feeds.get(i).isMachineOrHuman())
{
feedViewHolder.chat.setBackgroundResource(R.drawable.user);
**feedViewHolder.chat.setGravity(Gravity.RIGHT);** // *Sent Message*
feedViewHolder.chat.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
}
else {
feedViewHolder.chat.setBackgroundResource(R.drawable.ais);
**feedViewHolder.chat.setGravity(Gravity.LEFT);** // *Receive Message*
feedViewHolder.chat.setText(d1.feeds.get(i).getMessage());
}
Create A child layout under the parent layout for The bubble(9-patch) and set the Gravity approrpriately as the TEXT(Message).
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/message_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/message_bubble"
android:longClickable="true"
android:minHeight="40dp">
<TextView
android:id="@+id/message_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Put your message"
android:textColor="@color/black"
android:textColorLink="@color/black"
android:textSize="?attr/TextSizeBody"
android:visibility="visible" />
</LinearLayout>
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.