欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

一、首先在主布局中,用幀布局來填充 RecycleView 和 兩個模擬發(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>

如下圖所示:

主布局

二、在一個布局中,加載左邊好友發(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="報告李四,我收到你的消息了"
                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)有新消息時,刷新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)有新消息時,刷新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)文章

  • Android 啟動頁白屏解決方案

    Android 啟動頁白屏解決方案

    這篇文章主要介紹了Android 啟動頁白屏的解決方案,幫助大家更好的進(jìn)行Android app的開發(fā),感興趣的朋友可以了解下
    2020-12-12
  • Android ListView之setEmptyView正確使用方法

    Android ListView之setEmptyView正確使用方法

    這篇文章主要介紹了Android ListView之setEmptyView正確使用方法的相關(guān)資料,希望通過本文能幫助到大家使用該方法,需要的朋友可以參考下
    2017-09-09
  • Kotlin 協(xié)程 supervisorScope {} 運(yùn)行崩潰解決方法

    Kotlin 協(xié)程 supervisorScope {} 運(yùn)行崩潰解決方法

    看過很多?supervisorScope {}?文檔的使用,我照抄一摸一樣的代碼,運(yùn)行就崩潰,最后找到了解決方法,應(yīng)該是kotlin版本更新做過改動,當(dāng)前我使用的是?androidx.core:core-ktx:1.9.0,本文給大家介紹Kotlin 協(xié)程 supervisorScope {} 運(yùn)行崩潰解決方法,感興趣的朋友一起看看吧
    2024-01-01
  • Android BroadcastReceiver接收收到短信的廣播

    Android BroadcastReceiver接收收到短信的廣播

    這篇文章主要為大家詳細(xì)介紹了Android BroadcastReceiver接收收到短信的廣播,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android ViewGroup事件分發(fā)和處理源碼分析

    Android ViewGroup事件分發(fā)和處理源碼分析

    這篇文章主要為大家介紹了Android ViewGroup事件分發(fā)和處理源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Android實(shí)現(xiàn)掃雷小游戲

    Android實(shí)現(xiàn)掃雷小游戲

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • Android實(shí)現(xiàn)歷史搜索記錄

    Android實(shí)現(xiàn)歷史搜索記錄

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)歷史搜索記錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android模仿用戶設(shè)置密碼實(shí)例

    Android模仿用戶設(shè)置密碼實(shí)例

    首先有2個對話框,沒有設(shè)置過密碼,需要設(shè)置dialog_set_password.xml,用戶設(shè)置過密碼,不需要設(shè)置,直接輸入密碼dialog_input_password.xml,具體實(shí)現(xiàn)代碼,大家參考下本文
    2017-03-03
  • Android新聞廣告條滾動效果

    Android新聞廣告條滾動效果

    這篇文章主要為大家詳細(xì)介紹了Android新聞廣告條滾動效果的實(shí)現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android Activity與Fragment實(shí)現(xiàn)底部導(dǎo)航器

    Android Activity與Fragment實(shí)現(xiàn)底部導(dǎo)航器

    這篇文章主要介紹了Android Activity與Fragment實(shí)現(xiàn)底部導(dǎo)航器的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下
    2016-11-11

最新評論