我正在尝试在一个活动example中设置多个循环视图(对于每个...事件,漫画和角色都有循环视图)如何在我的代码中创建它?我正在尝试下面的代码
这是主要活动的布局,它有一个回收者视图
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RecyclerViewActivity">
<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" />
</RelativeLayout>
这是我的适配器中的代码,我正在设置一个适配器
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);
我在我的适配器类中想到了这样的方式,有几个视图会有所帮助,但是它很正确
@Override
public void onBindViewHolder(RecyclerView.ViewHolder TextViewHolder, int position) {
switch (TextViewHolder.getItemViewType()) {
case TYPE_IMAGE:
ImageViewHolder imageViewHolder = (ImageViewHolder) TextViewHolder;
// imageViewHolder.mImage.setImageResource(...);
final String namev = mDataset.get(position);
imageViewHolder.txtHeader.setText(mDataset.get(position));
imageViewHolder.txtHeader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
remove(namev);
}
});
break;
case TYPE_GROUP:
TextViewHolder viewHolder = (TextViewHolder) TextViewHolder;
// viewHolder.txtHeader.setText(...)
final String name = mDataset.get(position);
viewHolder.txtHeader.setText(mDataset.get(position));
viewHolder.txtHeader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
remove(name);
}
});
viewHolder.txtFooter.setText("Footer: " + mDataset.get(position));
break;
}
这是完整代码(编辑)
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.firstLine1);
txtFooter = (TextView) itemView.findViewById(R.id.secondLine1);
}
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class TextViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView textheader;
public TextView txtFooter;
public TextViewHolder(View v) {
super(v);
textheader = (TextView) v.findViewById(R.id.textc);
// txtFooter = (TextView) v.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) {
if (viewType == TYPE_IMAGE) {
// 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;
} else {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.childlayout, parent, false);
// set the view's size, margins, paddings and layout parameters
TextViewHolder vh = new TextViewHolder(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 == 0 ? TYPE_IMAGE : TYPE_GROUP;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(RecyclerView.ViewHolder TextViewHolder, int position) {
switch (TextViewHolder.getItemViewType()) {
case TYPE_IMAGE:
ImageViewHolder imageViewHolder = (ImageViewHolder) TextViewHolder;
// imageViewHolder.mImage.setImageResource(...);
final String namev = mDataset.get(position);
imageViewHolder.txtHeader.setText(mDataset.get(position));
imageViewHolder.txtHeader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
remove(namev);
}
});
break;
case TYPE_GROUP:
TextViewHolder viewHolder = (TextViewHolder) TextViewHolder;
// viewHolder.txtHeader.setText(...)
final String name = mDataset.get(position);
viewHolder.textheader.setText(mDataset.get(position));
viewHolder.textheader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
remove(name);
}
});
viewHolder.txtFooter.setText("Footer: " + mDataset.get(position));
break;
}
/*
// - get element from your dataset at this position
// - replace the contents of the view with that element
final String name = mDataset.get(position);
holder.txtHeader.setText(mDataset.get(position));
holder.txtHeader.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
remove(name);
}
});
holder.txtFooter.setText("Footer: " + mDataset.get(position));
*/
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.size();
}
答案 0 :(得分:3)
是的,您可以在适配器的getItemViewType()
中决定您的观点。
@Override
public int getItemViewType(int position) {
if (check condition here with your listData)) // if it is headerView return header type
return TYPE_HEADER;
return TYPE_ITEM;
}
现在您可以在onCreateViewHolder()
中获取此值,决定您对标题和子项的查看。
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
return new VHItem(v);
} else if (viewType == TYPE_HEADER) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
return new VHHeader(v);
}
}
并在您的onBindViewHolder()
show approprite数据中显示标题和子视图
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof VHItem) {
String dataItem = getItem(position);
//cast holder to VHItem and set data
((VHItem) holder).description.setText(dataItem);
} else if (holder instanceof VHHeader) {
//cast holder to VHHeader and set data for header.
}
}
摘自here
答案 1 :(得分:0)
首先,我不认为在一个RecyclerViews
上有多个Activity
是一个好主意(这是可能的,但有其自身的问题)。
您提供的示例可能只使用一个RecyclerView
与GridLayoutManager
,不同的ItemTypes和不同的spanning用于行。你可以实现这一切(最终不是那么难)或者搜索这种视图的自定义实现(GitHub有很多)。