【问题标题】:re instantiate a PagerAdapter [duplicate]重新实例化一个 PagerAdapter [重复]
【发布时间】:2011-10-24 12:03:45
【问题描述】:

如何重新实例化 ViewPager ? 我的适配器如下所示:

private class MyPagerAdapter extends PagerAdapter{
    @Override
    public int getCount() {
            return NUM_AWESOME_VIEWS;
    }

    @Override
    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater)cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.search_result, null);
        //
        TextView title = (TextView)layout.findViewById(R.id.search_result_title);
        TextView body = (TextView)layout.findViewById(R.id.search_result_body);
        TextView byline = (TextView)layout.findViewById(R.id.search_result_byline);
        TextView day = (TextView)layout.findViewById(R.id.search_result_date_day);
        TextView month = (TextView)layout.findViewById(R.id.search_result_date_month);
        TextView year = (TextView)layout.findViewById(R.id.search_result_date_year);
        TextView page = (TextView)layout.findViewById(R.id.search_result_page);
        TextView page_max = (TextView)layout.findViewById(R.id.search_result_page_max);
        //
        title.setText(list_title.get(position).toString());
        body.setText(list_body.get(position).toString());
        day.setText(list_day.get(position).toString());
        month.setText(list_month.get(position).toString());
        year.setText(list_year.get(position).toString());
        byline.setText(list_byline.get(position).toString());
        page.setText(Integer.toString(position+1));
        page_max.setText(Integer.toString(NUM_AWESOME_VIEWS));
        //
        ((ViewPager) collection).addView(layout,0);
        return layout;
    }

    @Override
    public void destroyItem(View collection, int position, Object view) {
            ((ViewPager) collection).removeView((View) view);
    }



    @Override
    public boolean isViewFromObject(View view, Object object) {
            return view==((View)object);
    }

    @Override
    public void finishUpdate(View arg0) {}


    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {}

    @Override
    public Parcelable saveState() {
            return null;
    }

    @Override
    public void startUpdate(View arg0) {}

}

我找到了一种将一堆数据重新加载到列表中的方法,但是如何从 onPostExecute (Asynctask) 重新加载所有页面。

【问题讨论】:

    标签: java android android-asynctask adapter


    【解决方案1】:

    将以下内容添加到您的 MyPagerAdapter 类中:

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
    

    然后打电话

    myPager.getAdapter().notifyDataSetChanged();
    

    来自你的 AsyncTask 的 onPostExecute

    【讨论】:

      【解决方案2】:

      我认为调用 notifyDataSetChanged();使用适配器参考将解决您的问题。

      【讨论】:

      • 我必须在哪里调用它?从 Asynctask 执行之后?
      • 在 AsyncTask 的 onPostExecute 上。加载您想要更改的所有内容后。
      • 事情正在运行,但很奇怪,因为我需要更改页面并返回查看我的新页面..
      • 使用适配器引用使用 setCurrentItem(0);
      • 我仍然需要更改我的页面才能看到更改。
      猜你喜欢
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 2020-07-04
      • 1970-01-01
      • 2010-11-08
      相关资源
      最近更新 更多