Android notifydatasetchanged()在视图中需要更新时创建新视图,并保持布局位置

时间:2013-06-12 06:37:51

标签: android android-listview horizontalscrollview notifydatasetchanged

我用horizo​​ntalView创建了listview。在这里,ListView的每个位置都包含Horizo​​ntalView,其中有一组可以水平滚动的图像。

每当List在垂直滚动时到达终点时,我调用listview的notifydatasetchange()方法,实习生创建列表的新视图。

我的列表视图就像这样

enter image description here

enter image description here

我的问题就是:

假设我位于ListView位置0的水平视图的图像12处。当notifydatasetchange()调用它时,它会自动刷新所有视图,并且ImageView的位置0处的Image位置变为0。

用户再次滚动可能会令人恼火。再次为特定的形象。

我需要在视图上保持图像的位置,这样当它重新加载它时会自动显示为调用notifydatasetchange()之前的状态。

注意:我正在使用两个适配器在ListView中填充Horizo​​ntal scrollView。

适配器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;
    }

}

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

}

1 个答案:

答案 0 :(得分:0)

在两个适配器上调用notifydatasetchange()时,

首先查看 scrollView Android setting position of hortizontal scrollview

对于 listview ,您可以在这里查看 Maintain/Save/Restore scroll position when returning to a ListView

只需在调用notifydatasetchange()

之前保存您的位置 祝你好运