最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android RecycleView和線型布局制作聊天布局

 更新時(shí)間:2022年01月27日 14:13:28   作者:南洋-呂曉貴  
大家好,本篇文章主要講的是Android RecycleView和線型布局制作聊天布局,感興趣的同學(xué)趕緊來看一看吧,對你有幫助的話記得收藏一下

一、首先在主布局中,用幀布局來填充 RecycleView 和 兩個(gè)模擬發(fā)送消息的Button

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".item_recycleview.MainActivityrecycle">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/test_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/test_click1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginLeft="20dp"
            android:layout_marginBottom="150dp"
            android:text="模擬插入對方消息" />
<EditText
    android:id="@+id/input_me"
    android:layout_gravity="bottom"
    android:layout_marginLeft="220dp"
    android:layout_marginBottom="190dp"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:hint="請輸入消息"/>
        <Button
            android:id="@+id/test_click"
            android:layout_width="203dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginLeft="220dp"
            android:layout_marginBottom="150dp"
            android:text="插入我發(fā)送的消息" />
    </FrameLayout>

</LinearLayout>

如下圖所示:

主布局

二、在一個(gè)布局中,加載左邊好友發(fā)送消息的布局,然后是自己發(fā)送消息的右邊布局

 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

    </data>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/from_use"
                android:background="#FF0067"
                android:layout_width="wrap_content"
                android:layout_height="24dp"
                android:text="張三:"
                android:layout_marginLeft="10dp"
                android:textColor="@color/black"
                android:textSize="18dp" />

            <TextView
                android:id="@+id/from_mesg"
                android:background="@drawable/good"
                android:layout_width="wrap_content"
                android:textSize="20sp"
                android:gravity="left|center"
                android:maxWidth="332dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:text="報(bào)告李四,我收到你的消息了"
                tools:ignore="RtlHardcoded" />

        </LinearLayout>
    </LinearLayout>
</layout>

如圖所示:

左布局

右邊的布局

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

    </data>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/from_self_mesg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginRight="65dp"
                android:background="@drawable/blue_p"
                android:gravity="left|center"
                android:maxWidth="332dp"
                android:paddingLeft="10dp"
                android:text="好的??!你最近過得怎么樣?"
                android:textColor="@color/black" />

            <TextView
                android:id="@+id/from_self"
                android:background="@color/purple_700"
                android:layout_width="wrap_content"
                android:layout_height="23dp"
                android:layout_alignParentRight="true"
                android:text="李四"
                android:layout_marginRight="8dp"
                android:textColor="@color/black"
                android:textSize="15dp" />

        </RelativeLayout>
    </LinearLayout>
</layout>

如圖所示:

在這里插入圖片描述

三、在MsgRecyclerViewActivity 中綁定控件和適配器

package com.example.mychat_layout.updata;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.mychat_layout.R;
import java.util.ArrayList;
import java.util.List;

public class MsgRecyclerViewActivity extends AppCompatActivity {
    private List<Msg> msgList = new ArrayList<>();
    private EditText inputText;
    private Button send, mTestClick;
    private RecyclerView msgRecyclerView;
    private MsgAdapter adapter;
    private RecyclerView mTestView;
    private Button mTestClick1;
    private EditText mInputMe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activityrecycle);
        initView();
    }
    private void initView() {
        //編輯文字
        mInputMe = (EditText) findViewById(R.id.input_me);
        msgRecyclerView = (RecyclerView) findViewById(R.id.test_view);
        mTestClick = (Button) findViewById(R.id.test_click);
        //初始化布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        msgRecyclerView.setLayoutManager(layoutManager);
        //初始化適配器
        adapter = new MsgAdapter(msgList);
        msgRecyclerView.setAdapter(adapter);
        send = findViewById(R.id.test_click1);//左

    }
    @Override
    protected void onResume() {
        super.onResume();
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //此處的content的內(nèi)容可以來自服務(wù)器接收到的消息,不能再主線程運(yùn)行
                Msg msg = new Msg("很好笑", Msg.TYPEE_RECEIVED);
                msg.setFromname("張三");
                msgList.add(msg);
                adapter.notifyItemInserted(msgList.size() - 1);//當(dāng)有新消息時(shí),刷新RecyclerView中的顯示
                msgRecyclerView.scrollToPosition(msgList.size() - 1); //將RecyclerView定位到最后一行

            }
        });
        mTestClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String me_send=mInputMe.getText().toString().trim();
                Msg msg = new Msg(me_send, Msg.TYPE_SENT);
                msg.setToname("李四");
                msgList.add(msg);
                adapter.notifyItemInserted(msgList.size() - 1);//當(dāng)有新消息時(shí),刷新RecyclerView中的顯示
                msgRecyclerView.scrollToPosition(msgList.size() - 1); //將RecyclerView定位到最后一行
                Log.e("ok", "onClick: " + "插入");
                mInputMe.setText("");
            }
        });
    }
}


