ListView控件与longClick显示和隐藏复选框? [英] ListView with longClick for showing and hiding Checkbox?

查看:131
本文介绍了ListView控件与longClick显示和隐藏复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我沿着 $ C $ READ C这里(网站链接) 。和code已被修改一点点地变成了这个样子:

I read along the code here (weblink). And the code has been modified a little bit to become like this:

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

和另一种对象,我迄今取得:

And also another object I made so far:

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>

我的问题是:

我知道如果我希望把复选框,然后我应该把它下的XML(布局)
但我的情况下,我想使longClick可用于显示复选框即可。
而如何做到这一点?

I know If I want to put Checkbox then I should put it under the XML (layout). But my case is, I want to make the longClick available for showing the Checkboxes. And how to do that?

如果我只是在下面添加此code,当然它会设置的ListView有onLongClick事件......

If I simply add this code below, of course it will set the ListView to have onLongClick event....

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

但是,为了使一次onLongClick执行,我该如何显示组合框?反之亦然......

But to make it once onLongClick executed, how do I show the Combobox? and vice versa....

推荐答案

在XML其简单的附加复选框,并做出了能见度

Its simple add checkbox in xml and make visibility gone

android:visibility="gone"

而在FileArrayAdapter类声明radiobox为你做文本框和把你的

And declare a radiobox in FileArrayAdapter class as you did with textbox and the put your

onItemLongClickListener();

在该检查,如果复选框是可见的,然后让它消失或不可见使其可见。

In that check if checkbox is visible then make it disappear or if not visible the make it visible.

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

这就是的吧。

这篇关于ListView控件与longClick显示和隐藏复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