在 ListView 之上添加一个 View 并在向下滚动时移动所有内容?
Add a View on top of a ListView and move everything when scrolling down?
我想在列表视图顶部添加一个视图操作(我们称之为 header)。当用户向下滚动时,我希望所有内容都向上移动(包括 header)。但是现在,当用户滚动时,header 固定在顶部(如 "position:fixed" 在 css 中),而不是与其余内容一起滚动。
如何让用户滚动所有内容而不仅仅是 ListView 的内容?
这是我现在的布局:
<?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:background="?attr/color2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/header"
style="@style/header"
android:drawableLeft="@drawable/ic_setup"
android:text="@string/fragment_contacts"/>
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"
android:fastScrollEnabled="false">
</ListView>
</LinearLayout>
my_layout_header.xml
这是您列表 header 的 XML 布局。
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_setup"
android:text="@string/fragment_contacts" />
my_layout.xml
这是主布局。
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list"
android:drawSelectorOnTop="false"
android:fastScrollEnabled="false" />
代码
现在您需要将 header 布局 XML 膨胀为 View
并将其设置为您的 ListView
的 header 视图。可以在 Activity.onCreate(Bundle)
:
中使用以下代码片段
setContentView(R.layout.my_layout);
View header = View.inflate(this, R.layout.my_layout, null);
// where "this" is a context / activity...
ListView list = (ListView)findViewById(android.R.id.list);
list.addHeaderView(header, null, false); // header will not be clickable
// ... whatever else you need to do AFTER you set a header (a bug before KITKAT) ...
我想在列表视图顶部添加一个视图操作(我们称之为 header)。当用户向下滚动时,我希望所有内容都向上移动(包括 header)。但是现在,当用户滚动时,header 固定在顶部(如 "position:fixed" 在 css 中),而不是与其余内容一起滚动。
如何让用户滚动所有内容而不仅仅是 ListView 的内容?
这是我现在的布局:
<?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:background="?attr/color2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/header"
style="@style/header"
android:drawableLeft="@drawable/ic_setup"
android:text="@string/fragment_contacts"/>
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"
android:fastScrollEnabled="false">
</ListView>
</LinearLayout>
my_layout_header.xml
这是您列表 header 的 XML 布局。
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_setup"
android:text="@string/fragment_contacts" />
my_layout.xml
这是主布局。
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list"
android:drawSelectorOnTop="false"
android:fastScrollEnabled="false" />
代码
现在您需要将 header 布局 XML 膨胀为 View
并将其设置为您的 ListView
的 header 视图。可以在 Activity.onCreate(Bundle)
:
setContentView(R.layout.my_layout);
View header = View.inflate(this, R.layout.my_layout, null);
// where "this" is a context / activity...
ListView list = (ListView)findViewById(android.R.id.list);
list.addHeaderView(header, null, false); // header will not be clickable
// ... whatever else you need to do AFTER you set a header (a bug before KITKAT) ...