Java 带长单击的ListView用于显示和隐藏复选框?

Java 带长单击的ListView用于显示和隐藏复选框?,java,android,listview,checkbox,android-listview,Java,Android,Listview,Checkbox,Android Listview,我一直在阅读,代码被修改了一点,如下所示: FileArrayAdapter.java public class FileArrayAdapter extends ArrayAdapter<Item> { private Context c; private int id; private List<Item> items; public FileArrayAdapter(Context context, int textViewRes

我一直在阅读,代码被修改了一点,如下所示:

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"));
    }
}
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();
    } }
公共类FileArrayAdapter扩展了ArrayAdapter{
私有上下文c;
私有int-id;
私人清单项目;
公共文件ArrayAdapter(上下文,int textViewResourceId,
列出对象){
超级(上下文、textViewResourceId、对象);
c=上下文;
id=textViewResourceId;
项目=对象;
}
公共项getItem(int i){
返回项目。获取(i);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)c
.getSystemService(上下文布局\充气机\服务);
v=vi.充气(id,空);
}
/*创建我的布局的新视图并在行中充气*/
//convertView=(RelativeLayout)充气器。充气(资源,空);
最终项目o=项目。获取(位置);
如果(o!=null){
TextView t1=(TextView)v.findViewById(R.id.TextView01);
setDefaultTextColor(t1);
TextView t2=(TextView)v.findViewById(R.id.TextView02);
设置默认文本颜色(t2);
TextView t3=(TextView)v.findViewById(R.id.TextViewDate);
设置默认文本颜色(t3);
/*从布局中获取ImageView并设置城市形象*/
ImageView imageCity=(ImageView)v.findViewById(R.id.fd_Icon1);
字符串uri=“drawable/”+o.getImage();
int imageResource=c.getResources().getIdentifier(uri,null,
c、 getPackageName());
Drawable image=c.getResources().getDrawable(imageResource);
imageCity.setImageDrawable(图像);
如果(t1!=null)
t1.setText(o.getName());
如果(t2!=null)
t2.setText(o.getData());
如果(t3!=null)
t3.setText(o.getDate());
}
返回v;
}
私有void setDefaultTextColor(TextView tx){
tx.setTextColor(Color.parseColor(#f8f9fe));
}
}
还有另一件我到目前为止做的东西:

Item.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"));
    }
}
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();
    } }
公共类项实现可比较{
私有字符串名称;
私有字符串数据;
私有字符串日期;
私有字符串路径;
私有字符串图像;
公共项(字符串n、字符串d、字符串dt、字符串p、字符串img)
{
name=n;
数据=d;
日期=dt;
path=p;
图像=img;
}
公共字符串getName()
{
返回名称;
}
公共字符串getData()
{
返回数据;
}
公共字符串getDate()
{
返回日期;
}
公共字符串getPath()
{
返回路径;
}
公共字符串getImage(){
返回图像;
}
公共整数比较(o项){
if(this.name!=null)
返回这个.name.toLowerCase().compareTo(o.getName().toLowerCase());
其他的
抛出新的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(布局)下。 但我的情况是,我想让长点击可以显示复选框。 如何做到这一点

如果我只是在下面添加这段代码,当然它会将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;
    }
});
dList.setLongClickable(true);
dList.setOnItemLongClickListener(新的OnItemLongClickListener(){
长单击(AdapterView arg0、视图arg1、,
内部位置,长id){
//显示每行的复选框
返回true;
}
});

但是,要使它只执行一次LongClick,如何显示组合框?反之亦然……

其简单的xml添加复选框并使可见性消失

android:visibility="gone"
并在FileArrayAdapter类中声明一个radiobox,就像使用textbox和

onItemLongClickListener();
在该复选框中,如果复选框可见,则使其消失;如果不可见,则使其可见

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

就是这样。

一种可能的解决方案是根据一个标志隐藏/显示复选框,该标志在项目收到长时间单击时切换。 在适配器的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;
    }
});
dList.setOnItemLongClickListener(新的OnItemLongClickListener(){
长单击公共布尔值(AdapterView arg0、视图arg1、int pos、长id){
showCheckBoxes=!showCheckBoxes;
fileArrayAdapter.notifyDataSetChanged();
返回true;
}
});

我这样做了,但复选框刚刚出现(我还没有长按),。。。它的复选框选择也起作用了,但是现在listview选择被破坏了@ArchitJain.then add with
longClickListener
cb.onClickListener();并在此
中处理复选框勾选和取消勾选事件这将允许您处理长单击以及复选框onClick
告诉我这是否有效@gumuruhwait。。。我先试试@jeremy,我会回来的。古穆鲁。他再也没有回来。