首页 > 解决方案 > 仅滚动 ListView 并将 LinearLayout 标题留在顶部

问题描述

我有一个设计,其中有一小部分屏幕专门用于显示用户的个人资料图像。下面,由分隔符分隔的是 ListView 中包含的项目列表。我希望当我滚动列表时,配置文件的顶部视图将保留在那里并且不可滚动,因此只有列表是可滚动的:

目前有一个 ListView 膨胀onCreate,然后header.xml添加一个包含顶部图像的图像,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.word_list);
    ListView listView = (ListView) findViewById(R.id.list);
    listView.addHeaderView(getLayoutInflater().inflate(R.layout.header, null));

这是word_list.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawSelectorOnTop="true"
    android:orientation="vertical" />

这是 header.xml:

<LinearLayout
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/headerImageView"
        android:layout_width="match_parent"
        android:layout_height="119dp"
        app:srcCompat="@drawable/profile_image" />

     <View
        android:id="@+id/headerDivider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="?android:attr/listDivider" />

这就是我想要实现的目标:

在此处输入图像描述

标签: android

解决方案


your view will look like this :

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

<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:paddingLeft="16dp"
 android:id="@+id/register_container"
 android:paddingRight="16dp"
 android:background="#FFF">

     <ImageView
      android:id="@+id/headerImageView"
      android:layout_width="match_parent"
      android:layout_height="0"
      android:layout_weight="1"
      app:srcCompat="@drawable/profile_image"/>
     <View
      android:id="@+id/headerDivider"
      android:layout_width="match_parent"
      android:layout_height="1dp"
      android:background="?android:attr/listDivider" 
      android:layout_marginBottom="4dp"/>

     <ListView
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight="3"
     android:id="@+id/list"/>

</LinearLayout>

推荐阅读