在ScrollView中为ListView进行自定义可视化

时间:2012-09-03 12:20:47

标签: android listview scrollview listadapter

我有一个包含很多项目的ScrollView。在它的底部,我从另一个Activity添加一组项目。我检查来自另一个Activity的输入ArrayList,并且应该使用ScrollView向我的Activity添加正确的复选计数。我在LinearLayout的帮助下添加项目:

  public void addFiles()
        {

            adapter = new ArrayAdapter<File>(this,
                    R.layout.new_order, R.id.fileCheck,
                    FileManagerActivity.finalAttachFiles) 
                    {
                @Override
                public View getView(final int position, View convertView, ViewGroup parent) {
                    // creates view

                    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    View view = inflater.inflate(R.layout.file, null);
                    view.setFocusable(false);
                    CheckBox textView = (CheckBox) view
                            .findViewById(R.id.fileCheck);
                    textView.setClickable(true);
                    textView.setText(FileManagerActivity.finalAttachFiles.get(position).getName().toString());
                    textView.setTextColor(Color.BLACK);
                    textView.setPadding(55, 0, 0, 0);
                    textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.file_icon, 0, 0, 0);
                    textView.setTextSize(16);
                    textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                    int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
                    textView.setCompoundDrawablePadding(dp5);
                    textView.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {


                            if(((CheckBox)v).isChecked()){
                              checks.set(position, 1);
                            }
                            else{
                             checks.set(position, 0);
                            }

                        }
                    });
                    return view;

                }
            };
}

我知道在ScrollView中使用ListView并不是推荐的,但我尝试使用LinearLayout作为我的目标,但它工作不好 - 它无法正确更新。要以正确的方式显示ListView,我使用这样的类:

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

        int totalHeight = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
        Log.i("ListAdapter count",Integer.toString(listAdapter.getCount()));
        for (int i = 0; i < listAdapter.getCount(); i++) {

            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();
        }

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

在每次数据更改后使用它:

fileList = (ListView) findViewById(R.id.fileListView);
    fileList.setAdapter(adapter);
    UtilityListView.setListViewHeightBasedOnChildren(fileList);

但ListView的高度远远大于列表项的数量......我该如何解决?

1 个答案:

答案 0 :(得分:0)

我使用带有页脚和标题的ListView解决了这个问题。在ScrollView中使用ListView的所有尝试都是不可行或不高效的。我是这样做的:

    View header = getLayoutInflater().inflate(R.layout.header_listview, null);
    View footer = getLayoutInflater().inflate(R.layout.footer_listview, null);
    fileList.addHeaderView(header);
    fileList.addFooterView(footer);
    fileList.setCacheColorHint(Color.WHITE);
    fileList.setAdapter(adapter);

我的所有项目都显示正确,我可以获得功能,我想要的。