我有更多的项目在cardView中水平显示在recyclerView内,我有更多的卡片。
我尝试将cardView放在HorizontalScrollView中,它可以滚动个人卡片。我想滚动整个RecyclerView以滚动查看右端项目。
我尝试使用HorizontalScroolView内的RecyclerView无效。 NestedScrollView内的RecyclerView无效。
RecyclerView是一个片段。在viewPager tabLayout中,这是一个片段
片段xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.RecyclerView
android:id="@+id/record_recycler"
android:layout_width="550dp"
android:scrollbars="horizontal"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
适配器:
public class TestAdapter extends RecyclerView.Adapter{
private List<Model> items;
Context context;
public TestAdapter(Context con, List<Model> itemslist) {
context=con;
this.items = itemslist;
}
@Override
public int getItemViewType(int position) {
return items.get(position).getUnique();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType==0)
return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cricketrank_record_valuecard, parent, false));
else
return new MyViewHolder1(LayoutInflater.from(parent.getContext()).inflate(R.layout.cricketrank_record_titlecard, parent, false));
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyViewHolder)
try{
((MyViewHolder)holder).bindViewHolder(position);
}catch (Exception e){
Log.e(Constant.Tag,e.toString());
}
else if(holder instanceof MyViewHolder1)
try{
((MyViewHolder1)holder).bindViewHolder(position);
}catch (Exception e){
Log.e(Constant.Tag,e.toString());
}
}
@Override
public int getItemCount() {
return items.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView text,text1,text2,text3,text4,text5;
public View mCardView;
public MyViewHolder(View view) {
super(view);
text1 = (TextView) view.findViewById(R.id.text1);
text2 = (TextView) view.findViewById(R.id.text2);
text3 = (TextView) view.findViewById(R.id.text3);
text4 = (TextView) view.findViewById(R.id.text4);
}
public void bindViewHolder(int position) {
text1.setText(items.get(position).getTeam());
text2.setText(items.get(position).getRank());
text3.setText(items.get(position).getMatches());
text4.setText(items.get(position).getPoints());
}
}
}
public class MyViewHolder1 extends RecyclerView.ViewHolder{
public TextView text,text1,text2,text3,text4,text5;
public RelativeLayout rl;
public MyViewHolder1(View view) {
super(view);
text = (TextView) view.findViewById(R.id.text);
text1 = (TextView) view.findViewById(R.id.text1);
text2 = (TextView) view.findViewById(R.id.text2);
text3 = (TextView) view.findViewById(R.id.text3);
text4 = (TextView) view.findViewById(R.id.text4);
rl = (RelativeLayout) view.findViewById(R.id.rl);
}
public void bindViewHolder(int position) {
text1.setText(items.get(position).getTeam());
text2.setText(items.get(position).getRank());
text3.setText(items.get(position).getMatches());
text4.setText(items.get(position).getPoints());
}
}
}
MainActivity:
RecyclerView record_recycler= (RecyclerView) view.findViewById(R.id.record_recycler);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
record_recycler.setLayoutManager(mLayoutManager);
record_recycler.setItemAnimator(new DefaultItemAnimator());
TestAdapter adapter = new TestAdapter(getActivity(), list);
record_recycler.setAdapter(adapter);
答案 0 :(得分:0)
你可以使用LayoutManager refrence存档它,带有3个参数,在你的情况下,稍微改一下MainActivity
,它会看起来像这样:
RecyclerView.LayoutManager mLayoutManager
= new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
就像文档说的那样,第二个参数定义了方向:
/**
* @param context Current context, will be used to access resources.
* @param orientation Layout orientation. Should be {@link #HORIZONTAL} or {@link
* #VERTICAL}.
* @param reverseLayout When set to true, layouts from end to start.
*/
答案 1 :(得分:0)
我认为您正试图向两个方向滚动Recyclerview。垂直是recylerview的常用滚动,横向是滚动视图,如果它不符合上述宽度。正确?
如果你正在尝试这个,那么我认为不可能像你想象的那样滚动。 您可以在两个方向上滚动回收者视图,并持有不同的视图。
正如您所说,可以滚动cardview,默认情况下它不会正常滚动。因此,如果用户没有完全看到,则会滚动显示卡片内容,并将滚动视图设置为滚动。
可能的方法是,使用字段名称和值垂直保留视图。如果要创建更多内容,请再添加一行以适应值。
看看下面的内容:
<android.support.v7.widget.CardView
android:id="@+id/card"
android:layout_marginTop="2dp"
android:layout_marginBottom="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/layout1"
android:orientation="horizontal">
<TextView
android:text="One"
android:textSize="@dimen/font_itemsrow_13dp"
android:layout_weight=".11"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="Two"
android:textSize="@dimen/font_itemsrow_13dp"
android:layout_weight=".11"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="Three"
android:textSize="@dimen/font_itemsrow_13dp"
android:layout_weight=".24"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="Four"
android:textSize="@dimen/font_itemsrow_13dp"
android:layout_weight=".11"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="Five"
android:textSize="@dimen/font_itemsrow_13dp"
android:layout_weight=".17"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="Six"
android:textSize="@dimen/font_itemsrow_13dp"
android:layout_weight=".26"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding_3dp"
android:layout_below="@+id/layout2"
android:orientation="horizontal">
<TextView
android:id="@+id/one"
android:textSize="@dimen/font_itemsrow_14dp"
android:textColor="@color/black"
android:layout_weight=".11"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/two"
android:textSize="@dimen/font_itemsrow_14dp"
android:textColor="@color/black"
android:layout_weight=".11"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/three"
android:textSize="@dimen/font_itemsrow_14dp"
android:textColor="@color/black"
android:layout_weight=".24"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/four"
android:textSize="@dimen/font_itemsrow_14dp"
android:textColor="@color/black"
android:layout_weight=".11"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/five
android:textSize="@dimen/font_itemsrow_14dp"
android:textColor="@color/black"
android:layout_weight=".17"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/six"
android:textSize="@dimen/font_itemsrow_14dp"
android:textColor="@color/black"
android:layout_weight=".26"
android:paddingRight="3dp"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
....// Add extra title Row
</LinearLayout>
<LinearLayout
....// Add extra Values Row
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>