我用horizontalView 制作了listview。在此 ListView 的每个位置都包含 HorizontalView,其中有一组可以水平滚动的图像。
每当 List 在垂直滚动时到达其末尾时,我都会调用 listview 的 notifydatasetchange() 方法,实习生会创建新的列表视图。
我的列表视图是这样的
我的问题是:
假设我在 ListView 位置 0 的水平视图的图像 12。当 notifydatasetchange() 调用时,它会自动刷新所有视图,并且 Image 位置在 ListView 的位置 0 处变为 0。
用户为特定图像再次滚动可能会很烦人。
我需要保持图像在视图上的位置,以便在重新加载时自动显示为调用 notifydatasetchange() 之前的状态。
注意:我使用两个适配器来填充 ListView 中的水平滚动视图。
适配器 1
private class MyAdapter extends BaseAdapter {
private Context context;
private Activity activity;
private int count_processed_rows = 0;
MyAdapter(Activity activity, Context context) {
this.context = context;
this.activity = activity;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
int size = 0;
if((al_thumb_images != null) && (al_thumb_images.size() > 0)){
size = (al_thumb_images.size() / 20) ;
}
return size;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//System.out.println("Parent position : "+position);
++count_processed_rows;
int processed_row_limit = getCount() * 2;
View row = convertView;
ViewHolder holder = null;
TextView textView_list_item = null;
if (row == null) {
holder = new ViewHolder();
LayoutInflater inflater_parent = this.activity.getLayoutInflater();
row = (View) inflater_parent.inflate(R.layout.listrow, null);
holder.linear = (HorizontalView) row.findViewById(R.id.gallery);
holder.linear.setId(position);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
try{
HorizontalImageAdapter imageAdapter = new HorizontalImageAdapter(activity, context, position);
holder.linear.setAdapter(imageAdapter);
} catch(Exception e){
System.out.println("catch :: "+e);
}
return row;
}
private class ViewHolder {
private HorizontalView linear;
}
}
适配器 2
private class HorizontalImageAdapter extends BaseAdapter {
private Activity activity;
private Context context;
private int list_row;
public HorizontalImageAdapter(Activity activity, Context context, int list_row) {
System.out.println(111);
this.activity = activity;
this.context = context;
this.list_row = list_row;
}
@Override
public int getCount() {
return 20;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
System.out.println(112);
View row = convertView;
ViewHolder holder = null;
if (row == null) {
holder = new ViewHolder();
LayoutInflater inflater_child = this.activity.getLayoutInflater();
row = (View) inflater_child.inflate(R.layout.listitem, null, false);
holder.image = (ImageView) row.findViewById(R.id.image);
holder.progress = (ProgressBar) row.findViewById(R.id.progress);
holder.checkBox_image = (CheckBox) row.findViewById(R.id.checkBox_image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
try{
{
int limit = ((this.list_row) * 20) + position;
System.out.println(TAG+" --> "+"position : "+position);
System.out.println(TAG+" --> "+"list_row : "+this.list_row);
System.out.println(TAG+" --> "+"limit : "+limit);
System.out.println(TAG+" --> "+" i : "+((this.list_row) * 20));
System.out.println(TAG+" --> "+"al.size() : "+al_thumb_images.size());
int counter = 0;
final String s_THUMB_IMAGE_URL = al_thumb_images.get(limit);
counter++;
aq.id(holder.image).progress(holder.progress).image(s_THUMB_IMAGE_URL, true, true, 500, R.drawable.ic_launcher);
}
} catch(Exception e){
System.out.println("catch :: "+e);
}
return row;
}
private class ViewHolder {
ImageView image;
ProgressBar progress;
TextView icon_text;
CheckBox checkBox_image;
}
}