我知道RecyclerView已经取代了旧的ListView和GridView的功能。我正在寻找一个非常基本的例子,显示了一个最小的网格设置使用RecyclerView。我并不是在寻找冗长的教程风格的解释,只是一个简单的例子。我想象最简单的网格,模仿旧的GridView将包括以下功能:
每行多个单元格 每个单元格中的单个视图 响应点击事件
我知道RecyclerView已经取代了旧的ListView和GridView的功能。我正在寻找一个非常基本的例子,显示了一个最小的网格设置使用RecyclerView。我并不是在寻找冗长的教程风格的解释,只是一个简单的例子。我想象最简单的网格,模仿旧的GridView将包括以下功能:
每行多个单元格 每个单元格中的单个视图 响应点击事件
当前回答
如果你想在reyclerview的XML文件中设置网格布局,那么你可以把这两个放在recyclerview XML中。
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount=numberOfItemsInSignleRow
如果你想从Java代码中设置网格布局,你可以这样写。
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), numberOfItemsInSignleRow));
其他回答
如果你想在reyclerview的XML文件中设置网格布局,那么你可以把这两个放在recyclerview XML中。
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount=numberOfItemsInSignleRow
如果你想从Java代码中设置网格布局,你可以这样写。
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), numberOfItemsInSignleRow));
在RecyclerView初始化中设置
recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
你应该将你的RecyclerView LayoutManager设置为Gridlayout模式。当你想要设置你的RecyclerView LayoutManager时,只需更改你的代码:
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), numberOfColumns));
虽然我喜欢并欣赏Suragch的回答,但我想留下一个提示,因为我发现编码适配器(MyRecyclerViewAdapter)来定义和公开监听器方法onItemClick并不是最好的方法,因为没有正确使用类封装。因此,我的建议是让适配器单独处理监听操作(这是他的目的!),并将这些操作与使用适配器的活动(MainActivity)分开。这就是我如何设置Adapter类:
MyRecyclerViewAdapter.java
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
private String[] mData = new String[0];
private LayoutInflater mInflater;
// Data is passed into the constructor
public MyRecyclerViewAdapter(Context context, String[] data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
}
// Inflates the cell layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
// Binds the data to the textview in each cell
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String animal = mData[position];
holder.myTextView.setText(animal);
}
// Total number of cells
@Override
public int getItemCount() {
return mData.length;
}
// Stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView myTextView;
public ViewHolder(View itemView) {
super(itemView);
myTextView = (TextView) itemView.findViewById(R.id.info_text);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
onItemClick(view, getAdapterPosition());
}
}
// Convenience method for getting data at click position
public String getItem(int id) {
return mData[id];
}
// Method that executes your code for the action received
public void onItemClick(View view, int position) {
Log.i("TAG", "You clicked number " + getItem(position).toString() + ", which is at cell position " + position);
}
}
请注意,现在在MyRecyclerViewAdapter中定义的onItemClick方法是您想要为接收到的事件/操作编码任务的地方。
为了完成这个转换,只需要做一个小的更改:Activity不需要实现MyRecyclerViewAdapter。ItemClickListener,因为现在完全由适配器完成。这将是最后的修改:
MainActivity.java
public class MainActivity extends AppCompatActivity {
MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// data to populate the RecyclerView with
String[] data = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"};
// set up the RecyclerView
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvNumbers);
int numberOfColumns = 6;
recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
adapter = new MyRecyclerViewAdapter(this, data);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
}
}
这是一种仅使用XML的简单方法
spanCount表示列数
layoutManager使它成为网格或线性(垂直或水平)
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/personListRecyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />