2

在我的应用程序中,我有一个列表视图,它的项目由图像、文本和线性布局组成。我想在我的自定义适配器类中实现一个 onClickListener,当单击列表中的项目时,线性布局可见性设置为可见或消失。这是问题所在:当我单击列表中的一个项目并滚动列表时,我看到其他项目的线性布局变得可见。(因为视图回收和一切,我知道)。

我怎样才能解决这个问题,只有点击项目的线性布局才能在滚动后获得我想要的可见性?

我的适配器代码如下,没有任何 onClick 事件:

public class myadapter extends ArrayAdapter<lwicon> {

    Context context;
    lwicon[] iteminarow;
    public myadapter(Context context,lwicon[] iteminarow)
    {
        super(context, R.layout.listitem, iteminarow);
        this.context=context;
        this.iteminarow=iteminarow;
    }

    static class ViewHolder
    {
        public ImageView imageview;
        public TextView textview;
        public LinearLayout linearlayout;
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent)
    {

        ViewHolder holder = null;

        if(convertView==null)
        {

         LayoutInflater LI= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         convertView= LI.inflate(R.layout.listitem, parent, false);
         holder = new ViewHolder();
         holder.textview=(TextView)convertView.findViewById(R.id.unitname);
         holder.imageview=(ImageView)convertView.findViewById(R.id.image);
         holder.linearlayout=(LinearLayout)convertView.findViewById(R.id.invisiblablesubitem);

         convertView.setTag(holder);
        }

        else
        {
            holder=(ViewHolder)convertView.getTag();

        }

        holder.imageview.setImageResource(iteminarow[position].icon);
        holder.textview.setText(iteminarow[position].unitname);

        return convertView;
    }
}

列表项布局是这样的:

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

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView android:id="@+id/image"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_gravity="center_vertical"/>
        <TextView  android:id="@+id/unitname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/invisiblablesubitem"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:orientation="vertical"
        android:visibility="gone">
        <TextView android:id="@+id/tt"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="HELLO"/>
    </LinearLayout>
</LinearLayout>

更新:关于 Rishabh.CreatioSoft 答案,我更新了适配器,但没有得到任何答案,每次单击列表视图中的项目时,应用程序崩溃。请帮助...

public class myadapter extends ArrayAdapter<lwicon> {

    Context context;
    lwicon[] iteminarow;
    private LayoutInflater inflater;
    public myadapter(Context context,lwicon[] iteminarow)
    {
        super(context, R.layout.listitem, iteminarow);
        this.context=context;
        this.iteminarow=iteminarow;
         inflater = LayoutInflater.from(context);
    }

    static class ViewHolder
    {
        public ImageView imageview;
        public TextView textview;
        public LinearLayout linearlayout;
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent)

    {
        lwicon item = (lwicon)this.getItem(position);

        ImageView imageView;
        TextView textView;
        LinearLayout linearLayout;


        if(convertView==null)
        {

         convertView=inflater.inflate(R.layout.listitem, parent,false);
         textView=(TextView)convertView.findViewById(R.id.unitname);
         imageView=(ImageView)convertView.findViewById(R.id.image);
         linearLayout=(LinearLayout)convertView.findViewById(R.id.invisiblablesubitem);

         ViewHolder holder = new ViewHolder();
         holder.imageview=imageView;
         holder.textview=textView;
         holder.linearlayout=linearLayout;
         convertView.setTag(holder);

         convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    lwicon item1 = (lwicon)v.getTag();
                    item1.isLLvisible=true;
                }
            });

        }

        else
        {
            ViewHolder holder2=(ViewHolder)convertView.getTag();
            textView=holder2.textview;
            imageView=holder2.imageview;
            linearLayout=holder2.linearlayout;

        }

        linearLayout.setTag(item);

        imageView.setImageResource(item.icon);
        textView.setText(item.unitname);
        if(item.isLLvisible==true)
            linearLayout.setVisibility(View.VISIBLE);



        return convertView;
    }
}
4

