1

在下载该文件后,我需要在 viewpager 中添加页面我的代码是

arrPdfFilename //declare globle

创建

awesomePager = (ViewPager) findViewById(R.id.awesomepager);
 NUM_AWESOME_VIEWS=10;
        awesomeAdapter = new AwesomePagerAdapter(this,arrPdfFilename);
        awesomePager.setAdapter(awesomeAdapter);

芬兰语下载后

public void FinishedDownloadTask(String Filename)
{
arrPdfFilename[lintpdfcount]=Filename;

awesomePager.getAdapter().notifyDataSetChanged();
}

页面适配器

private class AwesomePagerAdapter extends PagerAdapter{


    @Override
    public int getCount() {
        return NUM_AWESOME_VIEWS;
    }

    /**
     * Create the page for the given position.  The adapter is responsible
     * for adding the view to the container given here, although it only
     * must ensure this is done by the time it returns from
     * {@link #finishUpdate()}.
     *
     * @param container The containing View in which the page will be shown.
     * @param position The page position to be instantiated.
     * @return Returns an Object representing the new page.  This does not
     * need to be a View, but can be some other container of the page.
     */

    private Context context;

    private String[] marrPdfFilename;

    public AwesomePagerAdapter(Context ctx,String[] arrPdfFilename)
    {
        this.context =ctx;
        this.marrPdfFilename=arrPdfFilename;
        Log.d("arrPdfFilename initial length",arrPdfFilename.length+"");
        Log.d("marrPdfFilename initial length",marrPdfFilename.length+"");
    }


    @Override
    public Object instantiateItem(final View collection, int position) {

        LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View view = vi.inflate(R.layout.mainsub, null);
        final RelativeLayout RtLayout=(RelativeLayout)pdfview.findViewById(R.id.pdfviewlayout);

        String Filename=arrPdfFilename[position];


        if(Filename!=null)
        {
        Log.d("Filename",Filename);
        SetPage(context,Filename);
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                //progrsspdf.setVisibility(View.VISIBLE);
                createUI(RtLayout);
                ((ViewPager) collection).addView(view,0);

            }
        });
        }
        return view;

    }

    /**
     * Remove a page for the given position.  The adapter is responsible
     * for removing the view from its container, although it only must ensure
     * this is done by the time it returns from {@link #finishUpdate()}.
     *
     * @param container The containing View from which the page will be removed.
     * @param position The page position to be removed.
     * @param object The same object that was returned by
     * {@link #instantiateItem(View, int)}.
     */

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

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



    /**
     * Called when the a change in the shown pages has been completed.  At this
     * point you must ensure that all of the pages have actually been added or
     * removed from the container as appropriate.
     * @param container The containing View which is displaying this adapter's
     * page views.
     */
    @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) {
        //if(focusedPage>NUM_AWESOME_VIEWS)
            //Log.d("startUpdate",focusedPage+"-");

    }
    @Override
    public void notifyDataSetChanged()
    {
        Log.d("SSSSSSS","notify data set changed");
        super.notifyDataSetChanged();
    }
    @Override
    public int getItemPosition(Object object) {
        Log.d("getItemPosition","getItemPosition");
        return POSITION_NONE;
    }

}

它工作正常,但新添加的页面不会滚动请帮助我....谢谢

4

0 回答 0