ScrollView 中的 ListView 潜在解决方法 [英] ListView in ScrollView potential workaround

查看:24
本文介绍了ScrollView 中的 ListView 潜在解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了有关此事的所有研究.我知道 Google 认为这毫无意义,而开发人员也知道事实并非如此.我也知道没有已知的解决方法,但我知道我接近制作一个.用户 DougW 发布了此代码:

I've done all of the research on the matter. I know that Google thinks it's pointless and that the developers, know that it's not. I also know that there is no known workaround, but I know that I am close to making one. The user DougW posted this code:

public class Utility {
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); 
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
}

这几乎为我完成了工作.但是当我尝试它时,我在 listItem.measure(0, 0) 行得到一个 NullPointer 异常.listItem 本身已初始化,但该方法无论如何都会抛出异常.请告诉我如何解决这个问题.

Which almost gets the job done for me. But when I try it, I get a NullPointer exception at the listItem.measure(0, 0) line. The listItem itself is initialized, but the method throws the exception anyway. Please tell me how I can fix this.

这是我的代码:

public class ExpenseReportsActivity extends Activity {

    private ListView lvReports;
    private ExpenseReportListAdapter adapter;
    private Button btnSend;
    private Button btnCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expensereports);

        lvReports = (ListView)findViewById(R.id.lv_reports);
        lvReports.setBackgroundResource(R.drawable.shape_expense_report_list);

        ColorDrawable cd = new ColorDrawable(0xFFffffff);
        lvReports.setDivider(cd);
        lvReports.setDividerHeight(1);

        adapter = new ExpenseReportListAdapter(this);
        lvReports.setAdapter(adapter);

        int totalHeight = 0;
        for (int i = 0; i < adapter.getCount(); i++) {
            View listItem = adapter.getView(i, null, lvReports);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = lvReports.getLayoutParams();
        params.height = totalHeight + (lvReports.getDividerHeight() * (adapter.getCount() - 1));
        lvReports.setLayoutParams(params);
    }
}

我正在研究的另一种解决方法是使用我的自定义视图的 onWindowFocusChanged 方法.它告诉视图的确切高度.问题是当我仍然在我的 Activity 的 onCreate 方法中时,事件没有被触发,也没有在我的 Activity 的 onWindowFocusChanged 方法中.我尝试了一个自定义事件,但它从未触发(它被放置在我的自定义视图的 onWindowFocusChanged 方法中,而侦听器在我的 Activity 的 onWindowFocusChanged 方法中).

Another workaround I am working on is using my custom view's onWindowFocusChanged method. It tells the exacts height of the view. The problem is that the event isn't fired while I am still in my Activiy's onCreate method, nor in my Activity's onWindowFocusChanged method. I tried a custom event, but it never fired (it was placed inside my custom view's onWindowFocusChanged method and the listener was in my Activity's onWindowFocusChanged method).

推荐答案

好吧,就我得到你的需求而言,我认为你可以使用 ListView.addFooterView(View v) 方法:

Ok, as far as I got your needs I think you may just use the ListView.addFooterView(View v) method:

http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)

它将允许您将所有列表项+几个按钮"页脚作为一个块滚动.

It will allow you to have all your list items + "a few buttons" footer to be scrolled as a single block.

所以代码应该是这样的:

So the code should be smth like that:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;

public class YourActivity extends ListActivity {

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

        LayoutInflater factory = getLayoutInflater();
        LinearLayout footer = 
            (LinearLayout) factory.inflate(R.layout.your_a_few_buttons_footer, null);

        getListView().addFooterView(footer);

        String[] array = new String[50];
        for (int i = 0; i < 50;) { array[i] = "LoremIpsum " + (++i); }

        setListAdapter(
            new ArrayAdapter<String>(this, R.layout.list_item, array)
        );
    }   
}

注意,文档说 addFooterView() 应该在 setListAdapter() 之前调用.

Note, the doc says addFooterView() should be called BEFORE the setListAdapter().

UPDATE:使用ListView.addHeaderView(View v)在列表顶部添加一个View.请注意,例如,LinearLayout 也是一个 View.所以你可以把任何你想要的东西作为页眉或页脚,它会随着列表滚动,作为一个不可分割的块.

UPDATE: to add a View at the top of the list use ListView.addHeaderView(View v). Note that, for instance, LinearLayout is also a View. So you can put anything you want as a header or a footer and it'll be scrolled with the list as an indivisible block.

这篇关于ScrollView 中的 ListView 潜在解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