ListView 适配器的页眉和页脚

发布于 2024-10-31 19:44:14 字数 1657 浏览 0 评论 0原文

try {           
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
      sb.append(line);
    }
    is.close();
    resultString = sb.toString();
    String[] ArrayStr = Func.split(resultString, "|*|");
    String[] arr;

    lv = new ListView(context);
    lv.setAdapter(null);        
    // populate
    ArrayList<Device> m_Devices = new ArrayList<Device>();
    Device device;
    for(int i = 0; i < ArrayStr.length; i++){
        arr = Func.split(ArrayStr[i], "|**|");
        device = new Device(arr[1], Integer.parseInt(arr[0]));
        m_Devices.add(device);
    }
    // +header n footer (must above custom adapter)

    View v1 = getLayoutInflater().inflate(R.layout.header, null);
    lv.addHeaderView(v1);


    View v = getLayoutInflater().inflate(R.layout.footer, null);
    lv.addFooterView(v);



    // custom adapter
    CustomAdapter lvAdapter =  new CustomAdapter(context, m_Devices);
    lv.setAdapter(lvAdapter);
    lv.setOnItemClickListener(new OnItemClickListener() {
    @Override

    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
    AlertDialog.Builder adb=new AlertDialog.Builder(Menu1.this);
    adb.setTitle("LVSelectedItemExample");
    adb.setMessage("Selected Item is = "+lv.getItemAtPosition(position));
    adb.setPositiveButton("Ok", null);
    adb.show();
    }

    });

如何使页眉和页脚变为静态? 页眉和页脚也可以滚动,就像列表视图一样。 我有谷歌,有人建议 4 将页眉和页脚放在列表视图之外,但我不知道放在哪里?我已经尝试将它放置到我的自定义适配器中,但没有什么好处...所以有人可以告诉我如何解决这个问题吗?

try {           
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
      sb.append(line);
    }
    is.close();
    resultString = sb.toString();
    String[] ArrayStr = Func.split(resultString, "|*|");
    String[] arr;

    lv = new ListView(context);
    lv.setAdapter(null);        
    // populate
    ArrayList<Device> m_Devices = new ArrayList<Device>();
    Device device;
    for(int i = 0; i < ArrayStr.length; i++){
        arr = Func.split(ArrayStr[i], "|**|");
        device = new Device(arr[1], Integer.parseInt(arr[0]));
        m_Devices.add(device);
    }
    // +header n footer (must above custom adapter)

    View v1 = getLayoutInflater().inflate(R.layout.header, null);
    lv.addHeaderView(v1);


    View v = getLayoutInflater().inflate(R.layout.footer, null);
    lv.addFooterView(v);



    // custom adapter
    CustomAdapter lvAdapter =  new CustomAdapter(context, m_Devices);
    lv.setAdapter(lvAdapter);
    lv.setOnItemClickListener(new OnItemClickListener() {
    @Override

    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
    AlertDialog.Builder adb=new AlertDialog.Builder(Menu1.this);
    adb.setTitle("LVSelectedItemExample");
    adb.setMessage("Selected Item is = "+lv.getItemAtPosition(position));
    adb.setPositiveButton("Ok", null);
    adb.show();
    }

    });

How to make the header and footer become static?
the header n footer become scrollable too, like the list view..
i had google n there is someone sugest 4 placing the header and footer outside the listview, but i don't know place to where ? i'm already try place it to my custom adapter n there's no good... so plz anybody can tell me how to fix this problem?

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

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

发布评论

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

评论(1

老街孤人 2024-11-07 19:44:14

我认为将它们放在 ListView 之外意味着您应该使用自定义视图元素,例如放置在 ListView 上方和下方的 TextView。这些元素与 ListView 无关。因此,您无法再使用 lv.addHeaderView(v1);lv.addFooterView(v); 访问它们。相反,您必须像普通 TextView 元素一样对待它们。当然,您也可以使用自定义视图来代替 TextView。

更新

您的活动的布局应该类似于这样。第一个 TextView 是页眉,后面是 ListView,下面是另一个 TextView 作为页脚。页眉和页脚不需要 TextView,它们可以是任何类型的 View。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/header_txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Footer Text"/>
    <ListView android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/footer_txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Footer Text"/>
</LinearLayout>

因此,您将像之前一样将适配器绑定到 ListView。要设置页眉和页脚的文本,请使用以下行。

TextView header = (TextView) findViewById(R.id.header_text);
header.setText("This is my header text");
TextView footer = (TextView) findViewById(R.id.footer_text);
footer.setText("This is my footer text");

I think placing them outside of the ListView means that you should use custom view elements e.g. TextViews which you place above and below the ListView. These elements have nothing to do with the ListView anymore. So you cannot access them with lv.addHeaderView(v1); or lv.addFooterView(v); anymore. Instead you have to treat them like normal TextView elements. Of course you can also use custom Views instead of TextViews.

Update

The layout for you activity should look something like that. The first TextView is the header, followed by the ListView and below this we got another TextView as a footer. Header and footer don't need to TextViews they can be any kind of View.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/header_txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Footer Text"/>
    <ListView android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/footer_txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Footer Text"/>
</LinearLayout>

So you will bind the adapter to the ListView as you already did. To set the text for the header and footer you use the following lines.

TextView header = (TextView) findViewById(R.id.header_text);
header.setText("This is my header text");
TextView footer = (TextView) findViewById(R.id.footer_text);
footer.setText("This is my footer text");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文