ScrollView 内部的 RelativeLayout 和 ListView

RelativeLayout and ListView inside ScrollView

我正在尝试为我的社交网络客户创建一个可滚动的个人资料。

这是我的布局:

<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/user_profile"
            android:layout_width="fill_parent"
            android:layout_height="440dp"
            android:layout_alignParentTop="false"
            android:layout_centerHorizontal="true">

            <ImageView
                android:id="@+id/fragment_user_profile_avatar"
                android:layout_width="fill_parent"
                android:layout_height="300dp"
                android:scaleType="centerCrop"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#0277BD"
                android:layout_below="@+id/fragment_user_profile_avatar"
                android:layout_alignParentLeft="false"
                android:layout_alignParentStart="false"
                android:layout_alignParentBottom="false"
                android:id="@+id/tableLayout">

                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
                    <LinearLayout android:gravity="center" android:orientation="vertical" android:id="@+id/header_user_profile_friends_container"  android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="33.0" style="?android:attr/borderlessButtonStyle">

                        <TextView android:textSize="14.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_friends_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="-2.0dip" android:text="@string/friends"
                            android:fontFamily="sans-serif-light" />

                        <TextView android:textSize="26.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_friends" android:layout_width="wrap_content" android:layout_height="wrap_content"
                            android:fontFamily="sans-serif-light" />
                    </LinearLayout>
                    <LinearLayout android:gravity="center" android:orientation="vertical" android:id="@+id/header_user_profile_followers_container"  android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="33.0" style="?android:attr/borderlessButtonStyle">

                        <TextView android:textSize="14.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_followers_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="-2.0dip" android:text="@string/followers"
                            android:fontFamily="sans-serif-light" />

                        <TextView android:textSize="26.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_followers" android:layout_width="wrap_content" android:layout_height="wrap_content"
                            android:fontFamily="sans-serif-light" />
                    </LinearLayout>
                    <LinearLayout android:gravity="center" android:orientation="vertical" android:id="@+id/header_user_profile_groups_container"  android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="33.0" style="?android:attr/borderlessButtonStyle">

                        <TextView android:textSize="14.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_groups_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="-2.0dip" android:text="@string/groups"
                            android:fontFamily="sans-serif-light" />

                        <TextView android:textSize="26.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_groups" android:layout_width="wrap_content" android:layout_height="wrap_content"
                            android:fontFamily="sans-serif-light" />
                    </LinearLayout>
                </TableRow>

                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">

                    <LinearLayout android:gravity="center" android:orientation="vertical" android:id="@+id/header_user_profile_photos_container"  android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="33.0" style="?android:attr/borderlessButtonStyle">

                        <TextView android:textSize="14.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_photos_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="-2.0dip" android:text="@string/photos"
                            android:fontFamily="sans-serif-light" />

                        <TextView android:textSize="26.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_photos" android:layout_width="wrap_content" android:layout_height="wrap_content"
                            android:fontFamily="sans-serif-light" />
                    </LinearLayout>
                    <LinearLayout android:gravity="center" android:orientation="vertical" android:id="@+id/header_user_profile_videos_container"  android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="33.0" style="?android:attr/borderlessButtonStyle">

                        <TextView android:textSize="14.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_videos_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="-2.0dip" android:text="@string/videos"
                            android:fontFamily="sans-serif-light" />

                        <TextView android:textSize="26.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_videos" android:layout_width="wrap_content" android:layout_height="wrap_content"
                            android:fontFamily="sans-serif-light" />
                    </LinearLayout>
                    <LinearLayout android:gravity="center" android:orientation="vertical" android:id="@+id/header_user_profile_audios_container"  android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="33.0" style="?android:attr/borderlessButtonStyle">

                        <TextView android:textSize="14.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_audios_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="-2.0dip" android:text="@string/audios"
                            android:fontFamily="sans-serif-light" />

                        <TextView android:textSize="26.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_audios" android:layout_width="wrap_content" android:layout_height="wrap_content"
                            android:fontFamily="sans-serif-light" />
                    </LinearLayout>
                </TableRow>
            </TableLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:id="@+id/user_name"
                android:textColor="#ffffffff"
                android:textSize="34sp"
                android:layout_above="@+id/age_city"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#ffffffff"
                android:id="@+id/online_status"
                android:textSize="15sp"
                android:layout_alignBottom="@+id/fragment_user_profile_avatar"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="35dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:id="@+id/age_city"
                android:textColor="#FFFFFFFF"
                android:textSize="16sp"
                android:layout_above="@+id/online_status"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>

        <ExpandableListView
            android:id="@+id/expandable_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cacheColorHint="#00000000"
            android:listSelector="@android:color/transparent"
            android:textColor="@android:color/white"
            android:transcriptMode="normal"
            android:fastScrollEnabled="false"
            android:footerDividersEnabled="false"
            android:headerDividersEnabled="false"
            android:layout_below="@+id/user_profile"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="false"
            android:layout_alignParentTop="false"
            android:layout_alignWithParentIfMissing="false"
            android:layout_alignParentRight="false"
            android:layout_alignParentBottom="false" />

    </RelativeLayout>
