java – 使用longClick的ListView显示和隐藏Checkbox?

我沿着code here (weblink).阅读并且代码已被修改了一点,变成这样:

FileArrayAdapter.java

public class FileArrayAdapter extends ArrayAdapterstemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(id,null);
        }

        /* create a new view of my layout and inflate it in the row */
        // convertView = ( RelativeLayout ) inflater.inflate( resource,null );

        final Item o = items.get(position);
        if (o != null) {
            TextView t1 = (TextView) v.findViewById(R.id.TextView01);
            setDefaultTextColor(t1);

            TextView t2 = (TextView) v.findViewById(R.id.TextView02);
            setDefaultTextColor(t2);

            TextView t3 = (TextView) v.findViewById(R.id.TextViewDate);
            setDefaultTextColor(t3);

            /* Take the ImageView from layout and set the city's image */
            ImageView imageCity = (ImageView) v.findViewById(R.id.fd_Icon1);
            String uri = "drawable/" + o.getimage();
            int imageResource = c.getResources().getIdentifier(uri,null,c.getPackageName());
            Drawable image = c.getResources().getDrawable(imageResource);
            imageCity.setimageDrawable(image);

            if (t1 != null)
                t1.setText(o.getName());
            if (t2 != null)
                t2.setText(o.getData());
            if (t3 != null)
                t3.setText(o.getDate());
        }
        return v;
    }

    private void setDefaultTextColor(TextView tx) {
        tx.setTextColor(Color.parseColor("#f8f9fe"));
    }
}

还有我到目前为止做的另一个目标:

Item.java

public class Item implements Comparabletimage() {
            return image;
    }
    public int compareto(Item o) {
            if(this.name != null)
                    return this.name.toLowerCase().compareto(o.getName().toLowerCase());
            else
                    throw new IllegalArgumentException();
    } }

列表布局有点定制如下:
listupload_row.xml

我的问题是:

我知道如果我想放置CheckBox,那么我应该把它放在XML(布局)下面.
但我的情况是,我想让longClick可用于显示CheckBoxes.
怎么做?

如果我只是在下面添加这个代码,当然它会将ListView设置为onLongClick事件….

dList.setLongClickable(true);

dList.setonItemLongClickListener(new OnItemLongClickListener() {

    public boolean onItemLongClick(AdapterViewBox of each lines

        return true;
    }
});

但是为了让它一旦亮起执行,我如何展示ComboBox?反之亦然….

最佳答案
一种可能的解决方案是隐藏/显示复选框,该复选框基于当项目收到长按时切换的标志.
在适配器的getView方法中执行以下操作:

@Override
public View getView(int position,ViewGroup parent) {
    ...
    if(showCheckBoxes) {
        v.findViewById(R.id.checkBox).setVisible(View.VISIBLE);
    } else {
        v.findViewById(R.id.checkBox).setVisible(View.GONE);
    }
    ...
}

然后在你的长按监听器中:

dList.setonItemLongClickListener(new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterViewBoxes = !showCheckBoxes;
        fileArrayAdapter.notifyDataSetChanged();
        return true;
    }
});

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...