10

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);
        }
    }
}
Androider
  • 407
  • 2
  • 7
  • 17

1 Answers1

7

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 !

Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59
  • Check there is one method I added check now – Maheshwar Ligade Feb 03 '16 at 15:13
  • I noticed an issue with this. It is that when I am scrolling images - I scroll them like one long image. So when I stop scrolling - it stops everywhere and partly 3 images can be shown. But when I stop scrolling should be shown only two, so images must adjust to the edges (like it is hapeened with initial solution which I posted. When I stop this default one - always one image will be shown) – Androider Feb 03 '16 at 15:33
  • @Androider the divide count 2 or less/more depend on how many imageView do you want to show. For scroll problem check the LayoutManger – Maheshwar Ligade Feb 03 '16 at 15:39
  • @MaheshwarLigade - in your logic, you have taken entire screen width, suppose the width of recycler view is not same with screen width, please tell me how to calculate the recycler view width ? – Navas pk Jan 08 '17 at 13:56
  • @Navas this link may be usefule for you http://stackoverflow.com/questions/26517930/measure-recyclerview-before-it-appears – Maheshwar Ligade Jan 09 '17 at 07:13
  • 1
    @MaheshwarLigade What I used to find the width of recylcler view is : created a RelativeLayout as a parent for Recycler view and get the width from parent `View view = relativeLayout.getWidth()` this code will return the width of parent of recylcer view in pixel. Basically recycler view width is same as that of parent width in my code. So this approach i used to to get the width.. – Navas pk Jan 09 '17 at 07:39
  • @Navas your approach was good.if your parent layout has recyclerview – Maheshwar Ligade Jan 09 '17 at 07:46
  • @MaheshwarLigade what is the value of screenWidth variable? – Vidhi Dave Feb 03 '18 at 11:44