简体   繁体   English

仅滚动ListView,并在顶部保留LinearLayout标头

[英]Scroll only the ListView and leave the LinearLayout header on top

I have a design where there is a small portion of the screen dedicated to display the user's profile image. 我有一个设计,其中屏幕的一小部分专门用于显示用户的个人资料图像。 Below, separated by a divider is a list of items contained in a ListView. 下面用分隔符分隔的是ListView中包含的项目列表。 I want that when I scroll through the list, that the top view of the profile will stay there and not be scrollable, so that only the list is scrollable: 我希望在列表中滚动时,配置文件的顶视图将保留在那里并且不可滚动,以便仅列表可滚动:

Currently there is a ListView inflated on onCreate , then a header.xml containing the top image is added like so: 当前, onCreate上有一个充气的ListView,然后添加了包含顶部图像的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));

This is the word_list.xml : 这是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" />

This is the header.xml: 这是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" />

This is what I want to achieve: 这是我要实现的目标:

在此处输入图片说明

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>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity.UnderCoordinatorActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">


    <RelativeLayout
        android:id="@+id/main_layout_cordinate"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_margin="5dp"
        android:layout_below="@+id/relativeLayout"
        android:background="@color/home">
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
           >

            <ImageView
                android:id="@+id/headerImageView"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_gravity="center"
                android:src="@drawable/ic_launcher_background"

                />

             <TextView
                  android:id="@+id/voter_tv"
                  android:layout_width="match_parent"
                  android:layout_gravity="center_vertical"
                  android:layout_height="wrap_content"
                  android:textColor="#ffff"
                  android:textStyle="bold"
                  android:gravity="center"
                  android:text="john Doe"/>

        </LinearLayout>
    </RelativeLayout>

    <ListView
        android:id="@+id/list_item_group"
        android:layout_below="@+id/main_layout_cordinate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>





</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM