Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView number of visible items

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.

Here is my code

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);

Here is layout of an item

<?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>

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

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

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

here is my adapter:

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);
        }
    }
}
like image 808
Androider Avatar asked Feb 03 '16 14:02

Androider


People also ask

How to Get visible item count in RecyclerView?

RecyclerView rv = findViewById(...); StaggeredGridLayoutManager lm = new StaggeredGridLayoutManager(...); rv. setLayoutManager(lm); And to get visible item views: int[] viewsIds = lm.

How to check RecyclerView is scrolling or not in android?

You can use Scroll listener to detect scroll up or down changes in RecyclerView . RecycleView invokes the onScrollStateChanged() method before onScrolled() method. The onScrollStateChanged() method provides you the RecycleView's status: SCROLL_STATE_IDLE : No scrolling.


1 Answers

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

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);

The Layout file is here

 <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>

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;
    }

Hope this will help you !

like image 76
Maheshwar Ligade Avatar answered Oct 13 '22 15:10

Maheshwar Ligade