相关疑难解决方法(0)

嵌套的Recycler视图高度不会包装其内容

我有一个应用程序来管理书籍集(如播放列表).

我想显示一个带有垂直RecyclerView的集合列表,并在每行内部显示一个水平RecyclerView中的书籍列表.

当我将内部水平RecyclerView的layout_height设置为300dp时,它会正确显示,但是当我将其设置为wrap_content时,它不会显示任何内容. 我需要使用wrap_content,因为我希望能够以编程方式更改布局管理器以在垂直和水平显示之间切换.

在此输入图像描述

你知道我做错了什么吗?

我的片段布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white">

    <com.twibit.ui.view.CustomSwipeToRefreshLayout
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/shelf_collection_listview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingTop="10dp"/>

        </LinearLayout>

    </com.twibit.ui.view.CustomSwipeToRefreshLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

集合元素布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFF">

        <!-- Simple Header -->

    </RelativeLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/empty_collection"
            android:id="@+id/empty_collection_tv"
            android:visibility="gone"
            android:gravity="center"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/collection_book_listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/> <!-- android:layout_height="300dp" -->

    </FrameLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

书目项目:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="180dp"
              android:layout_height="220dp"
              android:layout_gravity="center">

        <ImageView
            android:id="@+id/shelf_item_cover"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:maxWidth="150dp"
            android:maxHeight="200dp"
            android:src="@drawable/placeholder"
            android:contentDescription="@string/cover"
            android:adjustViewBounds="true"
            android:background="@android:drawable/dialog_holo_light_frame"/> …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-support-library android-studio android-recyclerview

172
推荐指数
10
解决办法
10万
查看次数

我什么时候可以先测量视图?

所以我在尝试设置视图的背景可绘制时有点混乱.该代码依赖于知道视线的高度,所以我不能把它onCreate()或onResume(),因为getHeight()返回0 onResume()似乎是最接近我可以得到虽然.我应该在哪里放置如下所示的代码,以便在显示给用户时背景发生变化?

    TextView tv = (TextView)findViewById(R.id.image_test);
    LayerDrawable ld = (LayerDrawable)tv.getBackground();
    int height = tv.getHeight(); //when to call this so as not to get 0?
    int topInset = height / 2;
    ld.setLayerInset(1, 0, topInset, 0, 0);
    tv.setBackgroundDrawable(ld);
Run Code Online (Sandbox Code Playgroud)

height layout android android-lifecycle

59
推荐指数
3
解决办法
5万
查看次数

ViewTreeObserver不会调用onGlobalLayout

我已经有活动,我正在使用ViewTreeObserver没有问题,但在这种情况下,我没有得到onGlobalLayout回调.

由于在执行http API调用后得到视图的宽度,因此宽度似乎已经计算好了(由于API调用所花费的时间).无论如何,要确保我添加一个监听器ViewTreeObserver.但有时候我没有得到回调(是的,有时候).

我可以在添加监听器之前检查宽度,以避免等待回调,但我不知道为什么我有时没有得到回调.我检查过viewTreeObserver它总是活着的.

ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();

assert viewTreeObserver.isAlive();

viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout()   // Not called sometimes, why?
    {
        view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        doSomething(view.getWidth());
    }
});
Run Code Online (Sandbox Code Playgroud)

现在我将使用这个技巧:

int width = view.getWidth();

if (width > 0) {
    doSomething(view.getWidth());
} else {
    // use the ViewTreeObserver
}
Run Code Online (Sandbox Code Playgroud)

编辑:

为了防止它有用,我制作了这个帮助方法:

/**
 * Runs code on global layout event, useful when we need to do something after the layout is done
 * (like getting …
Run Code Online (Sandbox Code Playgroud)

layout android

8
推荐指数
1
解决办法
5879
查看次数