0

我遇到了包含 ListView 的 Activity 的问题。当 Activity 加载时,ListView 被填充,我可以点击行就好了。但是,一旦我滚动,这些项目就不再是可点击的了。为了能够单击一个项目,我必须旋转屏幕(注意:我android:configChanges="keyboardHidden|orientation"在 Manifest 中有此 Activity 的设置,因此 Activity 甚至不会在方向更改时被破坏)。我不太确定问题是什么。我有很多包含 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;
        }
    }
}
4

0 回答 0