列出滚动后无法选择的项目

时间:2012-08-08 15:08:06

标签: android listview

我遇到包含ListView的Activity的问题。加载Activity时,将填充ListView,我可以点击行。但是,一旦我滚动,项目就会停止点击。为了能够点击一个项目,我必须旋转屏幕(注意:我在Manifest中为此Activity设置了android:configChanges="keyboardHidden|orientation"设置,因此在方向更改时甚至没有销毁活动)。我不太确定问题是什么。我有很多包含ListViews的活动,但没有一个有这个问题。有没有人看到任何错误?

更新:我删除了关于页脚的getView开头的代码。现在一切正常。这段代码有什么问题?我的页脚实际上是一个阴影,但如果ListView(读取:ListView中的项目)占据整个屏幕,我不希望它存在。

这是我的活动代码。

public class ActivitySelector extends MyActivity implements OnItemClickListener, OnClickListener {

    private ArrayList<MyObject> mItems;
    private ActivitySelectorAdapter mAdapter;

    private ListView mList;
    private LinearLayout mFooter;
    private Button mDone;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_filterselector);

        super.onCreate(savedInstanceState);

        mItems = new ArrayList<MyObject>();

        getDataFromIntent();
        setUpUI();
    }

    public void onClick(View v) {
        if (v.getId() == R.id.activityselector_menu) {
            super.openSlider();
        }
        else if (v.getId() == R.id.activityselector_done) {
            this.onBackPressed();
        }
        else {
            super.onClick(v);
        }
    }

    public void onItemClick(AdapterView<?> l, View v, int position, long id) {
        if (l.getId() == R.id.activityselector_list) {
            if (position < mItems.size()) {
                mItems.get(position).setSelected(!mItems.get(position).getSelected());
                mAdapter.notifyDataSetChanged();
            }
        }
    }


    private void getDataFromIntent() {
        Bundle data = getIntent().getExtras();
        if (data != null) {
            if (data.containsKey("items")) {
                mItems = data.getParcelableArrayList("items");
            }
        }
    }


    private void setUpUI() {
        mDone = (Button) findViewById(R.id.activityselector_done);
        mDone.setOnClickListener(this);

        mList = (ListView) findViewById(R.id.activityselector_list);
        mFooter = (LinearLayout) ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_listfooter, mList, false);
        mList.addFooterView(mFooter, null, false);

        mAdapter = new ActivitySelectorAdapter(ActivitySelector.this, R.layout.layout_activityselector_item, mItems);
        mList.setAdapter(mAdapter);
        mList.setOnItemClickListener(this);
    }








    static class ActivitySelectorHolder {
        TextView txtName;
        TextView txtCount;
        ImageView imgCheck;
    }

    public class ActivitySelectorAdapter extends ArrayAdapter<MyObject> {

        private LayoutInflater mInflater;
        ArrayList<MyObject> items;

        public ActivitySelectorAdapter(Context context, int textViewResourceId, ArrayList<MyObject> items) {
            super(context, textViewResourceId, items);
            mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (mList.getFirstVisiblePosition() > 0) {
                if (mList.getFooterViewsCount() > 0) {
                    mList.removeFooterView(mFooter);
                }
            }
            else {
                if (mList.getFooterViewsCount() < 1) {
                    mList.addFooterView(mFooter, null, false);
                }
            }

            ActivitySelectorHolder holder = null;

            if (null == convertView) {
                holder = new ActivitySelectorHolder();
                convertView = mInflater.inflate(R.layout.layout_activityselector_item, null);
                holder.txtName = (TextView) convertView.findViewById(R.id.activityselector_item_name);
                holder.txtCount = (TextView) convertView.findViewById(R.id.activityselector_item_count);
                holder.imgCheck = (ImageView) convertView.findViewById(R.id.activityselector_item_check);
                convertView.setTag(holder);
            }
            else {
                holder = (ActivitySelectorHolder)convertView.getTag();
            }

            if (holder.txtName != null) {
                holder.txtName.setText(items.get(position).getName());
            }

            if (holder.txtCount != null) {
                holder.txtCount.setText("(" + String.valueOf(items.get(position).getCount()) + ")");
            }

            if (holder.imgCheck != null) {
                if (items.get(position).getSelected()) {
                    holder.imgCheck.setImageResource(R.drawable.selector_checkmark_reverse);
                }
                else {
                    holder.imgCheck.setImageResource(R.drawable.selector_checkmark);
                }
            }

            return convertView;
        }
    }
}

0 个答案:

没有答案