Reputation: 2009
Here is how I construct the recycle view
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
ItemData itemsData[] = { new ItemData("Help",R.drawable.profile_pic),
new ItemData("Delete",R.drawable.profile_pic),
new ItemData("Cloud",R.drawable.profile_pic),
new ItemData("Favorite",R.drawable.profile_pic),
new ItemData("Like",R.drawable.profile_pic),
new ItemData("Rating",R.drawable.profile_pic)};
LinearLayoutManager l = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(l);
MyAdapter mAdapter = new MyAdapter(itemsData);
recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
It works but the problem is it show only 1 item per time, how to change to 2 item? that means , the icon should be 50% of the screen width and total have 2 icons per page
Like this (no need of divider):
icon1 | icon2
XML (Main Activity)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
tools:context="com.hmkcode.android.recyclerview.MainActivity" >
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
/>
</RelativeLayout>
XML (item)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp">
<!-- icon -->
<ImageView
android:id="@+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/profile_pic" />
<!-- title -->
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#000000"
android:textSize="22sp" />
</RelativeLayout>
Thanks for helping
Update :
Notice The app only have potarit orientation so can ignore the case for landscape
Current result:
What I would like to achieve similar to
And GridLayoutManager is not the exact behavior
GridLayoutManager g = new GridLayoutManager(this, 2, GridLayoutManager.HORIZONTAL, false);
Upvotes: 2
Views: 2836
Reputation: 983
Use this class instead of LinearLayoutManager.
import android.content.Context;
import android.graphics.Point;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
/**
* Created by Islam Salah.
* <p>
* https://github.com/IslamSalah
* [email protected]
*/
public class CustomLinearLayoutManager extends LinearLayoutManager {
private Context mContext;
private int spanCount;
public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
this.mContext = context;
}
@Override
public void measureChild(View child, int widthUsed, int heightUsed) {
RecyclerView.LayoutParams lpTmp = (RecyclerView.LayoutParams) child.getLayoutParams();
final RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(measureScreenWidth(mContext) / spanCount, lpTmp.height);
int widthSpec = View.MeasureSpec.makeMeasureSpec(measureScreenWidth(mContext) / spanCount, View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(lp.height, View.MeasureSpec.EXACTLY);
child.measure(widthSpec, heightSpec);
}
@Override
public void measureChildWithMargins(View child, int widthUsed, int heightUsed) {
RecyclerView.LayoutParams lpTmp = (RecyclerView.LayoutParams) child.getLayoutParams();
final RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(measureScreenWidth(mContext) / spanCount, lpTmp.height);
int widthSpec = View.MeasureSpec.makeMeasureSpec(measureScreenWidth(mContext) / spanCount, View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(lp.height, View.MeasureSpec.EXACTLY);
child.measure(widthSpec, heightSpec);
}
private int measureScreenWidth(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
return point.x;
}
public int getSpanCount() {
return spanCount;
}
public void setSpanCount(int spanCount) {
this.spanCount = spanCount;
}
Upvotes: 1
Reputation: 2722
you should extend LinearLayoutManager and override tow method below:
/**
* Measure a child view using standard measurement policy, taking the padding
* of the parent RecyclerView and any added item decorations into account.
*
* <p>If the RecyclerView can be scrolled in either dimension the caller may
* pass 0 as the widthUsed or heightUsed parameters as they will be irrelevant.</p>
*
* @param child Child view to measure
* @param widthUsed Width in pixels currently consumed by other views, if relevant
* @param heightUsed Height in pixels currently consumed by other views, if relevant
*/
public void measureChild(View child, int widthUsed, int heightUsed) {
RecyclerView.LayoutParams lpTmp = (RecyclerView.LayoutParams) child.getLayoutParams();
final RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(screenWidth/2,lpTmp.height);
int widthSpec = View.MeasureSpec.makeMeasureSpec(MainActivity.screenWidth /2 , View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(lp.height , View.MeasureSpec.EXACTLY);
child.measure(widthSpec, heightSpec);
}
/**
* Measure a child view using standard measurement policy, taking the padding
* of the parent RecyclerView, any added item decorations and the child margins
* into account.
*
* <p>If the RecyclerView can be scrolled in either dimension the caller may
* pass 0 as the widthUsed or heightUsed parameters as they will be irrelevant.</p>
*
* @param child Child view to measure
* @param widthUsed Width in pixels currently consumed by other views, if relevant
* @param heightUsed Height in pixels currently consumed by other views, if relevant
*/
public void measureChildWithMargins(View child, int widthUsed, int heightUsed) {
super.measureChildWithMargins(child,widthUsed,heightUsed);
RecyclerView.LayoutParams lpTmp = (RecyclerView.LayoutParams) child.getLayoutParams();
final RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(screenWidth/2,lpTmp.height);
int widthSpec = View.MeasureSpec.makeMeasureSpec(MainActivity.screenWidth / 2, View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(lp.height, View.MeasureSpec.EXACTLY);
child.measure(widthSpec, heightSpec);
}
the code above are very simple,just a demo,and the number is 2.you can customize the codes according to your needs. hope these help to you;
Upvotes: 1