3 回答 3

2

感谢 CaseyB 和 Rishabh.CreatioSoft,我想出了如何解决我遇到的问题,因为问题是在滚动列表之后,列表项目的线性布局搞砸了,这是我为 addapter 实现的代码:

public class myadapter extends ArrayAdapter<lwicon> {

    Context context;
    public lwicon[] iteminarow;
    public myadapter(Context context,lwicon[] iteminarow)
    {
        super(context, R.layout.listitem, iteminarow);
        this.context=context;
        this.iteminarow=iteminarow;
    }
// the ViewHolder class that saves the row's states, acts as Tag
static class ViewHolder
{
    ImageView img;
    TextView txt;
    LinearLayout line;
    int visibility;
    public ViewHolder(ImageView img,TextView txt,LinearLayout line,int visibility)
    {
        this.img=img;
        this.txt=txt;
        this.line=line;
        this.visibility=visibility;
    }


}

    @Override
    public View getView(int position,View convertView,ViewGroup parent)

    {     // get item from ArrayAdapter and set viarables for the row to be inflated
        lwicon item = (lwicon) this.getItem(position);
        ImageView image;
        TextView text;
        LinearLayout linear;
        int visibility=View.GONE;

        if(convertView==null)
        {
        LayoutInflater LI= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         convertView=LI.inflate(R.layout.listitem, parent,false);
         text=(TextView)convertView.findViewById(R.id.unitname);
         image=(ImageView)convertView.findViewById(R.id.image);
         linear=(LinearLayout)convertView.findViewById(R.id.invisiblablesubitem);
             //row is inflated, we just tag it with the ViewHolder, we add the inflated
            //ImageView,TextView,LinearLayout and Visibility stat to the tagged ViewHolder
         convertView.setTag(new ViewHolder(image, text, linear,visibility));

        }


        else
        {        //recover the tagged ViewHolder and it's items
            ViewHolder holder = (ViewHolder) convertView.getTag();
            text = holder.txt;
            image= holder.img;
            linear=holder.line;
            visibility=holder.visibility;
        }


        image.setImageResource(item.icon);
        text.setText(item.unitname);
        linear.setVisibility(item.visibility);


        return convertView;
    }
}

和 lwicon 类是:

public class lwicon {
public int icon;
public String unitname;
public LinearLayout linearLayout;
public int visibility=View.GONE;
public lwicon(){
}
public lwicon(int imageid,String text)
{
    icon = imageid;
    unitname=text;

}
public void toggleVisibility()
{
    if(this.visibility==View.GONE)
        this.visibility=View.VISIBLE;
    else if(this.visibility==View.VISIBLE)
        this.visibility=View.GONE;
    else this.visibility=View.VISIBLE;
}

}

在设置列表的适配器后,我在 MainAvtivity 类中实现了 OnItemClickListener:

lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int position,
                    long id) {
                // TODO Auto-generated method stub
             // getting the item that was clicked in ArrayAdapter<lwicon>
                lwicon item=(lwicon)adapter.getAdapter().getItem(position);
                item.toggleVisibility();
             // recovering the tag (that is the viewholder)from convertView 
             //(view parameter here refers to convertView in getView method)
                ViewHolder holder = (ViewHolder)view.getTag();
                holder.line.setVisibility(item.visibility);
            }
        });

并且我的列表视图具有线性布局子项的项目在滚动后不会忘记它的父视图。

PS:伙计们,关于这个主题的教程很少......我想

于 2012-09-20T10:37:34.263 回答
1

嗨,您可以使用 Custome Adapter 而不是 BaseAdapter 来解决此问题。您应该使用此链接教程

于 2012-09-19T15:37:40.800 回答
0

您需要做的是将隐藏/显示状态存储在lwicon项目中,并在重新填充视图时检查getView().

于 2012-09-19T15:29:07.870 回答