在listview的项目和滚动问题中设置linearlayout的可见性

时间:2012-09-19 15:26:19

标签: android listview

在我的应用中,我有一个列表视图,它的项目包括图像,文本和线性布局。 我想在我的自定义适配器类中实现onClickListener,当单击列表中的项目时,linearlayout可见性设置为Visible或Gone。 这就是问题:当我点击列表中的项目并滚动列表时,我看到其他项目的线性布局变得可见。(因为视图回收和所有内容,我知道)。

如何解决此问题并且只有点击项目的linearlayout才能在滚动后获得我想要的可见性?

我的适配器代码如下,没有任何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的回答,我更新了适配器,但没有得到任何答案,每次我点击listview中的项目,app crashesh。请帮忙......

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

3 个答案:

答案 0 :(得分: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);
            }
        });

我的listview项目有一个linearlayout子项目,滚动后不会忘记它的父视图。

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

答案 1 :(得分:1)

您好您可以使用Custome Adapter代替BaseAdapter来解决此问题。您应该暂停此链接Tutorial

答案 2 :(得分:0)

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