LinearLayout和ListView

时间:2016-12-24 08:50:26

标签: android android-layout listview

我需要在片段中的listview之后显示listview和其他视图。

首先我尝试使用ScrollView代替LinearLayout但是当我使用它时,ListView仅显示单行。

LinearLayout的问题是它不会滚动整个视图。它只滚动ListView Only。所以其他视图总是在主视图中隐藏。

我想用扩展的listview使整个视图可滚动。所以不会有两个滚动视图。我怎么能这样做?

My Fragment Layout如下所示。

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

        <ListView>
            /** List View **/
        </ListView>

        <LinearLayout>
            /** Layouts that need to be display after list view **/
        </LinearLayout>

    </LinearLayout>
</FrameLayout>

4 个答案:

答案 0 :(得分:2)

<script>

 var now  = document.getElementById('dep').value;
 var then = document.getElementById('arr').value;
 document.writeln(moment.utc(moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss")); // 1

</script>

试试这个。

答案 1 :(得分:1)

您可以将此列表视图与滚动视图一起使用

import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;
import android.content.Context;

public class ExpandableHeightListView extends ListView
{

boolean expanded = false;

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

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

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

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded())
        {
            // Calculate entire height by providing a very large height hint.
            // But do not use the highest 2 bits of this integer; those are
            // reserved for the MeasureSpec mode.
            int expandSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

并在课堂上

ExpandableHeightListView listView = new ExpandableHeightListView(this);
listView.setAdapter(adapter);
listView.setExpanded(true);

in xml

<yourpackagename.ExpandableHeightListView 
      android:layout_width="match_parent"
        android:layout_height="match_parent"
          />

列表视图将完全展开

答案 2 :(得分:1)

在Xml中:

<!--Required to scroll the list view with Linear Layout-->
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">

            <!--Direct child container of scroll view-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <com.example.custom.ScrollableListView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:choiceMode="singleChoice"
                    android:divider="@color/colorDivider"
                    android:dividerHeight="0.09dp"
                    android:fadingEdge="none"
                    android:listSelector="@color/colorSelectedGreen"
                    android:orientation="vertical"
                    android:scrollbars="none" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            <!--Direct child container of scroll view-->

        </ScrollView>
        <!--Required to scroll the list view with Linear Layout-->

在java中创建一个ScrollableListView自定义类:

public class ScrollableListView extends ListView {

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

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

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

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 4, MeasureSpec.AT_MOST));
    }
}

答案 3 :(得分:1)

您应该尝试使用Recycler Views,因为谷歌现在建议使用回收站视图。您可以将回收器视图放在嵌套的滚动视图中,如下所示:

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="vertical">

    <ImageView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:src="@drawable/loader2" />

    <TextView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:text="Title"
        android:textColor="@android:color/black"
        android:textSize="26sp"
         />

    <app.so.city.appconstants.CustomTextViews.SourceRegularTextView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:layout_marginBottom="-40dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:lineSpacingMultiplier="1.3"
        android:textColor="#CC000000"
        android:textSize="16sp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/article_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:background="#ffffff" />

</LinearLayout>

您可以在嵌套滚动视图中放置任意数量的视图。

为了使滚动工作完美而顺利,您还需要将以下代码行添加到回收站视图的Java代码中:

recycler_view.setNestedScrollingEnabled(false);

尝试一下,让我知道这是否也适合你。