Adapter
1.概念
Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带。在常见的View(ListView,GridView)等地方都需要用到Adapter。如下图直观的表达了Data、Adapter、View三者的关系:
2. Android中所有的Adapter一览:
3.实例
1.添加依赖
由于我们会用到RecyclerView(这是一个滚动控件,可以使这个控件内的每一个Item(项目;子项)实现滚动),因此首先需要在app/build.gradle当中添加依赖库,如下所示:
dependencies {
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
复制代码
2.主界面的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d8e0e8"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/msg_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></androidx.recyclerview.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/input_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="输入要发送的内容"
android:maxLines="2"/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:textAllCaps="false"/>
<!-- textAllCaps="false"表示关掉文本字母全部大写方法 -->
</LinearLayout>
</LinearLayout>
复制代码
3.msg_item.xml
布局
需要注意的有两个地方:
①第一个聊天框和第二个聊天框的的对齐方式分别是左对齐和右对齐
②也许看到这你就会产生除这样的疑惑了:
为什么一个子项里面要设置两个TextView呢?怎么能让收到的消息和发出的消息都放在同一个不布局里呢? 这样运行的程序会不会是接受和发送两个聊天框重复的界面呢?其实认真看的读者或许不会产生这种疑问, 答案显然就是上面提到过的:我们会根据信息的类型来判断显示哪一个TextView。
<?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">
<LinearLayout
android:id="@+id/left_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="@android:color/holo_blue_bright">
<!-- 这里设置了聊天框(backgroud),聊天框长度会随发送或者接收的数据多少来自动拉伸 -->
<!-- 具体如何设置自动拉伸,我的推荐是解决下面两个问题就OK了:
1.如何在AndroidStudio里直接使用draw9patch(AS已经集成了这个功能了,当然网上也有教AS之外使用的)
2.如何使用draw9patch-->
<TextView
android:id="@+id/left_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/right_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@android:color/holo_blue_dark">
<TextView
android:id="@+id/right_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"/>
</LinearLayout>
</LinearLayout>
复制代码
4. msg.java
类
- 自定义信息类,用于存放信息的类型(收or发)以及信息的内容。
- 我们将文本内容和数据类型传给Msg的一个对象,
- 之后在别的函数里面读取文本内容和判断依据,
- 也是对信息包含属性的一种封装,这就是Msg.java的作用
/**
* 自定义信息类,用于存放信息的类型(收or发)以及信息的内容。
*
* 我们将文本内容和数据类型传给Msg的一个对象,
* 之后在别的函数里面读取文本内容和判断依据,
* 也是对信息包含属性的一种封装,这就是Msg.java的作用
* @author iwenLjr
* Create to 2020/3/3 10:44
*/
public class Msg {
public static final int TYPE_RECEICED = 0; // 接收消息的标志
public static final int TYPE_SEND = 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;
}
}
复制代码
5. 创建RecyclerView的适配器类MsgAdapter。
类MsgAdapter继承于RecyclerView.Adapter,并将泛型指定为自定义的内部类ViewHolder;
继承自类RecyclerView.Adapter后重写的几个方法;
下面我们慢慢来理解各个函数的作用;
- 构造函数MsgAdapter
构造函数MsgAdapter,在创建这个适配器对象的时候,将所有数据都传入,以便进行之后的操作。
public MsgAdapter(List<Msg> msgList){
mMsgList = msgList;
}
复制代码
-
getItemCount
利用创建时传入的数据,获取列表里总共有多少个Item(项目)。对于这个函数的作用,我的理解是返回能被布局的总的Item的数量。至于返回这个数据有什么作用,我们就不必深究了,系统会自动调用这个函数来获得它需要的数据。
public int getItemCount(){
return mMsgList.size();
}
复制代码
-
内部类ViewHolder
我将内部类ViewHolder理解为视图控件持有类,是一个囊括本类对象里所有控件的容器,本类的作用也是为了方便,在后面不用重复去定义这些控件,为什么这么说呢?
先看代码,这里有个值得注意的地方:
①它继承于RecyclerView.ViewHolder类,这与外层MsgAdapter类相似。
②ViewHolder类还在构造函数里调用了父类的构造函数,并且为每一个Item里的所有控件都创建了一个对应的对象。
由此,ViewHolder类创建的对象就能够对Item里面的控件进行操作了。
这里你可能会有疑问,构造函数中的参数是哪里来的,系统怎么知道需要哪个Item?
这个不用担心,这些系统会自动帮我们做,把传入的List对象一个个遍历,单独地对每一个对象进行操作。
static class ViewHolder extends RecyclerView.ViewHolder{
LinearLayout leftLayout;
LinearLayout rightLayout;
TextView leftMsg;
TextView rightMsg;
public ViewHolder(View view){
super(view);
leftLayout = (LinearLayout)view.findViewById(R.id.left_layout);
rightLayout = (LinearLayout)view.findViewById(R.id.right_layout);
leftMsg = (TextView) view.findViewById(R.id.left_msg);
rightMsg = (TextView) view.findViewById(R.id.right_msg);
}
}
复制代码
-
onCreaterViewHolder函数
①在实际开发种 LayoutInflater 这个类还是非常有用的,它的作用类似于 findViewById(), 不同点是 LayoutInflater 是用来找 layout 下x ml布局文件,并且实例化! findViewById() 是找具体 xml 下的具体 widget控件 (如:Button,TextView等)。
②对于一个没有被载入或者想要动态载入的界面,都需要使用 inflate 来载入。
③我们要知道,什么是已经被载入的 layout ,什么是还没有载入的?
我们启动一个应用,与入口 Activity 相关的 layout {常见的是main.xml} 就是被载入的,即在 Oncreate() 中的,对于一个已经载入的 Activity,,就可以使用实现了这个 Activiyt 的 findViewById 方法来获得其中的界面元素。 * 而对于其它没有被载入的 layout,就要动态载入了或通过另一个 activity。 这个 item 需要我们用 inflate 函数把 msg_item 动态的加载进main布局, 并且返回了一个用来获取 item 里控件并且对其进行操作的 View 对象。
public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
return new ViewHolder(view);
}
复制代码
-
onBindViewHolder : 通过判断信息类型来决定显示或者隐藏哪个布局。
对控件有约束的控制
/**
* 对控件有约束的控制,意思是通过判断信息类型来决定显示或者隐藏哪个布局。
*
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Msg msg = mMsgList.get(position);
if(msg.getType() == Msg.TYPE_RECEICED){
// 如果是接收消息
holder.leftLayout.setVisibility(View.VISIBLE); // 左边对话框出现
holder.rightLayout.setVisibility(View.GONE); // 右边对话框隐藏
holder.leftMsg.setText(msg.getContent());
}else if(msg.getType() == Msg.TYPE_SEND){
// 如果是发送消息
holder.rightLayout.setVisibility(View.VISIBLE);// 右边对话框出现
holder.leftLayout.setVisibility(View.GONE); // 左边对话框隐藏
holder.rightMsg.setText(msg.getContent());
}
}
复制代码
7.在main_activity.java
使用
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputText = (EditText)findViewById(R.id.input_text);
send = (Button)findViewById(R.id.send);
msgRecyclerView = (RecyclerView)findViewById(R.id.msg_recycler_view);
initData();
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
msgRecyclerView.setLayoutManager(layoutManager);
adapter = new MsgAdapter(msgList);
msgRecyclerView.setAdapter(adapter);
/**
* 也许你做过FruitAdapter,那理解起这段代码来就会很轻松了,
* 但是为了面向更多像博主一样的初学者(初学者难免会遇到一些很简单的甚至于大神都懒得回答的问题),就说的明白点。
* ListView可以实现上下滚动,但是不能实现横向滚动(例如微信选择小程序时的那个横向滚动),但是 RecyclerView 能够实现。
* 原因:ListView的布局排列是由自身去管理的,而RecyclerView则将这个工作交给了LayoutManager,
* LayoutManager中制定了一套可扩展的布局排列接口,子类只要按照接口的规范来实现,就能定制出各种不同排列方式的布局了。
* 这个程序我们使用了LinearLayoutManager这种线性的布局排列
*/
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = inputText.getText().toString();
if(!"".equals(content)){
Msg msg = new Msg(content,Msg.TYPE_SEND);
msgList.add(msg);
adapter.notifyItemChanged(msgList.size()-1);//当有新消息时,刷新ListView中的显示
msgRecyclerView.scrollToPosition(msgList.size()-1);//将ListView定位到最后一行
inputText.setText("");//消息发出后清空输入框中的内容
}
}
});
}//事件响应
private void initData() {
Msg msg1 = new Msg("Hello iwen.",Msg.TYPE_RECEICED);
msgList.add(msg1);
Msg msg2 = new Msg("Hello.Who is that?",Msg.TYPE_SEND);
msgList.add(msg2);
Msg msg3 = new Msg("This is Tom.Nice talking to you.",Msg.TYPE_RECEICED);
msgList.add(msg3);
Msg msg4 = new Msg("This is Tom.Nice talking to you.",Msg.TYPE_SEND);
msgList.add(msg4);
Msg msg5 = new Msg("This is Tom.Nice talking to you.This is Tom.Nice talking to you.This is Tom.Nice talking to you.This is Tom.Nice talking to you.",Msg.TYPE_RECEICED);
msgList.add(msg5);
Msg msg6 = new Msg("This is Tom.Nice talking to you.",Msg.TYPE_RECEICED);
msgList.add(msg6);
Msg msg7 = new Msg("This is Tom.Nice talking to you.",Msg.TYPE_SEND);
msgList.add(msg7);
Msg msg8 = new Msg("This is Tom.Nice talking to you.",Msg.TYPE_RECEICED);
msgList.add(msg8);
}
}
复制代码