RecyclerView 实现一百个格子进度
时间: 2023-09-01 14:06:24 浏览: 48
要实现一百个格子的进度,可以通过RecyclerView来实现。下面是实现的步骤:
1. 首先在布局文件中添加一个RecyclerView控件,设置其方向为垂直方向。
2. 创建一个Adapter类继承RecyclerView.Adapter,实现其中的onCreateViewHolder、onBindViewHolder和getItemCount方法。
3. 在Adapter的onCreateViewHolder方法中创建一个ViewHolder对象,该对象包含一个TextView用于显示进度。
4. 在Adapter的onBindViewHolder方法中根据position设置对应ViewHolder的TextView的文字内容,可以用随机数模拟进度。
5. 在Activity或Fragment中获取RecyclerView控件,并设置其Adapter为上一步创建的Adapter对象。
6. 最后运行程序即可看到一百个格子的进度。
以下是示例代码实现:
Adapter代码:
```java
public class ProgressAdapter extends RecyclerView.Adapter<ProgressAdapter.ViewHolder> {
private int mCount;
public ProgressAdapter(int count) {
mCount = count;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_progress, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.mProgressText.setText("进度:" + new Random().nextInt(101) + "%");
}
@Override
public int getItemCount() {
return mCount;
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView mProgressText;
ViewHolder(@NonNull View itemView) {
super(itemView);
mProgressText = itemView.findViewById(R.id.progress_text);
}
}
}
```
布局文件item_progress.xml:
```xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progress_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="16dp"
android:textSize="20sp" />
```
Activity中的代码:
```java
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
ProgressAdapter adapter = new ProgressAdapter(100);
mRecyclerView.setAdapter(adapter);
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)