ListView with longClick用于显示和隐藏Checkbox?

时间:2014-08-25 17:52:29

标签: java android listview checkbox android-listview

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

FileArrayAdapter.java

public class FileArrayAdapter extends ArrayAdapter<Item> {

    private Context c;
    private int id;
    private List<Item> items;

    public FileArrayAdapter(Context context, int textViewResourceId,
            List<Item> objects) {
        super(context, textViewResourceId, objects);
        c = context;
        id = textViewResourceId;
        items = objects;
    }

    public Item getItem(int i) {
        return items.get(i);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) c
                    .getSystemService(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 Comparable<Item>{
    private String name;
    private String data;
    private String date;
    private String path;
    private String image;

    public Item(String n,String d, String dt, String p, String img)
    {
            name = n;
            data = d;
            date = dt;
            path = p;
            image = img;           
    }
    public String getName()
    {
            return name;
    }
    public String getData()
    {
            return data;
    }
    public String getDate()
    {
            return date;
    }
    public String getPath()
    {
            return path;
    }
    public String getImage() {
            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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:contentDescription="@string/file_sharing_file_folder"
        android:id="@+id/fd_Icon1"
        android:layout_width="50dip"
        android:layout_height="50dip" >
    </ImageView>

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="5dip"
        android:layout_toRightOf="@+id/fd_Icon1"
        android:singleLine="true"
        android:text="@+id/TextView01"
        android:textStyle="bold" >
    </TextView>

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TextView01"
        android:layout_marginLeft="10dip"
        android:layout_toRightOf="@+id/fd_Icon1"
        android:text="@+id/TextView02" >
    </TextView>

    <TextView
        android:id="@+id/TextViewDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/TextView01"
        android:layout_marginLeft="5dip"
        android:text="@+id/TextViewDate" >
    </TextView>

</RelativeLayout>

我的问题是:

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

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

dList.setLongClickable(true);

dList.setOnItemLongClickListener(new OnItemLongClickListener() {

    public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
            int pos, long id) {

        // show the checkbox of each lines

        return true;
    }
});

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

2 个答案:

答案 0 :(得分:3)

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

@Override
public View getView(int position, View convertView, 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(AdapterView<?> arg0, View arg1, int pos, long id) {
        showCheckBoxes = !showCheckBoxes;
        fileArrayAdapter.notifyDataSetChanged();
        return true;
    }
});

答案 1 :(得分:1)

xml中的简单添加复选框,使可见性消失

android:visibility="gone"

并在FileArrayAdapter类中声明一个radiobox,就像使用文本框和放置

一样
onItemLongClickListener();

在该检查中,如果复选框可见,则使其消失或如果不可见则使其可见。

if(cb.isVisible()){
    cb.setVisibility(View.GONE);
}else{
    cb.setVisibility(View.VISIBLE);
}

多数民众赞成的。