在android中的ListView中添加标题

发布于 2024-12-05 16:38:50 字数 58 浏览 0 评论 0原文

我需要在列表视图中添加标题,并且标题应该始终显示,甚至滚动列表。

安卓列表视图可以吗?

I need add header in my listview and header should always displayed even scrolling list.

Is it possible in android list view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-12-12 16:38:50

对我有用的解决方案是创建一个 TableLayout,其中一行包含标题,一行包含列表,如下所示:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <include layout="@layout/header" />

   <ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</TableLayout>

请确保 ListView 将 @android:id/list 作为 ID。

The solution that works for me is to create a TableLayout that has a row with the heading and a row with the list like this:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <include layout="@layout/header" />

   <ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</TableLayout>

Please make sure that the ListView has @android:id/list as the ID.

妄想挽回 2024-12-12 16:38:50

您可以在列表视图中添加标题,如下所示:

View headerView = ((LayoutInflater)Activity.this.getSystemService(Activity.this.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.header, null, false);
list.addHeaderView(headerView)

you can add header in list view like this:

View headerView = ((LayoutInflater)Activity.this.getSystemService(Activity.this.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.header, null, false);
list.addHeaderView(headerView)
Spring初心 2024-12-12 16:38:50

是的,这就是我所做的:

showcase

对于带有 ListActivity 或 < code>ListFragment,您的布局必须包含 ListView,且 android:id 属性设置为 @android:id/list

1) main_layout.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    // custom Headers
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/systolic" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/diastolic" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/date" />
    </LinearLayout>

    // This is important to be exactly like below ,android:id="@android:id/list"

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout1" >
    </ListView>

</RelativeLayout>

用于列表每行的自定义布局,

2) custom_list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/bpSysHolder"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_gravity="center_vertical"
        android:text="TextView" >
    </TextView>

    <TextView
        android:id="@+id/bpDiaHolder"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_gravity="center_vertical"
        android:text="TextView" >
    </TextView>

    <TextView
        android:id="@+id/bpDtHolder"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_gravity="center_vertical"
        android:text="TextView" >
    </TextView>

</LinearLayout>

3) ListActivity

public class HistoryActivity extends ListActivity {
        private SimpleCursorAdapter dbAdapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // this is neccessery for you
            setContentView(R.layout.main_layout);

            dao = new BpDAO(this);

            Cursor bpList = dao.fetchAll_bp();

            String[] from = new String[] { BpDAO.bp_SYS, BpDAO.bp_DIA, BpDAO.bp_DT };
            int[] target = new int[] { R.id.bpSysHolder, R.id.bpDiaHolder,
                    R.id.bpDtHolder };

            // And this for custom row layout for each list row
            dbAdapter = new SimpleCursorAdapter(this, R.layout.custom_list_row,
                    bpList, from, target);

            setListAdapter(dbAdapter);
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            dao.close();
        }

        @Override
        public void onListItemClick(ListView l, View view, int position, long id) {
            // TODO something on item click

        }
    }

如果您需要它检查:“将粘性(固定)页眉/页脚附加到列表视图"

Yes, here is what i 've done:

showcase

For a custom layout with ListActivity or ListFragment, your layout must contain a ListView with the android:id attribute set to @android:id/list.

1) main_layout.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    // custom Headers
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/systolic" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/diastolic" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/date" />
    </LinearLayout>

    // This is important to be exactly like below ,android:id="@android:id/list"

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout1" >
    </ListView>

</RelativeLayout>

For custom layout for each row of list,

2) custom_list_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/bpSysHolder"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_gravity="center_vertical"
        android:text="TextView" >
    </TextView>

    <TextView
        android:id="@+id/bpDiaHolder"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_gravity="center_vertical"
        android:text="TextView" >
    </TextView>

    <TextView
        android:id="@+id/bpDtHolder"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_gravity="center_vertical"
        android:text="TextView" >
    </TextView>

</LinearLayout>

3) ListActivity:

public class HistoryActivity extends ListActivity {
        private SimpleCursorAdapter dbAdapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // this is neccessery for you
            setContentView(R.layout.main_layout);

            dao = new BpDAO(this);

            Cursor bpList = dao.fetchAll_bp();

            String[] from = new String[] { BpDAO.bp_SYS, BpDAO.bp_DIA, BpDAO.bp_DT };
            int[] target = new int[] { R.id.bpSysHolder, R.id.bpDiaHolder,
                    R.id.bpDtHolder };

            // And this for custom row layout for each list row
            dbAdapter = new SimpleCursorAdapter(this, R.layout.custom_list_row,
                    bpList, from, target);

            setListAdapter(dbAdapter);
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            dao.close();
        }

        @Override
        public void onListItemClick(ListView l, View view, int position, long id) {
            // TODO something on item click

        }
    }

In case you need it check: "Attaching a sticky (fixed) header/footer to a listview"

栖竹 2024-12-12 16:38:50

做一件事。在列表视图上方放置一个 TextView 并将文本设置为标题。它可以满足您的需求。

Do one thing. Put one TextView just above the list view and set text as your header .It will achieve your need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文