Android 在景观水平 recycleview 填充屏幕

标签: Android
发布时间: 2016/9/26 4:05:48
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我水平的 recycleview。它显示元素的列表。我的问题是它看起来不错在大屏幕和景观模式上。这里看上去很好。关于景观看起来坏的下面是截图。 如何解决在第一个屏幕快照中喜欢??

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>

HorizontalRecycleViewAdapter.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:

修改为 @Onur Çevik 说与参数的 ImageView

下面是代码︰

HorizontalRecycleViewAdapter.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();
    }
}
赞助商