1、步骤
2、实例demo
- 导入依赖包
dependencies {
implementation 'androidx.recyclerview:recyclerview:1.2.1'
}
- 在主布局创建recyclerView控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".recyclerView.RecyclerViewActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
- 创建item布局
<?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="match_parent">
<TextView
android:id="@+id/recycler_v"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"/>
</LinearLayout>
- 创建爱你Bean类
package com.example.viewexample.recyclerView;
public class RecyclerViewBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- 创建Adapter类
package com.example.viewexample.recyclerView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.viewexample.R;
import com.example.viewexample.listView.MyAdapter;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private List<RecyclerViewBean> data;
private Context mContext;
public RecyclerViewAdapter(List<RecyclerViewBean> data, Context context) {
this.data = data;
mContext = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = View.inflate(mContext, R.layout.recycler_view, null);
return new MyViewHolder(inflate);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.tv.setText(data.get(position).getName());
}
@Override
public int getItemCount() {
return data == null ? 0 : data.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tv;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.recycler_v);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnRecyclerItemClickListener != null){
mOnRecyclerItemClickListener.onRecyclerItemClick(getAdapterPosition());
}
}
});
}
}
public interface OnRecyclerItemClickListener{
void onRecyclerItemClick(int position);
}
private OnRecyclerItemClickListener mOnRecyclerItemClickListener;
public void setOnRecyclerItemClickListener(OnRecyclerItemClickListener listener){
mOnRecyclerItemClickListener = listener;
}
}
- 在MainActivity书写实际代码
package com.example.viewexample.recyclerView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.example.viewexample.R;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewActivity extends AppCompatActivity {
private static final String TAG = "RecyclerViewActivity";
private List<RecyclerViewBean> data = new ArrayList<>();
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
for (int i = 0; i < 100; i++) {
RecyclerViewBean bean = new RecyclerViewBean();
bean.setName("RecyclerView子元素"+i);
data.add(bean);
}
mRecyclerView = findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(data,this);
mRecyclerView.setAdapter(adapter);
adapter.setOnRecyclerItemClickListener(new RecyclerViewAdapter.OnRecyclerItemClickListener() {
@Override
public void onRecyclerItemClick(int position) {
Log.d(TAG, "onRecyclerItemClick: "+position);
}
});
}
}
3、效果图
4、java.lang.IllegalStateException错误
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = View.inflate(mContext, R.layout.recycler_view, parent);
return new MyViewHolder(inflate);
}
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.viewexample, PID: 9482
java.lang.IllegalStateException: ViewHolder views must not be attached when created. Ensure that you are not passing 'true' to the attachToRoot parameter of LayoutInflater.inflate(..., boolean attachToRoot)
at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7297)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6416)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4309)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4012)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4578)
at android.view.View.layout(View.java:23141)
at android.view.ViewGroup.layout(ViewGroup.java:6412)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:23141)
at android.view.ViewGroup.layout(ViewGroup.java:6412)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23141)
at android.view.ViewGroup.layout(ViewGroup.java:6412)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:530)
at android.view.View.layout(View.java:23141)
at android.view.ViewGroup.layout(ViewGroup.java:6412)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23141)
at android.view.ViewGroup.layout(ViewGroup.java:6412)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:23141)
at android.view.ViewGroup.layout(ViewGroup.java:6412)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:798)
at android.view.View.layout(View.java:23141)
at android.view.ViewGroup.layout(ViewGroup.java:6412)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3681)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3140)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2126)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8649)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1037)
at android.view.Choreographer.doCallbacks(Choreographer.java:845)
at android.view.Choreographer.doFrame(Choreographer.java:780)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1022)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7822)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
I/Process: Sending signal. PID: 9482 SIG: 9
5、自定义监听
public interface OnRecyclerItemClickListener{
void onRecyclerItemClick(int position);
}
private OnRecyclerItemClickListener mOnRecyclerItemClickListener;
public void setOnRecyclerItemClickListener(OnRecyclerItemClickListener listener){
mOnRecyclerItemClickListener = listener;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tv;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.recycler_v);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnRecyclerItemClickListener != null){
mOnRecyclerItemClickListener.onRecyclerItemClick(getAdapterPosition());
}
}
});
}
}
6、方法流程图
图示:RecyclerViewAdapter_onCreateViewHolder如下 图示:RecyclerViewActivity_onCreate如下
7、官方文献
|