gpt4 book ai didi

android - 如何在一个activity中实现2个或多个recyIclerview

转载 作者:行者123 更新时间:2023-11-30 01:36:00 27 4
gpt4 key购买 nike

如何在一个activity中实现2个或多个recyIclerview?我正在尝试在我的 Activity 中实现 2 个或更多 recyclerview

在此代码行中,我声明了第一个适配器

private RecyclerView.Adapter mAdapter;

如何声明我的第二个适配器?

顺便说一下,在使用以下代码运行我的应用程序时,recyclerview 没有正确滚动和显示

主要 Activity

 private RecyclerView mRecyclerView;
private RecyclerView mRecyclerView1;

private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

private static String LOG_TAG = "RecyclerViewActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
//first recycler
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
// LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
mLayoutManager = new

LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyAdapter(getDataSet());
mRecyclerView.setAdapter(mAdapter);
RecyclerView.ItemDecoration itemDecoration =
new DividerItemDecoration(this, LinearLayoutManager.VERTICAL);
mRecyclerView.addItemDecoration(itemDecoration);
//second recycler
mRecyclerView1 = (RecyclerView) findViewById(R.id.my_recycler_view1);
mRecyclerView1.setHasFixedSize(true);
LinearLayoutManager mLayoutManager1 = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
mRecyclerView1.setLayoutManager(mLayoutManager1);
// mAdapter1 = new MyAdapter1(getDataSet1());
mRecyclerView1.setAdapter(mAdapter1);

我的适配器

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<String> mDataset;

public class ImageViewHolder extends RecyclerView.ViewHolder {
//ImageView mImage;
public TextView txtHeader;
public TextView txtFooter;
public ImageViewHolder(View itemView) {
super (itemView);
txtHeader = (TextView) itemView.findViewById(R.id.firstLine);
txtFooter = (TextView) itemView.findViewById(R.id.secondLine);
}
}



public void add(int position, String item) {
mDataset.add(position, item);
notifyItemInserted(position);
}

public void remove(String item) {
int position = mDataset.indexOf(item);
mDataset.remove(position);
notifyItemRemoved(position);
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<String> myDataset) {
mDataset = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {

// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
// set the view's size, margins, paddings and layout parameters
ImageViewHolder vh = new ImageViewHolder(v);
return vh;
}


private static final int TYPE_IMAGE = 1;
private static final int TYPE_GROUP = 2;

@Override
public int getItemViewType(int position) {
// here your custom logic to choose the view type
return position;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(RecyclerView.ViewHolder TextViewHolder, int position) {


ImageViewHolder viewHolder = (ImageViewHolder) TextViewHolder;
// viewHolder.txtHeader.setText(...)
final String name = mDataset.get(position);
viewHolder.txtHeader.setText(mDataset.get(position));
viewHolder.txtFooter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
remove(name);
}
});

// viewHolder.txtFooter.setText("Footer: " + mDataset.get(position));

}


// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.size();
}

}

我的适配器1

公共(public)类 MyAdapter1 扩展 RecyclerView.Adapter { 私有(private) ArrayList mDataset;

public class ImageViewHolder extends RecyclerView.ViewHolder {
//ImageView mImage;
public TextView txtHeader;
public TextView txtFooter;
public ImageViewHolder(View itemView) {
super (itemView);
txtHeader = (TextView) itemView.findViewById(R.id.firstLine);
txtFooter = (TextView) itemView.findViewById(R.id.secondLine);
}
}



public void add(int position, String item) {
mDataset.add(position, item);
notifyItemInserted(position);
}

public void remove(String item) {
int position = mDataset.indexOf(item);
mDataset.remove(position);
notifyItemRemoved(position);
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter1(ArrayList<String> myDataset) {
mDataset = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {

// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout1, parent, false);
// set the view's size, margins, paddings and layout parameters
ImageViewHolder vh = new ImageViewHolder(v);
return vh;
}


private static final int TYPE_IMAGE = 1;
private static final int TYPE_GROUP = 2;

@Override
public int getItemViewType(int position) {
// here your custom logic to choose the view type
return position;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(RecyclerView.ViewHolder TextViewHolder, int position) {


ImageViewHolder viewHolder = (ImageViewHolder) TextViewHolder;
// viewHolder.txtHeader.setText(...)
final String name = mDataset.get(position);
viewHolder.txtHeader.setText(mDataset.get(position));
viewHolder.txtFooter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
remove(name);
}
});

// viewHolder.txtFooter.setText("Footer: " + mDataset.get(position));

}


// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.size();
}

主要 Activity 布局

    <TextView
android:id="@+id/textView"
android:layout_width="150dp"
android:layout_height="30dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:layout_alignParentTop="true"
android:paddingTop="5dp"
android:text="Recomanded for you"
android:textAppearance="?android:attr/textAppearanceLarge" />

<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="400dp"
android:layout_height="180dp"
android:layout_below="@id/textView"
android:scrollbars="vertical" />


<TextView
android:id="@+id/textView1"
android:layout_width="150dp"
android:layout_height="30dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:layout_alignParentTop="true"
android:paddingTop="5dp"
android:layout_below="@id/my_recycler_view"
android:text=" for you"
android:textAppearance="?android:attr/textAppearanceLarge" />

<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view1"
android:layout_width="400dp"
android:layout_height="180dp"
android:layout_below="@id/textView1"
android:scrollbars="vertical" />
</RelativeLayout>

enter image description here

最佳答案

由于适配器中的代码完全相同,因此您无需声明两个不同的类。您可以只使用一个类并从中创建两个不同的实例,将正确的数据集传递给每个实例以在 RecyclerView 中呈现。

希望对您有所帮助:

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;

private RecyclerView mRecyclerView1;
private RecyclerView.Adapter mAdapter1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);

//first recycler
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(
new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false));
mAdapter = new MyAdapter(getDataSet());
mRecyclerView.setAdapter(mAdapter);

//second recycler
mRecyclerView1 = (RecyclerView) findViewById(R.id.my_recycler_view1);
mRecyclerView1.setHasFixedSize(true);
mRecyclerView1.setLayoutManager(
new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false));
mAdapter1 = new MyAdapter(getDataSet1());
mRecyclerView1.setAdapter(mAdapter1);

// ...
}

关于android - 如何在一个activity中实现2个或多个recyIclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35113403/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com