RecyclerView 시리즈 : 오픈 소스 프레임 워크 BaseRecyclerViewAdapterHelper 사용

  • 이것은 간단한 데모입니다. AndroidX를 사용하여 효과를 확인하십시오.
  • 사용하는 간단한 단계 :
  • 프로젝트 디렉토리의 build.gradle에서 jitpack 주소를 추가해야합니다. 그렇지 않으면 BaseRecyclerViewAdapterHelper 라이브러리가 인식되지 않습니다.
allprojects {
    repositories {
       ......
        maven { url "https://jitpack.io" }
    }
}
  • app 디렉토리의 build.gradle :
android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        ......
        minSdkVersion 16
        targetSdkVersion 30
  	   ......
    }
    ......
}
dependencies {
 	......

    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.40'

}
  • MainActivity 코드 :
public class MainActivity extends AppCompatActivity {
    RecyclerView rv;
    NameAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(MainActivity.this));

        ArrayList<String> nameList = new ArrayList<>();
        nameList.add("刘德华");
        nameList.add("张学友");
        nameList.add("黎明");
        nameList.add("郭富城");
        nameList.add("任达华");
        nameList.add("洪金宝");
        nameList.add("周星驰");
        nameList.add("成龙");

        adapter = new NameAdapter(R.layout.itme_name, nameList);
        rv.setAdapter(adapter);
    }
}

NameAdapter 코드 :

public class NameAdapter extends SimpleQuickAdapter<String> {
    public NameAdapter(int layoutId, @Nullable List<String> data) {
        super(layoutId, data);
    }

    @Override
    public int getItemLayoutId() {
        return R.layout.itme_name;
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        helper.setText(R.id.tv_name, item);
    }
}

SimpleQuickAdapter는 NameAdapter를 단순화하기위한 것일 뿐이며 일반 BaseViewHolder를 전달하는 것을 종종 잊어 버립니다. SimpleQuickAdapter 코드.

public abstract class SimpleQuickAdapter<T> extends BaseQuickAdapter<T, BaseViewHolder> {
    /**
     * @return 此方法只是便于在Adapter的内部,直接看到布局id
     */
    public abstract int getItemLayoutId();


    public SimpleQuickAdapter(int layoutId, @Nullable List<T> data) {
        super(layoutId, data);
    }

}

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=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

item_name.xml 코드 :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/tv_name"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center_vertical"
    android:paddingLeft="10dp"
    android:textColor="#333333"
    android:textSize="16sp"
    tools:text="刘德华">

</TextView>

자세한 내용은 다음을 참조하십시오. RecyclerView 범용 어댑터

추천

출처blog.csdn.net/zhangjin1120/article/details/114175664