ScrollView困境中的ListView - 这个应用程序是如何设法完成的?

时间:2015-09-27 13:57:45

标签: android listview android-listview

所以基本上你不能在ScrollView中放置ListView,因为滚动功能在两种布局中都会发生冲突。当我尝试这样做时,ListView变得完全没用,并且出现了许多其他问题。

Facebook是如何做到的?

正如您所看到的,工作部分是一个ListView,它也是一个Scrollable布局,以便用户可以向下滚动以查看Education部分,它也是一个ListView。

enter image description here

我的代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff"
    android:layout_marginBottom="40dp">

               <!-- More layouts -->

                <ListView
                    android:id="@+id/work_list"
                    android:layout_below="@+id/recentpic"
                    android:layout_marginTop="10dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                </ListView>

</ScrollView >
  

我不想要ListView滚动条

因此滚动困境完全从等式中移除。即使我禁用滚动条,问题仍然存在。

我想到的解决方案:

  

生成XML行(ListView的每个工作区)并将其注入布局并避免使用ListView,类似于使用Javascript生成HTML代码。

你认为Facebook在他们的Android应用程序中使用了什么方法来完成这项工作以及我应该对我的代码做出哪些更改? :)

2 个答案:

答案 0 :(得分:1)

您是否尝试过使用NestedScrollView?我认为它是NestedScrollView,其中包含ListView,整个内容都包含在ScrollView中。此链接可能有所帮助: http://ivankocijan.xyz/android-nestedscrollview/

答案 1 :(得分:1)

好的,所以我设法编写了我自己提到的想法。这是一个非常“性感”的代码,它完成了工作:D

伙计,伙计们。我希望它可以帮助某人:)

所以基本上我正在动态地使用多个子布局来扩展父布局,并完全摆脱视图中的ListView。使用ScrollView使用它非常简单并且忘记了这个困境。

父布局:

<RelativeLayout
        android:id="@+id/work_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

</RelativeLayout>

子布局 - work_single_item.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff">

    <ImageView
        android:id="@+id/work_pic"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:src="@mipmap/image_placeholder"/>

</RelativeLayout>

在父布局的OnCreate函数中对以下行进行编码。

RelativeLayout parent = (RelativeLayout) findViewById(R.id.work_list);

//array containing all children ids
ArrayList<Integer> children = new ArrayList<>();

//adding 10 children to the parent
for(int i=0;i<10;i++) {

    RelativeLayout child = new RelativeLayout(this);
    View tempchild = getLayoutInflater().inflate(R.layout.work_single_item, null);
    child.addView(tempchild);

    child.setId(i); //setting an id for the child
    children.add(i); //adding the child's id to the list

    if(i!=0) //if it isn't the 1st child, stack them below one another, since the 1st child does not have a child to stack below
    {
                RelativeLayout.LayoutParams params 
                           = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.addRule(RelativeLayout.BELOW, children.get(i - 1)); //stack it below the previous child
                child.setLayoutParams(params);
    }

    parent.addView(child); //add the new child
}