使用LinearLayout和ListView的可滚动视图 [英] Scrollable view with LinearLayout and ListView

查看:72
本文介绍了使用LinearLayout和ListView的可滚动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在片段中的列表视图之后显示列表视图和其余视图.

I need to display listview and rest of the views after the listview in my Fragment.

首先,我尝试使用ScrollView而不是LinearLayout,但是当我使用它时,ListView仅显示单行.

First i tried using ScrollView instead of LinearLayout but when im using it ListView only show single row only.

LinearLayout的问题是它不能滚动整个视图.它仅滚动浏览ListView.因此其他视图始终在主视图中隐藏.

Problem with LinearLayout is it does not scroll entire view. it only scroll through ListView Only. so other views are always hidden from the main view.

我想使整个视图与扩展的listview一起滚动.这样就不会有两个滚动视图.我该怎么办?

i want to make entire view scrollable with expanded listview. so there wont be two scrolling views. how can i do that?

我的片段布局如下.

<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>

推荐答案

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

You can use this list view with scroll view

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);

在xml

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

列表视图将完全展开

这篇关于使用LinearLayout和ListView的可滚动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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