張萌&韓墨羽——RecyclerView萬能適配器

RecyclerView萬能適配器

效果如下
在這裏插入圖片描述
一、框架引入
先在項目的 build.gradle(Project:XXXX) 的 repositories 添加:

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

然後在Module的 build.gradle(Module:app) 的 dependencies 添加:

dependencies {
        ......
        compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22'
}

注意: 一旦出現加載失敗的情況,只有兩種情況:

一是:配置沒配置好
配置沒配置好,有幾種情況:

  1. 只配置了 dependencies
  2. 配置 repositories,但是位置錯了,build.gradle(Project:XXXX) 文件下的repositories有兩個,一個是buildscript下面的,一個是allprojects下面的,要配置到allprojects下面纔是對的。
  3. 版本號前面多一個v,這個是我的鍋,在2.1.2版本之前都是帶v的,之後(包含2.1.2)都不需要帶v。

二是:網絡原因

第一步:在佈局文件中引入RecyclerView

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".base.Main4Activity">

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

</RelativeLayout>

第二步:編寫條目佈局文件
item_rv.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp">
    <ImageView
        android:id="@+id/iv_img"
        android:layout_width="150dp"
        android:layout_height="80dp" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@+id/iv_img"
        android:text="我是標題"
        android:textColor="#f00"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_title"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@id/iv_img"
        android:text="我是描述" />

</RelativeLayout>

第三步:編寫數據實體類型
Model.java

package com.example.myrecyclelistview.base;

public class Model {
    private String title;
    private String content;
    private String imgUrl;

    public Model() {
    }

    public Model(String title, String content, String imgUrl) {
        this.title = title;
        this.content = content;
        this.imgUrl = imgUrl;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getImgUrl() {
        return imgUrl;
    }

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

第四步:編寫適配器
MyAdapter.java

package com.example.myrecyclelistview.base;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.myrecyclelistview.R;

import java.util.List;

public class MyAdapter_M4 extends BaseQuickAdapter<Model> {
    public MyAdapter_M4(@LayoutRes int layoutResId, @Nullable List<Model> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, Model item) {
        //可鏈式調用賦值
        helper.setText(R.id.tv_title, item.getTitle())
                .setText(R.id.tv_content, item.getContent())
                .setImageResource(R.id.iv_img, R.mipmap.ic_launcher);

        //獲取當前條目position
        //int position = helper.getLayoutPosition();
    }
}

最後一步:在Activity中使用該適配器
MainActivity.java

package com.example.myrecyclelistview.base;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.myrecyclelistview.MainActivity;
import com.example.myrecyclelistview.MyAdapter;
import com.example.myrecyclelistview.R;

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

public class Main4Activity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private List<Model> datas;
    private MyAdapter_M4 adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);

        //初始化RecyclerView
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        //模擬的數據(實際開發中一般是從網絡獲取的)
        datas = new ArrayList<>();
        Model model;
        for (int i = 0; i < 15; i++) {
            model = new Model();
            model.setTitle("我是第" + i + "條標題");
            model.setContent("第" + i + "條內容");
            datas.add(model);
        }

        //創建佈局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        //創建適配器
        adapter = new MyAdapter_M4(R.layout.item_rv, datas);

        //給RecyclerView設置適配器
        recyclerView.setAdapter(adapter);


        /**
         * 該適配器提供了5種動畫效果(漸顯、縮放、從下到上,從左到右、從右到左)
         *         public static final int ALPHAIN = 0x00000001;
         *         public static final int SCALEIN = 0x00000002;
         *         public static final int SLIDEIN_BOTTOM = 0x00000003;
         *         public static final int SLIDEIN_LEFT = 0x00000004;
         *         public static final int SLIDEIN_RIGHT = 0x00000005;
         */
        //開啓動畫(默認爲漸顯效果)
        adapter.openLoadAnimation();

        /**
         * 使用縮放動畫
         * 更換動畫效果
         */
//        adapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);

        //條目點擊事件
        adapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                Toast.makeText(Main4Activity.this, "點擊了第" + (i + 1) + "條條目", Toast.LENGTH_SHORT).show();
            }
        });
        //條目長按事件
        adapter.setOnRecyclerViewItemLongClickListener(new BaseQuickAdapter.OnRecyclerViewItemLongClickListener() {
            @Override
            public boolean onItemLongClick(View view, int i) {
                Toast.makeText(Main4Activity.this, "長按了第" + (i + 1) + "條條目", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章