</ScrollView>

当我 运行 我的应用程序时,我可以滚动我的 ListView,但 user_profile 布局只是留在原处。但我也想让它滚动。请帮我。提前致谢。

编辑

你们太棒了!非常感谢您的帮助,现在一切正常!

首先,永远不要将一个可滚动的 container/widget 放入另一个可滚动的容器小部件中 - 这会产生问题(相信我,我试过了 :))

据我了解,您想在顶部添加带有用户个人资料数据的可滚动列表吗?

我会那样做

在适配器覆盖中

@Override
public int getViewTypeCount() {
    return MY_VIEW_TYPE_COUNTER //for example 2;
}

@Override
public int getItemViewType(int position) {
    if (position == 0)
       return USER_PROFILE_TYPE_CONSTANT;
    else
       return NORMAL_LIST_ELEMENT_CONSTANT;
}

然后在 getView 方法中

public View getView(int position, View convertView, ViewGroup parent) {
    int type = getItemViewType(position);
    if(type == USER_PROFILE_TYPE_CONSTANT)
        /*inflate and set correct user profile view*/
    else
       /* inflate and set "normal" list item*/
    return convertView
}

希望我对问题的理解是正确的 :) 如果没有,请澄清

您可能希望完全摆脱 ScrollView 并将 'user_profile' RelativeLayout 放在单独的布局文件中,以便将其作为 header 扩展到您的 ExpandableListView。像这样:

ExpandableListView listView = (ExpandableListView) findViewById(R.id.expandable_list);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.user_profile, listView, false);
listView.addHeaderView(header);

