如何在列表视图中禁用所有元素(也是屏幕中当前不可见的元素)?

时间:2014-09-30 12:50:14

标签: android listview

我有一组带有一组元素的ListView。当我点击其中一个时我想禁用所有其他的。如果我再次单击所选项目,则会再次启用所有其他项目。

我尝试了不同的提议解决方案但没有成功。我希望有人可以帮助我。

这是我的代码:

//action to take when a presentation is selected
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
      public void onItemClick(AdapterView<?> parent, final View view,
          int position, long id) {
        //disable the other items when one is selected
         if(position!=selectedPresentation){
              for(int i=0;i<parent.getCount();i++){
                  if(i!=position)//disable all the items except the select one
                        parent.getChildAt(i).setEnabled(false);
              }
              selectedPresentation=position;

          }else if(selectedPresentation==position){
              //enable again all the items
              for(int i=0;i<parent.getCount();i++){
                  parent.getChildAt(i).setEnabled(true);
              }
              selectedPresentation=-1;

          }

其中selectedPresentation是存储所选项目的全局变量。如果未选择任何项目,则其值为-1。

感谢您的帮助!

4 个答案:

答案 0 :(得分:0)

创建自己的ArrayAdapter子类,其中AreAllItemsEnabled()返回false,并定义isEnabled(int position)以返回您要禁用的项目中给定项目的false。

在您的自定义ArrayAdapter overEnabled方法中,如下所示

@Override
public boolean isEnabled(int position) {
    return false;
}

其他选项

不要实施onItemclickListener。这不会为您提供项目click的任何更新。只将onClick监听器注册到views

答案 1 :(得分:0)

您可以使用List(Arraylist)中的另一个对象,它在listview中填充元素,然后单击将相应的位置数据复制到您创建的新arraylist然后再通过notifyDataSet,当您再次单击时,使用原始列表填充listview,这样它将会apear再次....只是做这个可能适合你的技巧

答案 2 :(得分:0)

parent.getChildeAt()仅适用于可见Item。 你应该在适配器中改变一些东西 如果制作自定义适配器然后你可以做一些像@ṁᾶƔƏňツ答案,但如果你使用默认适配器,你可以更改适配器的arraylist,然后再将它传递给适配器。
将布尔值放入其中(在arraylist中)并在项目中单击true / false将其放入所有项目。

答案 3 :(得分:0)

我写了这个解决方案,似乎工作正常!

            listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                  @Override
          public void onItemClick(AdapterView<?> parent, final View view,
              int position, long id) {
            //disable the other items when one is selected
                        Toast.makeText(context, "onClick called",Toast.LENGTH_SHORT).show(); // this will flash up twice

                      if(position!=selectedPresentation){
                         selectedPresentation=position;
                  for(int i=0;i<adapter.getCount();i++){
                      if(i!=position)//disable all the items except the select one
                            adapter.isEnabled(i);
                  }


              }else if(selectedPresentation==position){
                  //enable again all the items
                  selectedPresentation=-1;
                  for(int i=0;i<adapter.getCount();i++){
                      adapter.isEnabled(i);
                  }


              }

在我的适配器中我写道:

public boolean isEnabled(int position) {
   if(selectedPresentation==-1 || selectedPresentation==position)
       return true;
   return false;

现在我关心的是如何将listView中的项目显示为已禁用

相关问题