2016-01-04 48 views
1

這裏是我構建循環視圖如何在android中的horizo​​ntal recycleview中定義項目的數量?

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()); 

它的工作原理,但問題是,它每次只顯示1項,如何更改爲2項?這意味着,該圖標應該是屏幕寬度和總的50%具有每頁

2個圖標這樣的(無需分頻器的):

icon1 | icon2 

XML(主要活動)

<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(項目)

<?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> 

感謝您的幫助

更新:

通知該應用程序只有potarit方向,以便可以忽略景觀的情況下

當前的結果:

enter image description here

我想什麼來實現類似 enter image description here

而GridLayoutManager不是確切的行爲

GridLayoutManager g = new GridLayoutManager(this, 2, GridLayoutManager.HORIZONTAL, false); 

enter image description here enter image description here

+0

它是否可滾動?你能分享截圖嗎? –

+0

是可滾動的。 – user3538235

+0

更新了當前和期望的結果 – user3538235

回答

1

你應該擴展LinearLayoutManager並覆蓋下面拖方法:

/** 
    * 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); 
} 

上面的代碼很簡單,只是一個演示,以及數是。您可以根據您的需要定製代碼。希望這些對你有幫助;

+0

代碼片段爲寬度創建所需的效果,但不會產生所需的高度效果。使用完全相同的代碼片段,recyclerview行項目佔用屏幕的整個高度。任何解決這個問題的方法? – Krups

+0

沒關係。找到了解決方案。不是在child.measure( - , - )中傳遞heightspec,而是傳遞heightUsed變量(不使用makeMeasureSpec計算的高度)。 – Krups

1

使用此類而不是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; 
} 
相關問題