禁用AdapterView适配器中的项目

时间:2013-07-23 15:54:45

标签: android android-listview android-adapterview onitemclick

enter image description here

基本上我想要当我取消选中第一个项目上的框时,第二个项目是“灰色”并且无法点击或禁用。

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,  long id) {


        if(position == 0) { 
            //disable.enable push
            CheckBox checkBox = (CheckBox) mRoot.findViewById(R.id.cbRowCheckBox);
            checkBox.setChecked(!checkBox.isChecked());

            mPushEnabled = checkBox.isChecked();

            if (mPushEnabled) {
                //code for push enabled
                //code to let position 1 enabled, i was thinking something along th e lines,
               adapter.getposition(1).setEnabled(true);
            } else {
                //code for push disabled
                adapter.getposition(1).setEnabled(false);
            }
        } else if(position == 1) {
            //push notification settings
            //open intent
        }
    }

我在想我可以获得该项目的位置,并将其设置为setEnabled()。这是正确的方法吗?如果是这样,我无法弄清楚如何定位该项目。如果现在,最好的方法是什么?

2 个答案:

答案 0 :(得分:0)

究竟getposition到底是什么?

我认为你不能直接从适配器获取View,因为它们经常被listView重用。

我认为最好的方法是跟踪要启用的项目位置,并相应地从适配器中的getView()或bindView()方法更新视图。

答案 1 :(得分:0)

  

基本上我想要取消选中第一项的方框,第二项   item为“灰色”且无法点击或已禁用。

当您检查CheckBox在适配器级别存储下一行的position时,将被禁用并调用notifyDataSetChanged()

在适配器中首先覆盖isEnabled()方法,如果position参数与先前存储的禁用行位置匹配,则返回false(对于任何其他位置都为true)。这将使该行无法点击。

然后在getView()方法中检查当前位置是否为禁用行的位置,并使其看起来褪色或您想要的其他方式。