在我的应用程序中,我有一个列表视图,它的项目由图像、文本和线性布局组成。我想在我的自定义适配器类中实现一个 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;
}
}