Android 第二十一課 RecyclerView簡單的應用之編寫“精美”的聊天頁面

1、由于我們會使用到RecyclerView,因此首先需要在app/build.gradle當中添加依賴庫。如下:

apply plugin: 'com.android.application'
....
dependencies {....compile 'com.android.support:recyclerview-v7:26.1.0'
}

2、然后開始編寫主頁面,修該activity_main.xml中的代碼,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#d8e0e8"><android.support.v7.widget.RecyclerViewandroid:id="@+id/msg_recycler_view"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><EditTextandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:hint="Type something here"android:maxLines="2"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="send"/></LinearLayout></LinearLayout>

可以看到主頁面上面是一個RecyclerView,用來顯示聊天的消息內容,下面是一個EditView,用于輸入消息和Button,用于發送消息。

3、然后定義消息的是實體類,新建Msg,代碼如下:

package com.example.messsage;/*** Created by ZHJ on 2018/3/14.*/public class Msg {public static final int TYPE_RECEIVED = 0;public static final int TYPE_SENT = 1;private String content;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;}
}

Msg類中只有兩個字段,content表示消息的內容,type表示消息的類型。其中消息類型有兩個值可選,TYPE_RECEIVED表示這是一條收到的消息,TYPE_SENT表示這是一條發出的消息。

4、接著編寫RecyclerView子項的布局,新建msg_item.xml,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"><LinearLayoutandroid:id="@+id/left_layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="left"android:background="@drawable/message_left"><TextViewandroid:id="@+id/left_msg"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_margin="10dp"android:textColor="#f00"/></LinearLayout><LinearLayoutandroid:id="@+id/right_layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right"android:background="@drawable/message_right"><TextViewandroid:id="@+id/right_msg"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_margin="10dp"/></LinearLayout></LinearLayout>

我們讓收到的消息居左對齊,發出的消息居右對齊,并且使用meassage_right和message_left作為背景圖,這里我們讓收到的消息和發出的消息都放在了同一布局里,我們可以根據消息的類型來決定隱藏和顯示哪種消息就可以了。

5、創建RecyclerView的適配器類,新建類MsgAdapter,代碼如下:

package com.example.messsage;import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;import java.util.List;/*** Created by ZHJ on 2018/3/14.*/public class MsgAdapter extends RecyclerView.Adapter <MsgAdapter.ViewHolder>{private List<Msg> mMsgList;static class ViewHolder extends RecyclerView.ViewHolder{LinearLayout leftLayout;LinearLayout rightLayout;TextView leftMsg;TextView rightMsg;public ViewHolder(View view){super(view);leftLayout = (LinearLayout) itemView.findViewById(R.id.left_layout);rightLayout = (LinearLayout)itemView.findViewById(R.id.right_layout);leftMsg = (TextView)itemView.findViewById(R.id.left_msg);rightMsg = (TextView)itemView.findViewById(R.id.right_msg);}}public MsgAdapter (List<Msg>msgList){mMsgList = msgList;}@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);return new ViewHolder(view);}@Overridepublic void onBindViewHolder(ViewHolder holder, int position) {Msg msg = mMsgList.get(position);if (msg.getType() == Msg.TYPE_RECEIVED){holder.leftLayout.setVisibility(View.VISIBLE);holder.rightLayout.setVisibility(View.GONE);holder.leftMsg.setText(msg.getContent());}else if (msg.getType() == Msg.TYPE_SENT){holder.leftLayout.setVisibility(View.GONE);holder.rightLayout.setVisibility(View.VISIBLE);holder.rightMsg.setText(msg.getContent());}}@Overridepublic int getItemCount() {return mMsgList.size();}
}

在onBindViewHolder()方法中增加了對消息類型到 判斷,如果這條消息是收到的,則顯示左邊的的消息布局,如果消息是發出的,則顯示右邊的消息布局。

我們先定義了一個內部類ViewHolder,ViewHolder要繼承自RecyclerView.ViewHolder。然后,ViewHolder的構造函數中要傳入一個View參數,這個參數通常就是RecyclerView子項的最外層布局,那么我們就可以通過findViewById()方法來獲取到布局中的leftMsg 和rightMsg 的實例了。

????????MsgApapter中也有一個構造函數,這個方法用于把要展示的數據源傳進來,并賦值給一個全局變量mMsgList,我們后續的操作都將在這個數據源的基礎上進行。繼續往下看,由于mMsgAdapter是繼承自RecyclerView.Adapter的,那么就必須重寫onCreateViewHolder(),onBindViewHolder()和getItemCount()這3個方法。

????onCreateViewHolder()方法用于創建ViewHolder實例的,我們在這個方法中將R.layout.msg_item布局加載進來,然后創建一個ViewHolder實例,并把加載進來,然后創建一個ViewHolder實例,并把加載出來的布局傳入到構造函數中,最后將ViewHolder()方法的實例返回。

????onBindViewHolder()方法是用于對RecyclerView子項的數據進行賦值,會在每個子項被滾到屏幕內的時候執行,這里我們通過position參數得到當前項的Msg實例,然后再將數據設置到ViewHolder的ImageView和TextView當中即可。

