簡體   English   中英

如何在Android的水平recycleview中定義項目數?

[英]How to define the number of items in the Horizontal recycleview in android?

這是我構造回收視圖的方法

    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方向,因此可以忽略橫向情況

當前結果:

在此處輸入圖片說明

我想要達到的目標類似於 在此處輸入圖片說明

和GridLayoutManager不是確切的行為

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

在此處輸入圖片說明 在此處輸入圖片說明

您應該擴展LinearLayoutManager並在下面覆蓋tow方法:

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

上面的代碼非常簡單,只是一個演示,數字為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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM