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); } } }
android android-recyclerview
Androider
source share