片段内的scrollview不滚动

时间:2016-03-07 10:46:33

标签: android android-fragments scrollview android-linearlayout

我是应用开发的新手。

我正在使用3个标签。在每个片段中,我想显示cardview列表(7-8张卡片)。

我的第一个fragment.xml

<ScrollView
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:fillViewport="true"

xmlns:android="http://schemas.android.com/apk/res/android" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="@color/white">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/crt1"
        card_view:cardUseCompatPadding="true"
        card_view:cardElevation="2dp"
        card_view:cardCornerRadius="1dp"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="3dp">

            <ImageView
                android:layout_width="85dp"
                android:layout_height="85dp"
                android:src="@drawable/c_3musketeers" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Three Musketeers"
                    android:textSize="25sp" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Rare | Air + Ground"
                    android:textSize="15sp" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Three Musketeers"
        android:textSize="25sp" />

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/crt2"
        card_view:cardUseCompatPadding="true"
        card_view:cardElevation="2dp"
        card_view:cardCornerRadius="1dp"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="3dp">

            <ImageView
                android:layout_width="85dp"
                android:layout_height="85dp"
                android:src="@drawable/c_archers" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Archers"
                    android:textSize="25sp" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Common | Air + Ground"
                    android:textSize="15sp" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>

     当我安装这个ap时它根本就不滚动。

我的fragment.java文件

public class TroopsFragment extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final ScrollView scrollview1 = (ScrollView) inflater.inflate(R.layout.troops,
            container, false);


    CardView cvt1;
    cvt1 = (CardView) scrollview1.findViewById(R.id.crt1);


    cvt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "tushar kalsara",
                    Toast.LENGTH_SHORT).show();
        }
    });
    return scrollview1;
}}

5 个答案:

答案 0 :(得分:4)

在ScrollView中添加此属性

android:isScrollContainer="false"

并将LinearLayout设为

wrap_content

答案 1 :(得分:1)

使用嵌套的滚动视图作为片段视图可以达到实现可滚动视图的目的:

<androidx.core.widget.NestedScrollView
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical">

答案 2 :(得分:0)

在ScrollView中添加此属性

  

<强>机器人:isScrollContainer = “假”

并将LinerLayout设为

  

WRAP_CONTENT

答案 3 :(得分:0)

更改scrollview的高度以换行内容

及其子布局以包装内容

那么它会起作用

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >

<LinearLayout
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical"
    android:padding="10dp" >

    <android.support.v7.widget.CardView
        android:id="@+id/crt1"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        card_view:cardCornerRadius="1dp"
        card_view:cardElevation="2dp"
        card_view:cardUseCompatPadding="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="3dp" >

            <ImageView
                android:layout_width="85dp"
                android:layout_height="85dp"
                android:src="@drawable/c_3musketeers" />

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Three Musketeers"
                    android:textSize="25sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Rare | Air + Ground"
                    android:textSize="15sp" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Three Musketeers"
        android:textSize="25sp" />

    <android.support.v7.widget.CardView
        android:id="@+id/crt2"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        card_view:cardCornerRadius="1dp"
        card_view:cardElevation="2dp"
        card_view:cardUseCompatPadding="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="3dp" >

            <ImageView
                android:layout_width="85dp"
                android:layout_height="85dp"
                android:src="@drawable/c_archers" />

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Archers"
                    android:textSize="25sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Common | Air + Ground"
                    android:textSize="15sp" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

这可能有效。 :)

答案 4 :(得分:0)

您永远不应将可滚动视图放在另一个可滚动视图中。 (在您的示例中,cardviews中有scrollview。)

但是,如果您坚持这样做,请检查this answer。您可以在cardviews

上设置固定的高度

也可以为listviews完成。如果需要,请参阅this blog