RecyclerView
1. 和百分比布局类似,属于新增控件,所以先打开app/build.gradle文件,在dependencies闭包中添加如下内容
这里我们要用RecyclerView实现和ListView一样的效果,直接将之前ListView中的Fruit类和fruit_item.xml文件直接复制过来,与ListView不同的是适配器
2. 为RecyclerView准备新的适配器,新建FruitAdapter类,继承RecyclerView.Adapter类
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
private List<Fruit> mFruitList;
static 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){
mFruitList = fruitList;
}
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;
}
public void onBindViewHolder(ViewHolder holder,int position){
Fruit fruit = mFruitList.get(position);
holder.fruitImage.setImageResource(fruit.getImageId());
holder.fruitName.setText(fruit.getName());
}
public int getItemCount(){
return mFruitList.size();
}
}
由于FruitAdapter继承自RecyclerView.Adapter所以就必须重写onCreateViewHolder(),onBindViewHolder(),getItemCont()这三个方法。
onCreateViewHolder():这个方法是用于创建ViewHolder实例的,在这个方法中将fruit_item.xml布局加载进来,然后创建一个ViewHolder实例,并将加载出来的布局传入到构造函数中去,最后将ViewHolder实例返回。
onBindViewHolder():这个方法用于对RecyclerView的子项的数据进行赋值,会在每个子项被滚动到屏幕内的时候执行,这里通过posititon得到当前项的Fruit实例,然后再将数据设置到ViewHolder的ImageView和TextView当中即可。
getItemCount():该方法用于告诉RecyclerView一共有多少子项,直接返回数据源的长度即可。
3. 使用
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)findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
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("apple",R.mipmap.ic_launcher_round);
fruitlist.add(apple);
Fruit banana = new Fruit("banana",R.mipmap.ic_launcher_round);
fruitlist.add(banana);
Fruit orange = new Fruit("orange",R.mipmap.ic_launcher_round);
fruitlist.add(orange);
Fruit pear = new Fruit("pear",R.mipmap.ic_launcher_round);
fruitlist.add(pear);
Fruit grape = new Fruit("grape",R.mipmap.ic_launcher_round);
fruitlist.add(grape);
Fruit pineapple = new Fruit("pineapple",R.mipmap.ic_launcher_round);
fruitlist.add(pineapple);
Fruit cherry = new Fruit("cherry",R.mipmap.ic_launcher_round);
fruitlist.add(cherry);
Fruit mengo = new Fruit("mengo",R.mipmap.ic_launcher_round);
fruitlist.add(mengo);
}
}
}
同样,先初始化所有的数据,接着在onCreat()方法中先获取RecyclerView实例,然后创建一个LayoutManager对象,并将它设置在RecyclerView中。LayoutManager用于设置RecyclerView的布局方式,这里使用LinearLayoutManager是线性布局的方式,可以实现ListView类似的效果,接下来创建FruitAdapter实例,将数据传入,最后调用RecyclerView的setAdapter()方法完成适配器设置
4. 实现横向滚动
只需修改如上代码即可
5. 实现瀑布流布局
最主要的是更换layoutManager为StaggeredGridLayoutManager
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
第一个参数用于指定布局的列数,传入3表示会把布局分为3列,第二个参数用于指定布局的排列方向
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.mipmap.ic_launcher_round);
fruitlist.add(apple);
Fruit banana = new Fruit(getRandomLengthName("banana"),R.mipmap.ic_launcher_round);
fruitlist.add(banana);
Fruit orange = new Fruit(getRandomLengthName("orange"),R.mipmap.ic_launcher_round);
fruitlist.add(orange);
Fruit pear = new Fruit(getRandomLengthName("pear"),R.mipmap.ic_launcher_round);
fruitlist.add(pear);
Fruit grape = new Fruit(getRandomLengthName("grape"),R.mipmap.ic_launcher_round);
fruitlist.add(grape);
Fruit pineapple = new Fruit(getRandomLengthName("pineapple"),R.mipmap.ic_launcher_round);
fruitlist.add(pineapple);
Fruit cherry = new Fruit(getRandomLengthName("cherry"),R.mipmap.ic_launcher_round);
fruitlist.add(cherry);
Fruit mengo = new Fruit(getRandomLengthName("mengo"),R.mipmap.ic_launcher_round);
fruitlist.add(mengo);
}
}
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();
}
getRandomLengthName()这个方法是为了让字符串的长度不一,来直观体现瀑布流布局