如何在RecyclerView的适配器中替换Android中TextView的位置?

时间:2016-10-07 14:01:38

标签: android android-layout android-recyclerview

尝试构建聊天机器人,但如何在TextView的适配器中替换RecyclerView。请参见下面的屏幕截图:

enter image description here

正如您所看到的,所有响应当前都与开头一致,我想将回复从一个人移动到RecyclerView的适配器。以下是适配器的代码:

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" />

现在,我提出了另一种解决方案,即在TextView中保留两个chat_message.xml并更改未使用的TextView的可见性设置。但我正在寻找的是,是否可以使用单个TextView进行此操作。此外,TextView的背景是9-patch-image,它被指定为来自适配器的背景资源,因此当我移动TextView时,它不应该破坏背景。任何想法将不胜感激。 :)

2 个答案:

答案 0 :(得分:1)

with RowCo as
(
select ATID, count(*) as row_count
from tbl2
group by ATID
having row_count <= 100
)

SELECT tbl1.ATID, tbl1.ATDesc, tbl2.AValue, tbl2.ADesc, tbl2.APosNeg
FROM tbl1 
inner join RowCo
    on RowCo.ATID = tbl1.ATID
LEFT JOIN tbl2 
    ON tb1.ATID = tbl2.ATID

气泡(9-patch)的父布局下创建子布局,并将 Gravity 设置为TEXT(消息)。

答案 1 :(得分:0)

<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>

enter image description here