重新实例化一个PagerAdapter

发布于 2024-11-28 17:31:08 字数 2352 浏览 0 评论 0原文

如何重新实例化 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) 重新加载所有页面。

How can i re-instantiate a ViewPager ?
My adapter looks like this :

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) {}

}

I found a way to reload a bunch of data into the list, but how can i reload all the pages from the onPostExecute (Asynctask).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离旧人 2024-12-05 17:31:08

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

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

myPager.getAdapter().notifyDataSetChanged();

从 AsyncTask 的 onPostExecute调用

Add following to your MyPagerAdapter class:

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

and call

myPager.getAdapter().notifyDataSetChanged();

from onPostExecute of your AsyncTask

夢归不見 2024-12-05 17:31:08

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

I think calling notifyDataSetChanged(); using the adapter reference will solve your problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文