RecyclerView可见项目数 [英] RecyclerView number of visible items

查看:347
本文介绍了RecyclerView可见项目数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序中创建一个水平的RecyclerView.
它必须一次在屏幕上显示2张图像(因此每张图像的宽度必须为屏幕的50%).
现在,它可以正常工作,但每个项目都占用屏幕的整个宽度.

I am creating a horisontal RecyclerView in my app.
It has to show 2 images on the screen at a time (so width of each image has to be 50% of the screen).
For now it works fine but each item consums all width of the screen.

这是我的代码

mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_main_ads);
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getActivity());
        mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(tmp, R.layout.lv_main_screen);
        mRecyclerView.setAdapter(adapter);

这是项目的布局

<?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"
              android:weightSum="1">

    <ImageView
        android:id="@+id/iv_main_ad"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:scaleType="fitXY"
        android:src="@drawable/baner_gasoline"
        />

</LinearLayout>

如您所见,我尝试使用Layout_gravity ="0.5", 但这无济于事.

As you can see I tried to use Layout_gravity="0.5", But it doesn't help.

我尝试指定layout_width = ... dp,但我无法获得一半的屏幕.

I tried to specify layout_width = ...dp but I can not get exactly half of the screen.

我正在考虑将另一个ImageView添加到项目布局中,但是在这种情况下,我会遇到适配器问题,因为我想隐含圈出(无限)的水平listview

I am thinking of adding another ImageView into item layout, but in this case I will have troubles with the adapter, because I want to implemnt circled (infinity) horizontal listview

这是我的适配器:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyHolder> {

    private List<Integer> mImages;
    private int itemLayout;

    public RecyclerViewAdapter(ArrayList<Integer>  imageResourceIds, int itemLayout) {
        this.mImages = imageResourceIds;
        this.itemLayout = itemLayout;
    }


    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
        return new MyHolder(v);

    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {

        holder.adIv.setImageResource(mImages.get(position));

    }

    @Override
    public int getItemCount() {
        return mImages.size();
    }

    public static class MyHolder extends RecyclerView.ViewHolder {
        protected ImageView adIv;

        private MyHolder(View v) {
            super(v);
            this.adIv = (ImageView) v.findViewById(R.id.iv_main_ad);
        }
    }
}

推荐答案

因为您需要计算屏幕的宽度并动态设置宽度,以下是我的代码

For You need to calculate the width of the screen and set the width dynamically below is the my code

ViewHolder初始化中添加以下代码

Add below code in your ViewHolder initilisation

llImg = (LinearLayout) itemView.findViewById(R.id.llImg);

            llImg.getLayoutParams().width = (int) (Utils.getScreenWidth(itemView.getContext()) / 2);
            llImg.getLayoutParams().height = (int) (Utils.getScreenWidth(itemView.getContext()) / 2);

            imgView = (ImageView) itemView.findViewById(R.id.imgView);

布局文件在这里

 <LinearLayout
        android:id="@+id/llImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center">

        <ImageView
            android:id="@+id/imgView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>

制作一个Utils.java

Make one Utils.java

public static int getScreenWidth(Context context) {

        if (screenWidth == 0) {
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            screenWidth = size.x;
        }
        return screenWidth;
    }

希望这对您有帮助!

这篇关于RecyclerView可见项目数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