本文介绍了滚动视图中的Andr​​oid列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android的布局设置,其中我有一大堆的IT元素的滚动视图,然后在滚动视图的底部,我有一个ListView,然后填充一个适配器。

I have an android layout setup where I have a scrollView with a whole bunch of elements in it, and then at the bottom of the scroll view I have a listView which is then filled with an adapter.

这是我遇到的问题是,Android是不包括在滚动视图列表视图为滚动视图已经有一个滚动功能。我想在ListView只是只要内容和主滚动视图是滚动的。

The problem that I am having, is that android is excluding the listView from the scrollView as scrollView already has a scrollable function. I want the listView to just be as long as the content is and for the master scroll view to be scrollable.

我怎样才能做到这一点?

How can I achieve this?

下面是我的code的主要布局:

Here is my code in the main layout:

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:fillViewport="true"
    android:gravity="top" >

    <LinearLayout
        android:id="@+id/foodItemActvity_linearLayout_fragments"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

</ScrollView>

然后我编程方式添加我的组件到linearlayour用id: foodItemActvity_linearLayout_fragments 。下面是被加载到该LinearLayout中的视图之一。这是一个让我与滚动的麻烦。

I then programmatically add my components to the linearlayour with the id: foodItemActvity_linearLayout_fragments. Below is one of the views that is loaded into that linearlayout. This is the one giving me trouble with the scrolls.

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/fragment_dds_review_textView_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Reviews:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ListView
    android:id="@+id/fragment_dds_review_listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</ListView>

我的适配器则填补了这个列表视图。

My adapter then fills up this list view.

下面是从Android层次观众的图像时,我点击主滚动视图:

Here is an image from the android hierarchy viewer when I click on the master scrollView:

正如你所看到的,它是不包括审查的ListView。

As you can see, it is excluding the reviews listView.

我应该可以滚动页面向下,看看8条评论中,而是只显示我的3,我可以在小部分,其中评语是滚动。我想一个全球性的页面滚动

I should be able to scroll the page down and see 8 reviews, but instead it only shows me those 3, and I can scroll on the tiny part where the reviews are. I want a global page scroll

推荐答案

最短和放大器;最简单的解决方案的的的ListView内滚动型的问题。

您不必做什么特别的 layout.xml 的文件,也没有处理父滚动型什么。 您只需要处理孩子的ListView 的。 您也可以使用这个code使用任何类型的子视图内的滚动型及放大器;进行触摸操作。

You do not have to do anything special in layout.xml file nor handle anything on the parent ScrollView. You only have to handle the child ListView. You can also use this code to use any type of child view inside a ScrollView & perform Touch operations.

就在您的Java类添加code这几行:

Just add these lines of code in your java class :

ListView lv = (ListView) findViewById(R.id.layout_lv);
lv.setOnTouchListener(new OnTouchListener() {
    // Setting on Touch Listener for handling the touch inside ScrollView
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    // Disallow the touch request for parent scroll on touch of child view
    v.getParent().requestDisallowInterceptTouchEvent(true);
    return false;
    }
});

如果你把的的ListView内滚动型,然后所有的的ListView不会延伸到其全高。下面是解决此问题的方法。

If you put ListView inside a ScrollView then all the ListView does not stretch to its full height. Below is a method to fix this issue.

/**** Method for Setting the Height of the ListView dynamically.
 **** Hack to fix the issue of not showing all the items of the ListView
 **** when placed inside a ScrollView  ****/
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

要使用此方法只是通过这个方法里面的ListView:

To use this method just pass the ListView inside this method :

ListView list = (ListView) view.findViewById(R.id.ls);
setListViewHeightBasedOnChildren(list);

对于使用需要ExpandableListView - 信贷的

For using with ExpandableListView - credit Benny

ExpandableListView: view = listAdapter.getView(0, view, listView);
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.MATCH_PARENT, View.MeasureSpec.EXACTLY);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY);
view.measure(widthMeasureSpec, heightMeasureSpec);

对于ListView的可变项目高度使用下面的链接:

ListView滚动型内不滚动在Android

这篇关于滚动视图中的Andr​​oid列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 11:07