如何在android中定义水平回收视图中的项目数?

如何在android中定义水平回收视图中的项目数?,android,android-layout,android-recyclerview,Android,Android Layout,Android Recyclerview,下面是我如何构建回收视图的 RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); ItemData itemsData[] = { new ItemData("Help",R.drawable.profile_pic), new ItemData("Delete",R.drawable.profile_pic), new ItemDa

下面是我如何构建回收视图的

    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个图标

这样(不需要分隔器):

XML(主要活动)


您应该扩展LinearLayoutManager并覆盖以下拖曳方法:

/**
*使用标准测量策略测量子视图,使用填充
*将父RecyclerView和任何添加的项目装饰考虑在内。
*
*如果RecyclerView可以在任一维度上滚动,则调用者可以
*传递0作为widthUsed或heightUsed参数,因为它们将不相关

* *@param要测量的子视图 *@param Width使用其他视图当前使用的像素宽度(如果相关) *@param heightUsed Height当前由其他视图使用的像素数(如果相关) */ public void measureChild(视图子项、使用的整数宽度、使用的整数高度){ RecyclerView.LayoutParams lpTmp=(RecyclerView.LayoutParams)child.getLayoutParams(); 最终RecyclerView.LayoutParams lp=新的RecyclerView.LayoutParams(屏幕宽度/2,lpTmp.height); int-widthSpec=View.MeasureSpec.makeMeasureSpec(MainActivity.screenWidth/2,View.MeasureSpec.justice); int heightSpec=View.MeasureSpec.makeMeasureSpec(lp.height,View.MeasureSpec.justice); 儿童测量(宽度规格、高度规格); } /** *使用标准测量策略测量子视图,使用填充 *父RecyclerView、任何添加的项目装饰和子页边距 *考虑到。 * *如果RecyclerView可以在任一维度上滚动,则调用者可以 *传递0作为widthUsed或heightUsed参数,因为它们将不相关

* *@param要测量的子视图 *@param Width使用其他视图当前使用的像素宽度(如果相关) *@param heightUsed Height当前由其他视图使用的像素数(如果相关) */ 公共空心测量带边距的空心(视图子项、使用的整数宽度、使用的整数高度){ super.带边距的测量尺寸(子项、使用的宽度、使用的高度); RecyclerView.LayoutParams lpTmp=(RecyclerView.LayoutParams)child.getLayoutParams(); 最终RecyclerView.LayoutParams lp=新的RecyclerView.LayoutParams(屏幕宽度/2,lpTmp.height); int-widthSpec=View.MeasureSpec.makeMeasureSpec(MainActivity.screenWidth/2,View.MeasureSpec.justice); int heightSpec=View.MeasureSpec.makeMeasureSpec(lp.height,View.MeasureSpec.justice); 儿童测量(宽度规格、高度规格); }

上面的代码非常简单,只是一个演示,编号为2。您可以根据需要自定义代码。希望这些对你有帮助

使用此类而不是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
 * islamsalah007@gmail.com
 */

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;
}
导入android.content.Context;
导入android.graphics.Point;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入android.view.Display;
导入android.view.view;
导入android.view.WindowManager;
/**
*由伊斯兰萨拉赫创建。
*
* https://github.com/IslamSalah
* islamsalah007@gmail.com
*/
公共类CustomLinearLayoutManager扩展了LinearLayoutManager{
私有上下文;
私人帐户;
公共CustomLinearLayoutManager(上下文上下文、int方向、布尔反转){
超级(上下文、方向、反转);
this.mContext=上下文;
}
@凌驾
public void measureChild(视图子项、使用的整数宽度、使用的整数高度){
RecyclerView.LayoutParams lpTmp=(RecyclerView.LayoutParams)child.getLayoutParams();
final RecyclerView.LayoutParams lp=新的RecyclerView.LayoutParams(测量屏幕宽度(mContext)/spanCount,lpTmp.height);
int-widthSpec=View.MeasureSpec.makeMeasureSpec(measureScreenWidth(mContext)/spanCount,View.MeasureSpec.justice);
int heightSpec=View.MeasureSpec.makeMeasureSpec(lp.height,View.MeasureSpec.justice);
儿童测量(宽度规格、高度规格);
}
@凌驾
公共空心测量带边距的空心(视图子项、使用的整数宽度、使用的整数高度){
RecyclerView.LayoutParams lpTmp=(RecyclerView.LayoutParams)child.getLayoutParams();
final RecyclerView.LayoutParams lp=新的RecyclerView.LayoutParams(测量屏幕宽度(mContext)/spanCount,lpTmp.height);
int-widthSpec=View.MeasureSpec.makeMeasureSpec(measureScreenWidth(mContext)/spanCount,View.MeasureSpec.justice);
int heightSpec=View.MeasureSpec.makeMeasureSpec(lp.height,View.MeasureSpec.justice);
儿童测量(宽度规格、高度规格);
}
私有int-measureScreenWidth(上下文){
WindowManager WindowManager=(WindowManager)context.getSystemService(context.WINDOW\u服务);
Display Display=windowManager.getDefaultDisplay();
点=新点();
显示。getSize(点);
返回点x;
}
public int getSpanCount(){
返回spanCount;
}
公共无效设置计数(整数跨度计数){
this.spanCount=spanCount;
}

它是可滚动的吗?你能分享屏幕截图吗?更新了当前和期望的结果为什么你不使用
GridView
GridLayoutManager
?为什么在这种情况下你更喜欢
GridLayoutManager
?你想以后有更多的行吗?如果不想,你可以尝试使用
LinearLayoutManager
。并且ybe您的项目确实要求在其
宽度中填充父项
-这就是为什么您一开始只能看到一个项目的原因。检查
yqritc
在此处回答:代码片段为宽度创建所需效果,但不会为高度生成所需效果。使用完全相同的代码片段,recyclerview行项目将屏幕的总高度。这个问题有解决方案吗?没关系。找到了解决方案。我没有在child.measure(-)中传递heightspec,而是传递heightUsed变量(不使用高度c
<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 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>
 GridLayoutManager g = new GridLayoutManager(this, 2, GridLayoutManager.HORIZONTAL, false);
/**
     * 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);
}
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
 * islamsalah007@gmail.com
 */

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;
}