android studio写一段点击有着2个recyclerView类型的xml页面跳转到新的页面并显示读取数据并显示,请输出全部的代码
时间: 2024-05-03 15:22:32 浏览: 152
以下是一个示例代码,其中有两个RecyclerView类型的XML页面,点击其中一个将跳转到新的页面并显示数据:
activity_main.xml:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
```
MainActivity.java:
```java
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView1;
private RecyclerView recyclerView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView1 = findViewById(R.id.recyclerView1);
recyclerView2 = findViewById(R.id.recyclerView2);
// 设置RecyclerView1的布局管理器和适配器
RecyclerView.LayoutManager layoutManager1 = new LinearLayoutManager(this);
recyclerView1.setLayoutManager(layoutManager1);
RecyclerView.Adapter adapter1 = new MyAdapter1();
recyclerView1.setAdapter(adapter1);
// 设置RecyclerView2的布局管理器和适配器
RecyclerView.LayoutManager layoutManager2 = new LinearLayoutManager(this);
recyclerView2.setLayoutManager(layoutManager2);
RecyclerView.Adapter adapter2 = new MyAdapter2();
recyclerView2.setAdapter(adapter2);
// 设置RecyclerView1和RecyclerView2的点击事件
recyclerView1.addOnItemTouchListener(new RecyclerItemClickListener(this, recyclerView1, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
// 点击RecyclerView1,跳转到新的页面并显示数据
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("data", "RecyclerView1: " + position);
startActivity(intent);
}
@Override
public void onLongItemClick(View view, int position) {
// do nothing
}
}));
recyclerView2.addOnItemTouchListener(new RecyclerItemClickListener(this, recyclerView2, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
// 点击RecyclerView2,跳转到新的页面并显示数据
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("data", "RecyclerView2: " + position);
startActivity(intent);
}
@Override
public void onLongItemClick(View view, int position) {
// do nothing
}
}));
}
// RecyclerView1的适配器
private class MyAdapter1 extends RecyclerView.Adapter<MyViewHolder1> {
@NonNull
@Override
public MyViewHolder1 onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout1, parent, false);
return new MyViewHolder1(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder1 holder, int position) {
holder.textView.setText("Item " + position);
}
@Override
public int getItemCount() {
return 10;
}
}
// RecyclerView1的ViewHolder
private class MyViewHolder1 extends RecyclerView.ViewHolder {
private TextView textView;
public MyViewHolder1(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView1);
}
}
// RecyclerView2的适配器
private class MyAdapter2 extends RecyclerView.Adapter<MyViewHolder2> {
@NonNull
@Override
public MyViewHolder2 onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout2, parent, false);
return new MyViewHolder2(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder2 holder, int position) {
holder.textView.setText("Item " + position);
}
@Override
public int getItemCount() {
return 10;
}
}
// RecyclerView2的ViewHolder
private class MyViewHolder2 extends RecyclerView.ViewHolder {
private TextView textView;
public MyViewHolder2(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView2);
}
}
}
```
activity_second.xml:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/dataTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
```
SecondActivity.java:
```java
public class SecondActivity extends AppCompatActivity {
private TextView dataTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
dataTextView = findViewById(R.id.dataTextView);
String data = getIntent().getStringExtra("data");
dataTextView.setText(data);
}
}
```
阅读全文