我试图通过在list_item中隐藏一个取消隐藏的线性布局来让列表视图的下半部分向下滑动。问题是视图似乎在LayoutAdapter中被重用,因此更改不仅影响我打算应用它的视图。相反,它会显示在重用视图的任何位置。如何将下拉列表限制为我请求下拉列表的视图?通过下拉我的意思是取消隐藏线性布局。
答案 0 :(得分:0)
用户可以看到列表中的视图总是一样多。当用户滚动时,视图之外的视图将被重用以显示用户滚动到的新列表数据。重绘时需要重置列表项的状态。
将一个布尔变量'expanded'添加到存储列表数据的对象中。 (添加到ArrayAdapter的对象)。当用户在listItem中展开LinearLayout时,设置expanded = true。
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;
}
}
确保你的list_item.xml有一个LinearLayout包装整个东西,否则你会得到一个强制转换异常。
希望有帮助...