第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

时间:2022-04-22 04:41:29

简述教程:https://www.jianshu.com/p/4fc6164e4709

一 基础准备

1 添加RecyclerView控件引用库文件

2 总布局添加RecyclerView控件

3 RecyclerView控件设置子控件fruit_item.xml(每一个小单元)

4设置特定实体类 (水果类)

5创建适配器

5.1 修改引用实体类

5.2 布局初始化加入子控件fruit_item.xml

5.3获取子控件中的各个控件ID+设置按键动作

5.4获取当前位置(水果)实例信息(名称+图片ID),赋予子控件的各个ID

二 开始使用

1 特定类实例化-赋值

2 特定类实例化-存放在list中

3 创建

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

 applicationId "com.example.recyclerviewtest"
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'

  

  第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

同步一下:

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

布局添加:

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

 <android.support.v7.widget.RecyclerView
android:id="@_id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

  第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

子布局 fruit_item.xml

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

<?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:layout_margin="5dp" > <ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" /> <TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="10dp" />
</LinearLayout>

  第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

填充内容-Fruit类

package com.espressif.activitytest;
public class Fruit { private String name; private int imageId; public Fruit(String name, int imageId) {
this.name = name;
this.imageId = imageId;
} public String getName() {
return name;
} public int getImageId() {
return imageId;
} }

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

下面代码多了 按键动作

package com.espressif.activitytest;

/**
* Created by dongdong on 2018/6/18.
*/ import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; import java.util.ArrayList;
import java.util.List; public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>{ private List<Fruit> mFruitList;
private List<Fruit> fruitList = new ArrayList<Fruit>();
static class ViewHolder extends RecyclerView.ViewHolder {
View fruitView;
ImageView fruitImage;
TextView fruitName; public ViewHolder(View view) {
super(view);
fruitView = view;
fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
fruitName = (TextView) view.findViewById(R.id.fruit_name);
}
} public FruitAdapter(List<Fruit> fruitList) {
mFruitList = fruitList;
} @Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);
final ViewHolder holder = new ViewHolder(view);
holder.fruitView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
Fruit fruit = mFruitList.get(position);
Toast.makeText(v.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 = mFruitList.get(position);
Toast.makeText(v.getContext(), "you clicked image " + fruit.getName(), Toast.LENGTH_SHORT).show();
}
});
return holder;
} @Override
public void onBindViewHolder(ViewHolder holder, int position) {
Fruit fruit = mFruitList.get(position);
holder.fruitImage.setImageResource(fruit.getImageId());
holder.fruitName.setText(fruit.getName());
} @Override
public int getItemCount() {
return mFruitList.size();
} }

  

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

package com.example.recyclerviewtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager; import java.util.ArrayList;
import java.util.List;
import java.util.Random; public class MainActivity extends AppCompatActivity { private List<Fruit> fruitList = new ArrayList<Fruit>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
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 < 2; 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();
} }

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView


第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

<?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:layout_margin="5dp" > <ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" /> <TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="10dp" />
</LinearLayout>

  

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

package com.example.recyclerviewtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager; import java.util.ArrayList;
import java.util.List;
import java.util.Random; public class MainActivity extends AppCompatActivity { private List<Fruit> fruitList = new ArrayList<Fruit>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
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 < 2; 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();
} }

  

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

package com.espressif.activitytest;

/**
* Created by dongdong on 2018/6/18.
*/ import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; import java.util.ArrayList;
import java.util.List; public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>{ private List<Fruit> mFruitList;
private List<Fruit> fruitList = new ArrayList<Fruit>();
static class ViewHolder extends RecyclerView.ViewHolder {
View fruitView;
ImageView fruitImage;
TextView fruitName; public ViewHolder(View view) {
super(view);
fruitView = view;
fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
fruitName = (TextView) view.findViewById(R.id.fruit_name);
}
} public FruitAdapter(List<Fruit> fruitList) {
mFruitList = fruitList;
} @Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);
final ViewHolder holder = new ViewHolder(view);
holder.fruitView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
Fruit fruit = mFruitList.get(position);
Toast.makeText(v.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 = mFruitList.get(position);
Toast.makeText(v.getContext(), "you clicked image " + fruit.getName(), Toast.LENGTH_SHORT).show();
}
});
return holder;
} @Override
public void onBindViewHolder(ViewHolder holder, int position) {
Fruit fruit = mFruitList.get(position);
holder.fruitImage.setImageResource(fruit.getImageId());
holder.fruitName.setText(fruit.getName());
} @Override
public int getItemCount() {
return mFruitList.size();
} }

  

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView

第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView第一行代码 -3-2 软件也要拼脸蛋-UI界面-更强大的滚动条-RecyclerView