Reputation: 177
I got a horizontal recycleview. It shows a list of elements. My problem is that it looks bad on large screens and landscape modes. Here looks ok. On landscape looks bad, here is screenshot. How to fix to like in first screenshot ??
HomeFragment.class
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
HorizontalRecycleViewAdapter adapter = new HorizontalRecycleViewAdapter(getActivity(), categoriesList);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(adapter);
fragment_home.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scrollbars="none" />
</LinearLayout>
HorizontalRecycleViewAdapter.class
public class HorizontalRecycleViewAdapter extends RecyclerView.Adapter<HorizontalRecycleViewAdapter.SingleItemRowHolder> {
private ArrayList<Category> itemsList;
private Context mContext;
public HorizontalRecycleViewAdapter(Context context, ArrayList<Category> itemsList) {
this.itemsList = itemsList;
this.mContext = context;
}
@Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_single_card, null);
SingleItemRowHolder mh = new SingleItemRowHolder(v);
return mh;
}
@Override
public void onBindViewHolder(SingleItemRowHolder holder, int i) {
Category item = itemsList.get(i);
holder.title.setText(item.getName());
holder.imageResource.setImageResource(item.getImageSource());
}
@Override
public int getItemCount() {
return (null != itemsList ? itemsList.size() : 0);
}
public class SingleItemRowHolder extends RecyclerView.ViewHolder {
protected TextView title;
protected ImageView imageResource;
public SingleItemRowHolder(View view) {
super(view);
this.title = (TextView) view.findViewById(R.id.tvTitle);
this.imageResource = (ImageView) view.findViewById(R.id.itemImage);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), title.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
}
list_single_card.xml
<?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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/itemImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:scaleType="fitCenter"
android:layout_marginBottom="5dp"
android:src="@drawable/android" />
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/itemImage"
android:gravity="center"
android:padding="5dp"
android:text="Sample title"
android:textColor="@android:color/black"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
Upvotes: 1
Views: 3261
Reputation: 177
Modified as @Onur Çevik said with layoutparams of ImageView
Here is the code:
HorizontalRecycleViewAdapter.class
@Override
public void onBindViewHolder(SingleItemRowHolder holder, int i) {
Category item = itemsList.get(i);
holder.title.setText(item.getName());
holder.imageResource.setImageResource(item.getImageSource());
if (Utils.getScreenOrientation((Activity) mContext) == 2) {
holder.imageResource.getLayoutParams().width = Utils.getScreenWidth((Activity) mContext) / getItemCount();
}
}
Utils.class
public class Utils{
public static int getScreenOrientation(Activity activity) {
Display getOrient = activity.getWindowManager().getDefaultDisplay();
int orientation = Configuration.ORIENTATION_UNDEFINED;
if (getOrient.getWidth() == getOrient.getHeight()) {
orientation = Configuration.ORIENTATION_SQUARE;
} else {
if (getOrient.getWidth() < getOrient.getHeight()) {
orientation = Configuration.ORIENTATION_PORTRAIT;
} else {
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
return orientation;
}
public static int getScreenWidth(Activity activity) {
Display display = activity.getWindowManager().getDefaultDisplay();
return display.getWidth();
}
}
Upvotes: 2