Android RecyclerView学习使用(一)

我使用的理由:

1、更强大的滚动空间
2、自定义权限够你free style
3、优化了ListView的各种不足
4、官方推荐 (亲儿子)

使用控件,还是遵循 数据源 、初始化控件、控件适配的顺序

基本用法

1、RecyclerView定义在了support库中,所以需要在项目中gradle中添加相应的依赖,“’com.android.support:design:26-alph
a1”

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'cn.jzvd:jiaozivideoplayer:6.2.10'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:design:26.0.0-alpha1'
}

2、创建Activilty

StudyRecverViewActivity.java

package com.bruce.chenxh.tesyapplication.acty;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.bruce.chenxh.tesyapplication.R;
import com.bruce.chenxh.tesyapplication.adapter.StudyRecyclerViewAdapter;
import com.bruce.chenxh.tesyapplication.entry.Fruit;

import java.util.ArrayList;
import java.util.List;

public class StudyRecverViewActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List<Fruit> mFruitList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_study_recver_view);
        initView();
        initData();
        initControl();
    }

    private void initControl() {
        StudyRecyclerViewAdapter adapter = new StudyRecyclerViewAdapter(mFruitList);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new GridLayoutManager(this,3));
    }
    private void initData() {
        mFruitList = new ArrayList<>();
        for (int i =0;i<20;i++){
            mFruitList.add(new Fruit("从喜欢到爱",R.mipmap.ic_launcher,"从第三页到杜姝燕"));
        }
    }
    private void initView() {
        recyclerView = findViewById(R.id.study_recycler_view);
    }
}

界面代码:
activity_study_recver_view.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible"
    tools:context="com.bruce.chenxh.tesyapplication.acty.StudyRecverViewActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/study_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</android.support.constraint.ConstraintLayout>

3、编写适配器,以及item的界面代码:

recycler_view_item1.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="200dp"
    android:gravity="center_horizontal"
    >
    <TextView
        android:id="@+id/study_recycler_view_name"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:gravity="center_horizontal"
        />
    <ImageView
        android:id="@+id/study_recycler_view_img"
        android:layout_width="match_parent"
        android:layout_height="130dp" />
    <TextView
        android:id="@+id/study_recycler_view_color"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center_horizontal"
        />
</LinearLayout>

适配器代码:
StudyRecyclerViewAdapter.java

package com.bruce.chenxh.tesyapplication.adapter;

import android.content.Context;
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 com.bruce.chenxh.tesyapplication.R;
import com.bruce.chenxh.tesyapplication.entry.Fruit;

import java.util.List;

/**
 * Created by BruceXuheng on 2018/5/30 21:06.
 * Description:
 */

public class StudyRecyclerViewAdapter extends RecyclerView.Adapter<StudyRecyclerViewAdapter.ViewHolder> {

    private List<Fruit> mFruitList;

    public StudyRecyclerViewAdapter(List<Fruit> fruitList) {
        mFruitList = fruitList;
    }

    //    创建View View Holder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item1,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Fruit fruit = mFruitList.get(position);
        holder.name.setText(fruit.getName());
        holder.color.setText(fruit.getColor());
        holder.img.setImageResource(fruit.getImgUrl());
    }

//    Item 个数
    @Override
    public int getItemCount() {
        return mFruitList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView name,color;
        ImageView img;
        public ViewHolder(View itemView) {
            super(itemView);
            this.name = itemView.findViewById(R.id.study_recycler_view_name);
            this.color = itemView.findViewById(R.id.study_recycler_view_color);
            this.img = itemView.findViewById(R.id.study_recycler_view_img);
        }
    }

}

4、实体类 用于传值:

Fruit.java

package com.bruce.chenxh.tesyapplication.entry;

/**
 * Created by BruceXuheng on 2018/5/30 20:51.
 * Description:
 */

public class Fruit {

    private String name;
    private int imgUrl;
    private String color;

    public Fruit(String name, int imgUrl, String color) {
        this.name = name;
        this.imgUrl = imgUrl;
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(int imgUrl) {
        this.imgUrl = imgUrl;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

5、最主要是Acitivity中的适配使用方法:

1)这是设置网格类型的,自动适配,什么瀑布流状==

private void initControl() {
        StudyRecyclerViewAdapter adapter = new StudyRecyclerViewAdapter(mFruitList);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new GridLayoutManager(this,3));
    }

2)这样可以设置列表样式

LinearLayoutManager( Context context, int orientation, boolean reverseLayout)
参数分别是 :
上下文
方向(LinearLayoutManager.HORIZONTAL,LinearLayoutManager.VERTICAL);
是否翻转(false)

private void initControl() {
        StudyRecyclerViewAdapter adapter = new StudyRecyclerViewAdapter(mFruitList);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,false));
    }

下一篇来体验一下RecyclerView多布局。

猜你喜欢

转载自blog.csdn.net/Mr_ChenXu/article/details/80517627