2016-02-12 88 views
1

我得到了一個水平的回收視圖。它顯示了一個元素列表。我的問題是,它在大屏幕和橫向模式下看起來很糟糕。 Here looks ok。在風景看起來不好,here is screenshot。如何修復像第一次截圖?Android水平回收視圖在景觀上填充屏幕

HomeFragment.class

RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view); 
     recyclerView.setHasFixedSize(true); 
     HorizontalRecycleViewAdapter adapter = new HorizontalRecycleViewAdapter(getActivity(), categoriesList); 
     recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false)); 
     recyclerView.setNestedScrollingEnabled(false); 
     recyclerView.setAdapter(adapter); 

fragment_home.xml

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:focusable="true" 
     android:focusableInTouchMode="true"> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/my_recycler_view" 
      android:layout_width="match_parent" 
      android:layout_height="200dp" 
      android:scrollbars="none" /> 
    </LinearLayout> 

Horizo​​ntalRecycleViewAdapter.class

public class HorizontalRecycleViewAdapter extends RecyclerView.Adapter<HorizontalRecycleViewAdapter.SingleItemRowHolder> { 

    private ArrayList<Category> itemsList; 
    private Context mContext; 

    public HorizontalRecycleViewAdapter(Context context, ArrayList<Category> itemsList) { 
     this.itemsList = itemsList; 
     this.mContext = context; 
    } 

    @Override 
    public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
     View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_single_card, null); 
     SingleItemRowHolder mh = new SingleItemRowHolder(v); 
     return mh; 
    } 

    @Override 
    public void onBindViewHolder(SingleItemRowHolder holder, int i) { 
     Category item = itemsList.get(i); 
     holder.title.setText(item.getName()); 
     holder.imageResource.setImageResource(item.getImageSource()); 
    } 

    @Override 
    public int getItemCount() { 
     return (null != itemsList ? itemsList.size() : 0); 
    } 

    public class SingleItemRowHolder extends RecyclerView.ViewHolder { 

     protected TextView title; 
     protected ImageView imageResource; 
     public SingleItemRowHolder(View view) { 
      super(view); 
      this.title = (TextView) view.findViewById(R.id.tvTitle); 
      this.imageResource = (ImageView) view.findViewById(R.id.itemImage); 
      view.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Toast.makeText(v.getContext(), title.getText(), Toast.LENGTH_SHORT).show(); 
       } 
      }); 
     } 
    } 
} 

list_single_card.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    > 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <ImageView 
      android:id="@+id/itemImage" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:layout_gravity="center_horizontal" 
      android:scaleType="fitCenter" 
      android:layout_marginBottom="5dp" 
      android:src="@drawable/android" /> 
     <TextView 
      android:id="@+id/tvTitle" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/itemImage" 
      android:gravity="center" 
      android:padding="5dp" 
      android:text="Sample title" 
      android:textColor="@android:color/black" 
      android:textSize="18sp" /> 
    </LinearLayout> 
</LinearLayout> 
+1

您可以爲橫向模式使用不同的列表項目佈局,也可以更改ImageView的layoutparams並將其增加到最適合您的值。 –

回答

1

改性如@OnurÇevik與ImageView的

的所述的LayoutParams

下面是代碼:

Horizo​​ntalRecycleViewAdapter.class

@Override 
    public void onBindViewHolder(SingleItemRowHolder holder, int i) { 


     Category item = itemsList.get(i); 
     holder.title.setText(item.getName()); 

     holder.imageResource.setImageResource(item.getImageSource()); 
     if (Utils.getScreenOrientation((Activity) mContext) == 2) { 
      holder.imageResource.getLayoutParams().width = Utils.getScreenWidth((Activity) mContext)/getItemCount(); 
     } 

    } 

的Utils .class

public class Utils{ 
public static int getScreenOrientation(Activity activity) { 
     Display getOrient = activity.getWindowManager().getDefaultDisplay(); 
     int orientation = Configuration.ORIENTATION_UNDEFINED; 
     if (getOrient.getWidth() == getOrient.getHeight()) { 
      orientation = Configuration.ORIENTATION_SQUARE; 
     } else { 
      if (getOrient.getWidth() < getOrient.getHeight()) { 
       orientation = Configuration.ORIENTATION_PORTRAIT; 
      } else { 
       orientation = Configuration.ORIENTATION_LANDSCAPE; 
      } 
     } 
     return orientation; 
    } 

    public static int getScreenWidth(Activity activity) { 
     Display display = activity.getWindowManager().getDefaultDisplay(); 
     return display.getWidth(); 
    } 
}