android - 如何定义android中Horizo​​ntal recyclerview中的项目数量?

标签 android android-layout android-recyclerview

这是我构建回收 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());

它可以工作,但问题是它每次只显示 1 个项目,如何更改为 2 个项目?这意味着,图标应占屏幕宽度的 50%,并且每页总共有 2 个图标

像这样(不需要分隔符):

icon1 | icon2 

XML(主要 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(项目)

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

感谢您的帮助

更新:

注意该应用程序只有正向方向,因此可以忽略横向的情况

当前结果:

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

最佳答案

您应该扩展 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);
}

上面的代码很简单,只是一个demo,数量是2。您可以根据自己的需要自定义代码。希望这些对您有所帮助;

关于android - 如何定义android中Horizo​​ntal recyclerview中的项目数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34584504/

相关文章:

java - 当另一个 fragment 中的数据发生变化时,如何刷新一个 fragment 中的RecyclerView

android - 你如何注释掉 Proguard.cfg 中的规则?

javascript - Javascript中按位运算结果的区别

android - 如何创建自定义环形可绘制 android

android - 滑动项目时如何确定项目在 ViewHolder 中的位置?

android - RecyclerView 的适配器,没有按预期工作

java - 在启动时获取许多项目时不断出现内存不足错误

android - facebook android app如何防止自己被杀

android - 按钮不显示 Material Design

android-layout - 创建全景 View android