写个超简单的RecycleView

为何要写一个超简单的RecycleView呢?主要是我不会用大笑偷笑

在项目的build.gradle中添加相应的依赖库,也可以在自己引入.jar库,打开app/build.gradle文件:

 
 
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    compile 'com.android.support:recyclerview-v7:26.1.0'
}

在activity_main.xml加入RecycleView组件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

</RelativeLayout>

接着修改MainActivity.java代码:

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private LinearLayoutManager mLinearLayoutManager;
    private List<Fruit> mFruitList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruits();
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mLinearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(mLinearLayoutManager);
        //添加adapter适配器
        MyAdapter adapter = new MyAdapter(mFruitList);
        recyclerView.setAdapter(adapter);
    }

    private void initFruits() {
        for (int i = 0; i < 2; i++){
            Fruit apple = new Fruit("apple");
            mFruitList.add(apple);
            Fruit orange = new Fruit("orange");
            mFruitList.add(orange);
            Fruit banana = new Fruit("banana");
            mFruitList.add(banana);
            Fruit watermelon = new Fruit("watermelon");
            mFruitList.add(watermelon);
            Fruit pear = new Fruit("pear");
            mFruitList.add(pear);
            Fruit grape = new Fruit("grape");
            mFruitList.add(grape);
        }
    }
}

编写FruitBean类

public class Fruit {

    private String name;

    public Fruit(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

我自定义一个布局item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#D1EEEE">

    <TextView
        android:id="@+id/friut_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"/>

</LinearLayout>

再写一个适配器

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

    private List<Fruit> mFruitList;

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

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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Fruit fruit = mFruitList.get(position);
        holder.mTextView.setText(fruit.getName());
    }

    @Override
    public int getItemCount() {
        return mFruitList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        private TextView mTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.friut_name);
        }
    }

}
一个简单的RecylerView就完成啦!


猜你喜欢

转载自blog.csdn.net/zingfeng_sky/article/details/80259674
今日推荐