Android 列表适配器-->;如何仅将转换视图更改应用于特定视图项?
我试图通过在列表项中隐藏一个取消隐藏的线性布局,使列表视图的下半部分向下滑动。问题是视图似乎在LayoutAdapter中被重用,因此更改不会影响我打算应用它的视图。相反,它显示在重用视图的任何位置。我如何将下拉列表限制为我请求下拉列表的视图?通过下拉,我的意思是取消隐藏线性布局 列表中的视图数量始终与用户看到的视图数量相同。当用户滚动时,超出视图的视图将被重新使用,以显示用户滚动到的新列表数据。重新绘制列表项时,需要重置列表项的状态 向存储列表数据的对象添加布尔变量“expanded”。(添加到ArrayAdapter的对象)。当用户在listItem中展开LinearLayout时,将expanded设置为trueAndroid 列表适配器-->;如何仅将转换视图更改应用于特定视图项?,android,android-layout,Android,Android Layout,我试图通过在列表项中隐藏一个取消隐藏的线性布局,使列表视图的下半部分向下滑动。问题是视图似乎在LayoutAdapter中被重用,因此更改不会影响我打算应用它的视图。相反,它显示在重用视图的任何位置。我如何将下拉列表限制为我请求下拉列表的视图?通过下拉,我的意思是取消隐藏线性布局 列表中的视图数量始终与用户看到的视图数量相同。当用户滚动时,超出视图的视图将被重新使用,以显示用户滚动到的新列表数据。重新绘制列表项时,需要重置列表项的状态 向存储列表数据的对象添加布尔变量“expanded”。(添加
public class MyListItem
{
public boolean expanded = false;
// data you are trying to display to the user goes here
// ...
}
然后在列表适配器的getView方法中执行此操作
public class MyListAdapter extends ArrayAdapter<MyListItem>
{
public MyListAdapter (Context context, ArrayList<AudioPlaylist> objects)
{
super(context, R.layout.list_item, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout rowLayout;
MyListItem item = this.getItem(position);
if (convertView == null)
{
rowLayout = (LinearLayout) LayoutInflater.from(this.getContext()).inflate(R.layout.list_item, parent, false);
}
else
{
rowLayout = (LinearLayout) convertView;
}
//set the textviews, etc that you need to display the data with
//...
LinearLayout expanded = rowLayout.findViewById(R.id.expanded_area_id);
if (item.expanded)
{
//show the expanded area
expanded.setVisibility(View.VISIBLE);
}
else
{
//hide the area
expanded.setVisibility(View.GONE);
}
return rowLayout;
}
}
公共类MyListAdapter扩展了ArrayAdapter
{
公共MyListAdapter(上下文、ArrayList对象)
{
super(上下文、R.layout.list_项、对象);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
线性布局;
MyListItem=this.getItem(位置);
if(convertView==null)
{
rowLayout=(LinearLayout)LayoutFlater.from(this.getContext()).inflate(R.layout.list_项,父项,false);
}
其他的
{
rowLayout=(LinearLayout)转换视图;
}
//设置显示数据所需的文本视图等
//...
LinearLayout expanded=rowLayout.findViewById(R.id.expanded\u area\u id);
如果(扩展项)
{
//显示扩展区域
expanded.setVisibility(View.VISIBLE);
}
其他的
{
//隐藏该区域
expanded.setVisibility(View.GONE);
}
返回行布局;
}
}
确保list_item.xml有一个线性布局来包装整个内容,否则会出现强制转换异常
希望这对您有所帮助……我认为您应该检查视图持有者模式。听起来您需要设置一个数组来跟踪每一行的状态(扩展或不扩展),并在适配器的getView方法中使用它。