user_profile.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/user_profile"
        android:layout_width="fill_parent"
        android:layout_height="440dp"
        android:layout_alignParentTop="false"
        android:layout_centerHorizontal="true">

        <ImageView
            android:id="@+id/fragment_user_profile_avatar"
            android:layout_width="fill_parent"
            android:layout_height="300dp"
            android:scaleType="centerCrop"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#0277BD"
            android:layout_below="@+id/fragment_user_profile_avatar"
            android:layout_alignParentLeft="false"
            android:layout_alignParentStart="false"
            android:layout_alignParentBottom="false"
            android:id="@+id/tableLayout">

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                <LinearLayout android:gravity="center" android:orientation="vertical" android:id="@+id/header_user_profile_friends_container"  android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="33.0" style="?android:attr/borderlessButtonStyle">

                    <TextView android:textSize="14.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_friends_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="-2.0dip" android:text="@string/friends"
                        android:fontFamily="sans-serif-light" />

                    <TextView android:textSize="26.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_friends" android:layout_width="wrap_content" android:layout_height="wrap_content"
                        android:fontFamily="sans-serif-light" />
                </LinearLayout>
                <LinearLayout android:gravity="center" android:orientation="vertical" android:id="@+id/header_user_profile_followers_container"  android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="33.0" style="?android:attr/borderlessButtonStyle">

                    <TextView android:textSize="14.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_followers_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="-2.0dip" android:text="@string/followers"
                        android:fontFamily="sans-serif-light" />

                    <TextView android:textSize="26.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_followers" android:layout_width="wrap_content" android:layout_height="wrap_content"
                        android:fontFamily="sans-serif-light" />
                </LinearLayout>
                <LinearLayout android:gravity="center" android:orientation="vertical" android:id="@+id/header_user_profile_groups_container"  android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="33.0" style="?android:attr/borderlessButtonStyle">

                    <TextView android:textSize="14.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_groups_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="-2.0dip" android:text="@string/groups"
                        android:fontFamily="sans-serif-light" />

                    <TextView android:textSize="26.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_groups" android:layout_width="wrap_content" android:layout_height="wrap_content"
                        android:fontFamily="sans-serif-light" />
                </LinearLayout>
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <LinearLayout android:gravity="center" android:orientation="vertical" android:id="@+id/header_user_profile_photos_container"  android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="33.0" style="?android:attr/borderlessButtonStyle">

                    <TextView android:textSize="14.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_photos_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="-2.0dip" android:text="@string/photos"
                        android:fontFamily="sans-serif-light" />

                    <TextView android:textSize="26.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_photos" android:layout_width="wrap_content" android:layout_height="wrap_content"
                        android:fontFamily="sans-serif-light" />
                </LinearLayout>
                <LinearLayout android:gravity="center" android:orientation="vertical" android:id="@+id/header_user_profile_videos_container"  android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="33.0" style="?android:attr/borderlessButtonStyle">

                    <TextView android:textSize="14.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_videos_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="-2.0dip" android:text="@string/videos"
                        android:fontFamily="sans-serif-light" />

                    <TextView android:textSize="26.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_videos" android:layout_width="wrap_content" android:layout_height="wrap_content"
                        android:fontFamily="sans-serif-light" />
                </LinearLayout>
                <LinearLayout android:gravity="center" android:orientation="vertical" android:id="@+id/header_user_profile_audios_container"  android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="33.0" style="?android:attr/borderlessButtonStyle">

                    <TextView android:textSize="14.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_audios_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="-2.0dip" android:text="@string/audios"
                        android:fontFamily="sans-serif-light" />

                    <TextView android:textSize="26.0sp" android:textColor="#ffffffff" android:id="@+id/fragment_user_profile_audios" android:layout_width="wrap_content" android:layout_height="wrap_content"
                        android:fontFamily="sans-serif-light" />
                </LinearLayout>
            </TableRow>
        </TableLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/user_name"
            android:textColor="#ffffffff"
            android:textSize="34sp"
            android:layout_above="@+id/age_city"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#ffffffff"
            android:id="@+id/online_status"
            android:textSize="15sp"
            android:layout_alignBottom="@+id/fragment_user_profile_avatar"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="35dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/age_city"
            android:textColor="#FFFFFFFF"
            android:textSize="16sp"
            android:layout_above="@+id/online_status"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>

list_view.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <ExpandableListView
        android:id="@+id/expandable_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="#00000000"
        android:listSelector="@android:color/transparent"
        android:textColor="@android:color/white"
        android:transcriptMode="normal"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:layout_below="@+id/user_profile"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="false"
        android:layout_alignParentTop="false"
        android:layout_alignWithParentIfMissing="false"
        android:layout_alignParentRight="false"
        android:layout_alignParentBottom="false" />

YourActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view);

    // inflate header to add to list view header
    ListView listView = (ListView) findViewById(R.id.expandable_list);
    listView.addHeaderView(getLayoutInflater().inflate(R.layout.user_profile, listView, false));

...add list adapter/other setup