RecyclerView
RecyclerView能够灵活的实现大数据集的展示,视图的复用管理比ListView更好,能够显示列表、网格、瀑布流等形式,且不同的ViewHolder能够实现item多元化的功能。
但是使用起来会稍微麻烦一点,并且没有类似ListView的onItemClickListener监听事件,需要开发者自己实现。
1.线性布局
RecyclerView
activity_linear_recycler_view.xml
RecyclerViewItem
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 {
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
RecyclerViewItem
HorAdapter
public class HorAdapter extends RecyclerView.Adapter {
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
GridRecyclerViewItem
GridAdapter
public class GridAdapter extends RecyclerView.Adapter {
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();
}
}));
}
}