1、先创建 activity_recycle_view.xml 和 activity_recycler_linear_item.xml
如下:
xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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=".RecycleViewActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/re1" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteX="202dp" tools:layout_editor_absoluteY="184dp" /> androidx.constraintlayout.widget.ConstraintLayout>
xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content" tools:context=".RecyclerLinearItemActivity" android:background="@color/colorBg"> <TextView android:id="@+id/textView2" android:layout_width="0dp" android:layout_height="60dp" android:text="@string/textview" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> androidx.constraintlayout.widget.ConstraintLayout>
2、创建LinearAdapter 适配器:
package com.example.myapplication; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; public class LinearAdapter extends RecyclerView.Adapter{ private Context context; public LinearAdapter(Context context){ this.context = context; } @NonNull @Override public LinearAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new LinearViewHolder(LayoutInflater.from(context).inflate(R.layout.activity_recycler_linear_item,parent,false)); } @Override public void onBindViewHolder(@NonNull LinearAdapter.LinearViewHolder holder, final int position) { holder.tv.setText("Hello World"); holder.tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(context,"click..."+position,Toast.LENGTH_SHORT).show(); } }); } @Override public int getItemCount() { return 30; } class LinearViewHolder extends RecyclerView.ViewHolder{ private TextView tv; public LinearViewHolder(@NonNull View itemView) { super(itemView); tv = itemView.findViewById(R.id.textView2); } } }
3、RecycleViewActivity.java 代码
package com.example.myapplication; 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; public class RecycleViewActivity extends AppCompatActivity { private RecyclerView rv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycle_view); rv = findViewById(R.id.re1); rv.setLayoutManager(new LinearLayoutManager(RecycleViewActivity.this)); //设置分割线 rv.addItemDecoration( new MyDecoration()); rv.setAdapter(new LinearAdapter(RecycleViewActivity.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.height)); } } }
4、效果如下: