在水平的Android水平的recycleview填装屏幕

时间:2016-02-12 10:57:45

标签: android android-recyclerview

我有一个水平的循环视图。它显示了一个元素列表。我的问题是它在大屏幕和横向模式下看起来很糟糕。 Here looks ok。在风景看起来不好,here is screenshot。如何在第一个截图中修复?

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 个答案:

答案 0 :(得分: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();
    }
}