为什么在scrollView下看不到recyclerView?

时间:2016-02-04 06:17:35

标签: android

我想要一个布局,其中我需要在两个视图之间使用recyclelerView并且为了滚动所有,我需要scrollView。我创建了一个示例程序,其中我使用了以下xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
tools:context="com.example.vatishs.recyclerviewunderscrollview.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom|start"
        android:hint="Hint" />

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

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom|start"
        android:hint="Hint" />
</LinearLayout>
</ScrollView>

现在,我不明白为什么在运行这个程序时,recyclelerView不可见?

如果我不使用scrollView,则可见。

4 个答案:

答案 0 :(得分:1)

将您的 empd_id emails 1 aa@gmail.com 1 aaa@gmail.com 1 aaaa@gmail.com 2 bb@gmail.com 2 bbb@gmail.com 3 cc@gmail.com 替换为xml以下。

xml

答案 1 :(得分:1)

您已将主要布局的高度指定为wrap_content,将其更改为match_parent

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context="com.example.vatishs.recyclerviewunderscrollview.MainActivity">

答案 2 :(得分:0)

从技术上讲,您不应在可滚动视图中放置可滚动视图。这意味着,您不能将RecyclerView放在ScrollView中。

答案 3 :(得分:0)

这是我的问题的正确xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context="com.example.vatishs.recyclerviewunderscrollview.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom|start"
        android:hint="Hint" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/mRecycler"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom|start"
        android:hint="Hint" />
</LinearLayout>
</ScrollView>

并且对于回收器视图允许滚动与其他scrollView项目,我们必须通过设置其LayoutParams在运行时给出recyclerView高度。就像每个50dp的100 TextView一样,recyclerView高度将是:

mRecyclerView = (RecyclerView) findViewById(R.id.mRecycler);

    float density = getResources().getDisplayMetrics().density;
    mRecyclerView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ((int) (density * 50)) * 100));

它会正常工作:)