微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
    }
});

原文地址:https://www.jb51.cc/android/431125.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