安卓开发 原生View 帮我写完整代码 垂直recyclerview 嵌套水平recyclerview
时间: 2024-03-01 13:55:03 浏览: 88
好的,下面是一个垂直的 RecyclerView 嵌套水平的 RecyclerView 的完整代码:
在布局文件中添加以下代码:
```xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/verticalRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
在 Java 文件中添加以下代码:
```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.verticalRecyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
mRecyclerView.setAdapter(new VerticalAdapter());
}
private class VerticalAdapter extends RecyclerView.Adapter<VerticalAdapter.VerticalViewHolder> {
@NonNull
@Override
public VerticalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_vertical, parent, false);
return new VerticalViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull VerticalViewHolder holder, int position) {
holder.mHorizontalRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false));
holder.mHorizontalRecyclerView.setAdapter(new HorizontalAdapter());
}
@Override
public int getItemCount() {
return 10;
}
class VerticalViewHolder extends RecyclerView.ViewHolder {
RecyclerView mHorizontalRecyclerView;
VerticalViewHolder(@NonNull View itemView) {
super(itemView);
mHorizontalRecyclerView = itemView.findViewById(R.id.horizontalRecyclerView);
}
}
}
private class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.HorizontalViewHolder> {
@NonNull
@Override
public HorizontalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_horizontal, parent, false);
return new HorizontalViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull HorizontalViewHolder holder, int position) {
holder.mTextView.setText(String.valueOf(position + 1));
}
@Override
public int getItemCount() {
return 5;
}
class HorizontalViewHolder extends RecyclerView.ViewHolder {
TextView mTextView;
HorizontalViewHolder(@NonNull View itemView) {
super(itemView);
mTextView = itemView.findViewById(R.id.textView);
}
}
}
}
```
在 res/layout/ 目录下创建以下布局文件:
activity_main.xml:
```xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/verticalRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
item_vertical.xml:
```xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/horizontalRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
```
item_horizontal.xml:
```xml
<TextView
android:id="@+id/textView"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/colorAccent"
android:gravity="center"
android:textColor="@android:color/white" />
```
这样就完成了一个垂直的 RecyclerView 嵌套水平的 RecyclerView。
阅读全文