Horizo​​ntalScrollView中具有RecyclerView的2D列表 [英] 2D List with RecyclerView in HorizontalScrollView

查看:92
本文介绍了Horizo​​ntalScrollView中具有RecyclerView的2D列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个视图,该视图将允许用户水平和垂直滚动类似Excel的结构.我最初的想法是将RecyclerView(带有LinearManager)放入Horizo​​ntalScrollView中.但这似乎不起作用.

I am trying to build a view which will allow the user to scroll an Excel-like structure both horizontally and vertically. My initial idea was to put a RecyclerView (with LinearManager) into a HorizontalScrollView. But it does not seem to work.

这是我的代码:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/gameplay_Toolbar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/accent"
        app:title="@string/gameplay_score_toolbar"
        app:titleMarginStart="48dp"
        app:titleTextAppearance="@style/toolbar_title" />

    <HorizontalScrollView
        android:id="@+id/gameplay_hotizontalScroll_ScrollView"
        android:layout_below="@+id/gameplay_Toolbar"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:fillViewport="true"
        >

        <android.support.v7.widget.RecyclerView
            android:id="@+id/gameplay_gameContents_RecyclerView"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"/>

    </HorizontalScrollView>

</RelativeLayout>

现在,它只允许Recycler滚动,Horizo​​ntalScrollView看起来像普通的FrameLayout(因为Recycler内部的视图被剪切到边缘).

Right now it only allows the Recycler to scroll, the HorizontalScrollView seems to act like a normal FrameLayout (as the views inside the Recycler are clipping to the edge).

我认为放入回收站的视图具有固定大小可能是有意义的.

I think it may be relevant that the views I put into the Recycler have fixed size.

有关如何使该概念发挥作用的任何提示?

Any tips on how to get this concept to work?

推荐答案

[已解决]

所有技巧都是手动设置RecyclerView宽度,因为它拒绝接受WRAP_CONTENT,并且始终始终与屏幕宽度一样大.窍门如下:

All the trick is to manually set RecyclerView width, because it refuses to accept WRAP_CONTENT and always is maximally as wide as screen width. Trick is following:

public class SmartRecyclerView extends RecyclerView {

public int computedWidth = <needs to be set from outside>

public SmartRecyclerView(Context context) {
    super(context);
}

public SmartRecyclerView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SmartRecyclerView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public boolean canScrollHorizontally(int direction) {
    return false;
}

@Override
public int getMinimumWidth() {
    return computedWidth;
}

@Override
protected void onMeasure(int widthSpec, int heightSpec) {       
    super.onMeasure(widthSpec, heightSpec);
    setMeasuredDimension(computedWidth, getMeasuredHeight());       
}

@Override
protected int getSuggestedMinimumWidth() {      
    return computedWidth;
}
}

然后简单地:

HorizontalScrollView myScroll = ...
SmartRecyclerView recyclerView = new SmartRecyclerView(...)
...
recyclerView.computedWidth = myNeededWidth;
myScroll.addView(recyclerView);

成功了! 编码愉快...

and it WORKS! Happy coding...

示例工作代码: https://dl.dropboxusercontent.com/u/79978438/RecyclerView_ScrollView.zip

这篇关于Horizo​​ntalScrollView中具有RecyclerView的2D列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