Recyclerview新增控件的应用:
1.在APP->build.gradedependencies中添加:
{implementation ‘androidx.recyclerview:recyclerview:1.1.0’}
2.
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
3.适配器
//继承RecyclerView.Adapter,必须重写onCreateViewHolder、onBindViewHolder、getItemCount
public class FruitAdapter extends RecyclerView.Adapter <FruitAdapter.ViewHolder>{
private List<Fruit> myFruitList;
//定义一个内部类
// 构造方法中传入一个人View参数,通常是RecyclerView子項的最外层布局
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView fruitImage;
TextView fruitName;
public ViewHolder(View view){
super(view);
fruitImage=(ImageView) view.findViewById(R.id.fruit_image);
fruitName=(TextView) view.findViewById(R.id.fruit_name);
}
}
//把要展示的数据源传进来,并赋值给全局变量
public FruitAdapter(List<Fruit> fruitList){
myFruitList=fruitList;
}
@Override
//用于创建ViewHolder实例,将fruit_item布局加载进来,
// 创建一个 ViewHolder实例,并把加载的布局传入到构造方法中,最后将实例返回
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
ViewHolder holder=new ViewHolder(view);
return holder;
}
@Override
//用于对 RecyclerView子项的数据进行赋值的,会在每个子项被滚动到屏幕内的时候执行,
// 这里我们通过 position 参数得到当前项的Fruit 实例,
// 然后再将数据设置到ViewHolder 的ImageView和 TextView当中即可。
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Fruit fruit=myFruitList.get(position);
holder.fruitName.setText(fruit.getName());
holder.fruitImage.setImageResource(fruit.getImageId());
}
@Override
//用于告诉RecyclerView一共有多少子项,返回数据源的长度。
public int getItemCount() {
return myFruitList.size();
}
}
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits(); // 初始化水果数据
//获取 RecyclerView实例
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
//创建一个LinearLayoutManager对象,把它设置到RecyclerView中
//LayoutManager用于指定 RecyclerView的布局方式,
// LinearLayoutManager即线性布局,用于显示和LISTView相同的效果
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//创建了FruitAdapter的实例,并将水果的数据传入到FruitAdapter的构造函数中
FruitAdapter adapter = new FruitAdapter(fruitList);
//调用setAdapter()的方法来完成适配器设置
recyclerView.setAdapter(adapter);
}
private void initFruits() {
//创建水果实例,并添加到水果实例列表
for (int i = 0; i < 5; i++) {
Fruit apple = new Fruit("Apple", R.drawable.apple_pic);
fruitList.add(apple);
Fruit banana = new Fruit("Banana", R.drawable.banana_pic);
fruitList.add(banana);
Fruit orange = new Fruit("Orange", R.drawable.orange_pic);
fruitList.add(orange);
Fruit watermelon= new Fruit("Watermelon", R.drawable.watermelon_pic);
fruitList.add(watermelon);
Fruit pear = new Fruit("Pear", R.drawable.pear_pic);
fruitList.add(pear );
Fruit grape = new Fruit("grape", R.drawable.grape_pic);
fruitList.add(grape);
Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple_pic);
fruitList.add(pineapple);
Fruit strawberry = new Fruit("Strawberry", R.drawable.strawberry_pic);
fruitList.add(strawberry);
Fruit cherry = new Fruit("Cherry", R.drawable.cherry_pic);
fruitList.add(cherry);
Fruit mango = new Fruit("Mango", R.drawable.mango_pic);
fruitList.add(mango);
}
}
}
5.水平实现
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="100dp"
android:layout_height="400dp">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_weight="1"/>
</LinearLayout>
protected void onCreate(Bundle savedInstanceState) {
...
//设置排列方向,默认纵向
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager);
FruitAdapter adapter=new FruitAdapter(fruitList);
recyclerView.setAdapter(adapter);
}
瀑布:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
//宽度会根据布局的列数来自动适配
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:layout_marginTop="10dp"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits(); // 初始化水果数据
//获取 RecyclerView实例
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
//创建一个StaggeredGridLayoutManager实例(指定布局的列数,指定布局的排列方向/指定为纵向)
StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
FruitAdapter adapter=new FruitAdapter(fruitList);
recyclerView.setAdapter(adapter);
}
private void initFruits() {
//创建水果实例,并添加到水果实例列表
for (int i = 0; i < 5; i++) {
Fruit apple = new Fruit(getRandomLengthName("Apple"), R.drawable.apple_pic);
fruitList.add(apple);
Fruit banana = new Fruit(getRandomLengthName("Banana"), R.drawable.banana_pic);
fruitList.add(banana);
Fruit orange = new Fruit(getRandomLengthName("Orange"), R.drawable.orange_pic);
fruitList.add(orange);
Fruit watermelon= new Fruit(getRandomLengthName("Watermelon"), R.drawable.watermelon_pic);
fruitList.add(watermelon);
Fruit pear = new Fruit(getRandomLengthName("Pear"), R.drawable.pear_pic);
fruitList.add(pear );
Fruit grape = new Fruit(getRandomLengthName("grape"), R.drawable.grape_pic);
fruitList.add(grape);
Fruit pineapple = new Fruit(getRandomLengthName("Pineapple"), R.drawable.pineapple_pic);
fruitList.add(pineapple);
Fruit strawberry = new Fruit(getRandomLengthName("Strawberry"), R.drawable.strawberry_pic);
fruitList.add(strawberry);
Fruit cherry = new Fruit(getRandomLengthName("Cherry"), R.drawable.cherry_pic);
fruitList.add(cherry);
Fruit mango = new Fruit(getRandomLengthName("Mango"), R.drawable.mango_pic);
fruitList.add(mango);
}
}
private String getRandomLengthName(String name){
Random random=new Random();
int length=random.nextInt(20)+1;
StringBuilder builder=new StringBuilder();
for(int i=0;i<length;i++){
builder.append(name);
}
return builder.toString();
}
}
点击事件注册:
public class FruitAdapter extends RecyclerView.Adapter <FruitAdapter.ViewHolder>{
public class ViewHolder extends RecyclerView.ViewHolder{
...
View fruitview;
...
public ViewHolder(View view){
...
fruitview=view;
...
}
}
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
...
final ViewHolder holder=new ViewHolder(view);
holder.fruitview.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
int position=holder.getAdapterPosition();
Fruit fruit=myFruitList.get(position);
Toast.makeText(view.getContext(),"you clicked view"+fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
holder.fruitImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
Fruit fruit = myFruitList.get(position);
Toast.makeText(v.getContext(),"you clicked image "+fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
return holder;
}
对话界面适配器
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
private List<Msg> mMsgList;
//定义一个内部类
static public class ViewHolder extends RecyclerView.ViewHolder {
//控件
LinearLayout leftLayout;
LinearLayout rightLayout;
TextView leftMsg;
TextView rightMsg;
// 构造方法中传入一个人View参数,通常是RecyclerView子項的最外层布局
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> mMsgList) {
this.mMsgList = mMsgList;
}
//用于创建ViewHolder实例,将msg_item布局加载进来,
// 创建一个 ViewHolder实例,并把加载的布局传入到构造方法中,最后将实例返回
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item, parent, false);
return new ViewHolder(view);
}
@Override
//用于对 RecyclerView子项的数据进行赋值的,会在每个子项被滚动到屏幕内的时候执行,
// 这里我们通过 position 参数得到当前项的msg实例,
// 然后再将数据设置到ViewHolder 的当中。
public void onBindViewHolder(ViewHolder holder, int position) {
Msg msg = mMsgList.get(position);
if (msg.getType() == Msg.TYPE_RECEIVED) {
//如果收到的消息,则显示左边的消息布局,将右边的消息布局隐藏
// 将数据设置到ViewHolder 的leftLayout当中。
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());
}
}
//用于告诉RecyclerView一共有多少子项,返回数据源的长度。
@Override
public int getItemCount() {
return mMsgList.size();
}
}
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);
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(){
@Override
public void onClick(View view) {
//获取EditText中的内容
String content=inputText.getText().toString();
if(!"".equals(content)){
Msg msg=new Msg(content,Msg.TYPE_SEND);
msgList.add(msg);
adapter.notifyItemInserted(msgList.size()-1);//当有新消息时,刷新ListView中的显示
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_SEND);
msgList.add(msg2);
Msg msg3=new Msg("This is Tom.Nice talking to you",Msg.TYPE_RECEIVED);
msgList.add(msg3);
}
}
<?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="@drawable/message_left">
<TextView
android:id="@+id/left_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#fff"/>
</LinearLayout>
<LinearLayout
android:id="@+id/right_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/message_right">
<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>
<?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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/msg_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<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="Type something here"
android:maxLines="2"/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"/>
</LinearLayout>
</LinearLayout>