RecyclerView
RecyclerView能够灵活的实现大数据集的展示,视图的复用管理比ListView更好,能够显示列表、网格、瀑布流等形式,且不同的ViewHolder能够实现item多元化的功能。
但是使用起来会稍微麻烦一点,并且没有类似ListView的onItemClickListener监听事件,需要开发者自己实现。
1.线性布局
线性布局效果图RecyclerView
activity_linear_recycler_view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_main"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
RecyclerViewItem
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textSize="20sp"
android:textColor="#000"/>
</LinearLayout>
Activity
LinearRecyclerViewActivity
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, new LinearAdapter.OnItemClickListener() {
@Override
public void onClick(int pos) {
Toast.makeText(LinearRecyclerViewActivity.this, "回调点击:" + pos, Toast.LENGTH_SHORT).show();
}
}));
}
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));
}
}
}
Adapter
LinearAdapter
public class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.LinearViewHolder> {
private Context mContext;
private OnItemClickListener mListener;
public LinearAdapter(Context context, OnItemClickListener listener) {
mContext = context;
mListener = listener;
}
@NonNull
@Override
public LinearAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull LinearAdapter.LinearViewHolder holder, final int position) {
holder.textView.setText("Hello World!");
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onClick(position);
// Toast.makeText(mContext, "点击:" + position, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return 30;
}
class LinearViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public LinearViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv_title);
}
}
public interface OnItemClickListener {
void onClick(int pos);
}
}
2.水平布局
水平布局效果图RecyclerView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_hor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorGray">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
RecyclerViewItem
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorWhite">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center"
android:textSize="20sp"
android:textColor="#000"/>
</LinearLayout>
HorAdapter
public class HorAdapter extends RecyclerView.Adapter<HorAdapter.LinearViewHolder> {
private Context mContext;
private OnItemClickListener mListener;
public HorAdapter(Context context, OnItemClickListener listener) {
mContext = context;
mListener = listener;
}
@NonNull
@Override
public HorAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_hor_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull HorAdapter.LinearViewHolder holder, final int position) {
holder.textView.setText("Hello!");
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onClick(position);
// Toast.makeText(mContext, "点击:" + position, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return 30;
}
class LinearViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public LinearViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv_title);
}
}
public interface OnItemClickListener {
void onClick(int pos);
}
}
HorRecyclerViewActivity
public class HorRecyclerViewActivity extends AppCompatActivity {
private RecyclerView mRvHor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hor_recycler_view);
mRvHor = findViewById(R.id.rv_hor);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(HorRecyclerViewActivity.this);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mRvHor.setLayoutManager(linearLayoutManager);
mRvHor.addItemDecoration(new MyDecoration());
mRvHor.setAdapter(new HorAdapter(HorRecyclerViewActivity.this, new HorAdapter.OnItemClickListener() {
@Override
public void onClick(int pos) {
Toast.makeText(HorRecyclerViewActivity.this, "Click:"+pos, Toast.LENGTH_SHORT).show();
}
}));
}
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,getResources().getDimensionPixelOffset(R.dimen.dividerHeight),0);
}
}
}
3.网格布局
网格布局效果图GridRecyclerView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
GridRecyclerViewItem
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorWhite">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center"
android:textSize="20sp"
android:textColor="#000"/>
</LinearLayout>
GridAdapter
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.LinearViewHolder> {
private Context mContext;
private OnItemClickListener mListener;
public GridAdapter(Context context, OnItemClickListener listener) {
mContext = context;
mListener = listener;
}
@NonNull
@Override
public GridAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_grid_recycler_view_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull GridAdapter.LinearViewHolder holder, final int position) {
holder.textView.setText("Hello grid!");
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onClick(position);
// Toast.makeText(mContext, "点击:" + position, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return 30;
}
class LinearViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public LinearViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv_title);
}
}
public interface OnItemClickListener {
void onClick(int pos);
}
}
GridRecyclerViewActivity
public class GridRecyclerViewActivity extends AppCompatActivity {
private RecyclerView mRvGrid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_recycler_view);
mRvGrid = findViewById(R.id.rv_grid);
mRvGrid.setLayoutManager(new GridLayoutManager(GridRecyclerViewActivity.this, 3));
mRvGrid.setAdapter(new GridAdapter(GridRecyclerViewActivity.this, new GridAdapter.OnItemClickListener() {
@Override
public void onClick(int pos) {
Toast.makeText(GridRecyclerViewActivity.this, "点击:" + pos, Toast.LENGTH_SHORT).show();
}
}));
}
}
网友评论