gpt4 book ai didi

android - 在 ListView 的项目和滚动问题中设置线性布局的可见性

转载 作者:搜寻专家 更新时间:2023-11-01 09:05:09 25 4
gpt4 key购买 nike

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

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

我的适配器代码如下,没有任何 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 中的项目时,应用程序都会崩溃。请帮助...

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

最佳答案

感谢 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 子项目不会在滚动后忘记它的父 View 。

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

关于android - 在 ListView 的项目和滚动问题中设置线性布局的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12497879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com