????getItemCount()方法就簡單了,它用于告訴RecyclerView一共有多少子項,直接返回數據源的長度就可以了。

6、最后修該Main_activity中的代碼,如下:


package com.example.messsage;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity {private List<Msg> msgList = new ArrayList<>();private EditText inputText;private Button send;private RecyclerView msgRecyclerView;private MsgAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initMsgs();//初始化消息數據inputText = (EditText)findViewById(R.id.input_text);send =(Button) findViewById(R.id.send);msgRecyclerView = (RecyclerView)findViewById(R.id.msg_recycler_view);
       LinearLayoutManager layoutManager = new LinearLayoutManager(this);msgRecyclerView.setLayoutManager(layoutManager);adapter = new MsgAdapter(msgList);msgRecyclerView.setAdapter(adapter);send.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String content = inputText.getText().toString();if (!"".equals(content)){Msg msg = new Msg(content,Msg.TYPE_SENT);msgList.add(msg);adapter.notifyItemInserted(msgList.size()-1);msgRecyclerView.scrollToPosition(msgList.size() - 1);inputText.setText("");}}});}private void initMsgs(){Msg msg1 = new Msg("Hello guy.",Msg.TYPE_RECEIVED);msgList.add(msg1);Msg msg2 = new Msg("hello,Who is that?",Msg.TYPE_SENT);msgList.add(msg2);Msg msg3 = new Msg("This is Tom.Nice talking to you.",Msg.TYPE_RECEIVED);msgList.add(msg3);}
}

initMsgs()方法中我們先初始化了幾條數據用于在RecyclerView中顯示出來。

然后在按鈕的點擊事件中獲取了EditText中的內容,如果內容不為空,則創建出一個新的Msgz對象,并把它添加都msgList列表中。之后又調用了適配器的notifyItemInserted()方法,用于通知列表有新的數據插入,這樣新增的一條消息才能夠在RecyclerView中顯示出來。接著調用RecyclerView的scrollPosition()方法將顯示的數據定位到最后一行,以保證一定可以看到最后發出的一條消息,最后調用EditText的setText()方法將輸入的內容清空。

運行之后,如下:














本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274079.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274079.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274079.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

VS 2008 生成操作中各個選項的差別

近日&#xff0c;在編譯C#項目時經常發現有些時候明明代碼沒錯&#xff0c;但就是編譯不過&#xff0c;只有選擇重新編譯或者清理再編譯才會不出錯&#xff0c;本著求學的態度&#xff0c;搜羅了下VS2008IDE中生成操作的種類以及差別&#xff0c;整理如下&#xff1a;內容(Cont…

dbus-python指南

菜鳥學dbus-python&#xff0c;翻譯dbus-python指南&#xff0c;錯誤之處請在所難免&#xff0c;請諸位不吝賜教&#xff0c;多多指正&#xff01;查看英文原版請點這里。 連接總線Connecting to the Bus方法調用Making method calls代理對象proxy objects接口和方法Interfaces…

JavaScript 第三課 DOM

主要內容&#xff1a; 節點5個常用的DOM方法&#xff1a;getElementById、getElementByTagname、getElementByClassName、getAttribute和setAttribute詳細內容: 1、文檔&#xff1a;DOM中的“D”如果沒有document(文檔),DOM也就無從談起。當創建了一個網頁并把它加載到Web瀏覽器…

源碼編譯安裝Nginx

1.源碼下載 Nginx在github上有一個只讀源碼庫&#xff0c;我獲取的源碼方式為&#xff1a; git clone https://github.com/nginx/nginx.git 2.configure 我下載源碼的時候&#xff0c;github上的源碼的目錄結構為: auto, conf, contrib, docs, misc, src共6個目錄。src目錄是…

SOAP協議初級指南(2)

目前的技術存在的問題&#xff1f;   盡管DCOM和IIOP都是固定的協議&#xff0c;業界還沒有完全轉向其中任何一個協議。沒有融合的部分原因是文化的問題所致。而且在當一些組織試圖標準化一個或另一個協議的時候&#xff0c;兩個協議的技術適用性就被提出質疑。傳統上認為DC…

JavaScript 第四課 案例研究:JavaScript圖片庫

主要內容&#xff1a;編寫一個優秀的標記文件編寫一個JavaScript函數以顯示用戶想要查看的內容由標記出發函數調用使用幾個新方法擴展這個JavaScript函數 學習過DOM&#xff0c;我們用JavaScript和DOM去建立一個圖片庫。最好的辦法是什么呢&#xff1f; 利用JavaScript來建立圖…

windows下mongodb安裝與使用整理

一、首先安裝mongodb 1.下載地址&#xff1a;http://www.mongodb.org/downloads 2.解壓縮到自己想要安裝的目錄&#xff0c;比如d:\mongodb 3.創建文件夾d:\mongodb\data\db、d:\mongodb\data\log&#xff0c;分別用來安裝db和日志文件&#xff0c;在log文件夾下創建一個日志文…

