RecyclerView是谷歌推出的代替ListView的列表控件。
1.导入依赖,在build.gradle下添加recyclerview的依赖
implementation 'com.android.support:recyclerview-v7:27.0.0'
2.进行使用
效果图:
package com.example.lineralayout.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.lineralayout.R;
public class RecyclerviewActivity extends AppCompatActivity {
private Button mBtnLinear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerview);
mBtnLinear = findViewById(R.id.btn_linear);
mBtnLinear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(RecyclerviewActivity.this,LinearRecyclerViewActivity.class);
startActivity(intent);
}
});
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".recyclerview.RecyclerviewActivity">
<Button
android:id="@+id/btn_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="列表视图"/>
</LinearLayout>
Linear类
package com.example.lineralayout.recyclerview;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import com.example.lineralayout.R;
public class LinearRecyclerViewActivity extends AppCompatActivity {
private RecyclerView mRvMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linear_recycler_view);
mRvMain = findViewById(R.id.rv_main);
mRvMain.setLayoutManager(new LinearLayoutManager(LinearRecyclerViewActivity.this));
mRvMain.addItemDecoration(new MyDecoration());
mRvMain.setAdapter(new LinearAdapter(LinearRecyclerViewActivity.this));
}
class MyDecoration extends RecyclerView.ItemDecoration{
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(0,0,0,getResources().getDimensionPixelOffset(R.dimen.dividerHeight));
}
}
}
对应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=".recyclerview.LinearRecyclerViewActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorGrayDark"/>
</RelativeLayout>
recyclerview分割线的方法,
.addItemDeciration
##RecyclerView.Adapter适配器RecyclerView.Adapter
,一个抽象类,并支持泛型
public static abstract class Adapter<VH extends ViewHolder> {
...
}
重写的三个方法:
1:public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
2:public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
3:public int getItemCount()
##点击事件(注:也可以自定义一个接口来实现)
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext,"click..."+position,Toast.LENGTH_SHORT).show();
}
});