四、設(shè)置適配器

package com.example.mychat_layout.updata;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mychat_layout.R;

import java.util.ArrayList;
import java.util.List;

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.MyViewHolder> {
    public List<Msg> msgList=new ArrayList<>();

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        LinearLayout leftLayout;
        LinearLayout rightLayout;
        TextView leftMsg,rightMsg;
        TextView leftMsg_use,rightMsg_use;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            leftLayout=(LinearLayout)itemView.findViewById(R.id.chat_left);
            rightLayout=(LinearLayout)itemView.findViewById(R.id.chat_right);

            leftMsg=(TextView)itemView.findViewById(R.id.from_mesg);
            leftMsg_use=(TextView)itemView.findViewById(R.id.from_use);

            rightMsg=(TextView)itemView.findViewById(R.id.from_self_mesg);
            rightMsg_use=(TextView)itemView.findViewById(R.id.from_self);
        }
    }

    public MsgAdapter(List<Msg> data){ //構(gòu)造函數(shù)
        msgList=data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { //替換視圖內(nèi)容

        Msg msg=msgList.get(position);
        if (msg.getType()==Msg.TYPEE_RECEIVED)
        {
            //如果收到的消息,則顯示左邊的消息布局,將右邊的消息布局隱藏
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);
            holder.leftMsg_use.setText(msg.getFromname());
            holder.leftMsg.setText(msg.getContent());
        }
        else if(msg.getType()==Msg.TYPE_SENT)
        {
            //如果是發(fā)出的消息,則顯示右邊的消息布局,將左邊的消息布局隱藏
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightMsg_use.setText(msg.getToname());
            holder.rightMsg.setText(msg.getContent());
        }
    }

    @Override
    public int getItemCount() {
        return msgList.size();
    }
}


消息的實(shí)體類

package com.example.mychat_layout.updata;

public class Msg {
    public static final int TYPEE_RECEIVED=0;
    public static final int TYPE_SENT=1;
    private String content;
    private String toname;

    public String getToname() {
        return toname;
    }

    public void setToname(String toname) {
        this.toname = toname;
    }

    public String getFromname() {
        return fromname;
    }

    public void setFromname(String fromname) {
        this.fromname = fromname;
    }

    private String fromname;
    private int type;

    public Msg(String content, int type)
    {
        this.content=content;
        this.type=type;
    }
    public String getContent()
    {
        return content;
    }
    public int getType()
    {
        return type;
    }
}


最后打工告成
部分邏輯思想來自簡書的網(wǎng)友大佬,本人只是簡單的加工了其他邏輯,部分邏輯思想來自簡書的網(wǎng)友大佬,本人只是簡單的加工了其他邏輯,部分邏輯思想來自簡書的網(wǎng)友大佬,本人只是簡單的加工了其他邏輯

總結(jié)

到此這篇關(guān)于Android RecycleView和線型布局制作聊天布局的文章就介紹到這了,更多相關(guān)Android制作聊天布局內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

定安县| 綦江县| 全椒县| 井陉县| 封开县| 扬州市| 即墨市| 双辽市| 宁阳县| 旬邑县| 秀山| 荔浦县| 宁蒗| 武功县| 广州市| 古交市| 永德县| 苏州市| 洛南县| 福清市| 榕江县| 即墨市| 连山| 沅江市| 金昌市| 樟树市| 咸阳市| 田东县| 潞城市| 于都县| 清镇市| 云南省| 长白| 奉新县| 南昌县| 洛隆县| 长海县| 衡水市| 丰原市| 芜湖市| 谢通门县|