2016-07-25 2 views
2

У меня есть больше предметов, которые нужно показать в карточке. По горизонтали внутри recyclerView, и у меня больше карт.RecyclerView для прокрутки по горизонтали с шириной

Я попытался поместить cardView в HorizontalScrollView, он работал, чтобы прокручивать индивидуальную карту. Я хотел прокрутить весь RecyclerView для прокрутки, чтобы увидеть правые элементы.

Я попытался с RecyclerView внутри горизонтальногоScroolView не работал. RecyclerView внутри NestedScrollView не работал.

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

Я думаю, что вы пытаетесь прокрутить recyclerview в двух направлениях. Вертикальный обычный свиток recylerview и по горизонтали, скроллинг, если он не соответствует указанной ширине. Правильно?

Если вы пытаетесь это сделать, то я думаю, что прокручивание невозможно, как вы думаете. Вы можете прокрутить список ресайклеров в обоих направлениях с различными видами, удерживающими их.

Как вы сказали, возможна прокрутка карты, по умолчанию она не будет нормально прокручиваться нормально. Таким образом, он прокрутит, чтобы показать содержимое карты полностью пользователю, если они не видят полностью, и вы прокручиваете прокрутку.

Возможный способ: сохранить вертикальные представления с указанием их имени и значения поля. Если вы хотите создать больше, добавьте еще одну строку, чтобы уменьшить значения.

Посмотрите ниже:

<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> 
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. 
*/ 
+0

Но это не будет прокручиваться, чтобы увидеть дополнительные вещи, если мы имеем его в правую сторону после того, как размер экрана. Это покажет следующий элемент, если я прокручу. Но я хочу видеть те же значения представления элементов, которые расширяются после размера экрана (например, прокрутка страницы по горизонтали, чтобы увидеть не видимое содержимое). Спасибо @Mij – 12345