RecyclerView number of visible items

I am creating a horizontal RecyclerView in my application.
It should simultaneously display 2 images on the screen (so that the width of each image should be 50% of the screen).
At the moment it works fine, but each element consumes the entire 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 the location of the 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 using Layout_gravity = "0.5", but this does not help.

I tried to specify layout_width = ... dp, but I cannot get exactly half the screen.

I am considering adding another ImageView to the element layout, but in this case I will have problems with the adapter, because I want to implement a horizontal list in a circle (infinity)

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); } } } 
+7
android android-recyclerview
source share
1 answer

To do this, you need to calculate the width of the screen and dynamically set the width below my code

Add code below to 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); 

Layout file 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 helps you!

+7
source share

All Articles