可變參數列表(va_list,va_arg,va_copy,va_start,va_end)

本文轉自:http://blog.csdn.net/costa100/article/details/5787068 va_list arg_ptr&#xff1a;定義一個指向個數可變的參數列表指針&#xff1b;      va_start(arg_ptr, argN)&#xff1a;使參數列表指針arg_ptr指向函數參數列表中的第一個可選參數&#xff0c;說明&…

src與href屬性的區別

src和href之間存在區別&#xff0c;能混淆使用。src用于替換當前元素&#xff0c;href用于在當前文檔和引用資源之間確立聯系。 src是source的縮寫&#xff0c;指向外部資源的位置&#xff0c;指向的內容將會嵌入到文檔中當前標簽所在位置&#xff1b;在請求src資源時會將其指向…

USACO4.12Beef McNuggets(背包+數論)

昨天晚上寫的一題 結果USACO一直掛中 今天交了下 有一點點的數論知識 背包很好想 就是不好確定上界 官方題解&#xff1a; 這是一個背包問題。一般使用動態規劃求解。 一種具體的實現是&#xff1a;用一個線性表儲存所有的節點是否可以相加得到的狀態&#xff0c;然后每次可以…

Java 循環語句中 break,continue,return有什么區別?

break 結束循環&#xff0c;跳出循環體,進行后面的程序;continue 結束本次循環&#xff0c;進行下次循環;return 跳出循環體所在的方法&#xff0c;相當于結束該方法; 例子&#xff1a; public class whiletrueTest{public static void main(String[] args) {heihei();haha();…

Epoll模型詳解

轉自http://blog.163.com/huchengsz126/blog/static/73483745201181824629285/ Linux 2.6內核中提高網絡I/O性能的新方法-epoll I/O多路復用技術在比較多的TCP網絡服務器中有使用&#xff0c;即比較多的用到select函數。 1、為什么select落后 首先&#xff0c;在Linux內核中…

運算放大器單電源應用中的使用齊納二極管偏置方法

運算放大器單電源應用中的偏置方法除了使用大電阻使運放輸出達到電源電壓的一半外&#xff0c;還有使用齊納二極管&#xff08;穩壓管&#xff09;方法也能得到達到應用目的。 下面就推薦幾個齊納二極管&#xff08;分別對應著電源電壓是15V,12V&#xff0c;9V;5V&#xff09; …

Java——demo之仿ATM操作

java.util.Scanner類&#xff0c;這是一個用于掃描輸入文本的新的實用程序。其中nextInt()獲取String型&#xff0c;而next()獲取int、double型。這是一個仿ATM的小程序。 實現條件 1.登陸界面&#xff0c;2.三次登陸機會&#xff0c;登陸成功進入登陸菜單&#xff0c;3&#x…

dpi 、 dip 、分辨率、屏幕尺寸、px、density 關系以及換算

本文轉自&#xff1a;http://www.cnblogs.com/yaozhongxiao/archive/2014/07/14/3842908.html 一、基本概念 dip &#xff1a; Density independent pixels &#xff0c;設備無關像素。 dp &#xff1a;就是dip px &#xff1a; 像素 dpi &#xf…

Ninject使用demo

public class HomeController : Controller{public ActionResult Index(){ //核心對象IKernel ninjectKernel new StandardKernel();ninjectKernel.Bind<IValueCaculator>().To<LinqValueCalcalator>(); //方案1&#xff1a;獲取接口實例IV…

Java 集合中關于Iterator 和ListIterator的詳解

1.Iterator Iterator的定義如下&#xff1a;public interface Iterator<E> {}Iterator是一個接口&#xff0c;它是集合的迭代器。集合可以通過Iterator去遍歷集合中的元素。Iterator提供的API接口如下&#xff1a;forEachRemaining(Consumer<? super E> action)&a…

使用xrandr和cvt命令添加自定義的分辨率模式

可以使用xrandr -q命令查看當前支持的分辨率模式: 如果過沒有你想要的分辨率模式,則需要自己創建新的分辨率模式,例如,我想要創建800x750的分辨率模式,步驟如下: 1.使用cvt命令創建新的分辨率: 2.使用xrandr –newmode modeline信息(CVT命令產生的結果)創建新的mode: $xra…

Java List集合

我們先看一下jdk1.9對其的描述&#xff1a;什么是List&#xff0c;也就是一個有序集合(序列)。1.List接口 List集合代表一個有序集合&#xff0c;集合中每個元素都有其對應的順序索引。List集合允許使用重復元素&#xff0c;可以通過索引來訪問指定位置的集合元素。 List接口繼…

winform錯誤提示 :窗口類名無效(Window class name is not valid)

winfrom 程序在 xp 操作系統上報錯提示 窗口類名無效(Window class name is not valid) 解決方法 注釋 Program類 里 這句 Application.EnableVisualStyles(); 解決轉載于:https://www.cnblogs.com/z_lb/p/3288850.html