凡在PagerAdatper到异步 [英] Where to Async in PagerAdatper

查看:132
本文介绍了凡在PagerAdatper到异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的片段活动中使用此适配器来显示所有的结果列表视图。
该页面将有一个标题标签,每个标签都会有名单的结果。结果现在,我从内部存储,每次我刷到下一个页面时阅读列表结果就会有轻微的延迟或滞后,所以我想实现这个的AsyncTask里面pageradapter这样的体验会更好,但我不知道在哪里实行。你们能告诉我要出去?

My fragment activity used this adapter to display all the result in a listview. The page will have a title tab and each tab will have the list result.
Now the list result I read from internal storage and each time I swipe to next page it will have slight lag or delay, so I am thinking implementing ASYNCTask inside this pageradapter so the experience will be better but I have no idea where to implement. Could you guys point me out??

public class ViewPagerAdapter extends PagerAdapter 
{        
public ViewPagerAdapter( Context context )
{
    //This is where i get my title
     for (HashMap<String, String> channels : allchannel){       
            String title = channels.get(KEY_TITLE);
            titles[acc] = title;
            acc++;
     }
       scrollPosition = new int[titles.length];

    for ( int i = 0; i < titles.length; i++ )
    {
        scrollPosition[i] = 0;
    }
}

@Override
public String getPageTitle( int position )
{           
    return titles[position];
}

@Override
public int getCount()
{
    return titles.length;
}

@Override
public Object instantiateItem( View pager, final int position )
{
    String filename = null;
    ListView v = new ListView( context );
    final ArrayList<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();

    DatabaseHandler db = new DatabaseHandler(context);

    filename = openFile("PAGE1"); //OpenFile function is read file from internal storage
    switch(position){
    case 0:
        filename = openFile("PAGE1");
        break;
    case 1:
        filename = openFile("PAGE2");
    break;
    case 2:
        filename = openFile("PAGE3");
        break;
    }

       try{
        //Use json to read internal storage file(Page1/Page2) and display all the result on the list

        }
       }catch(Exception e){

       }

    ListAdapter listadapter=new ListAdapter(context, items);
    v.setAdapter( listadapter );
    ((ViewPager)pager ).addView( v, 0 );

    return v;
}

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

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

@Override
public void finishUpdate( View view )
{
}

@Override
public void restoreState( Parcelable p, ClassLoader c )
{
    if ( p instanceof ScrollState )
    {
        scrollPosition = ( (ScrollState) p ).getScrollPos();
    }
}

@Override
public Parcelable saveState()
{
    return new ScrollState( scrollPosition );
}

@Override
public void startUpdate( View view )
{
}

}

推荐答案

我建议做它像这样

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

//Some other work related to instantiation of item    

     AsyncTask<Uri, Void, Bitmap> mLoadTask = new AsyncTask<Uri, Void, Bitmap>() {              
     //async task for loading images in background

        @Override
        protected void onPostExecute(Bitmap result) {
            pageview[position].setImageBitmap(result);                                         
            //post in ui thread
            //this way you have a reference object for each item(using position)
            //and hence you can start several tasks at the same time             
        }

        @Override
        protected Bitmap doInBackground(Uri... params) {        
            Bitmap bitmap=getBitmap(params[0]);    
            //here getBitmap returns the bitmap using uri arguement
            return bitmap;
        }

    };
    mLoadTask.execute(Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + image_id));
}

此方式,每个项目都有自己的线程,你不用担心,不管多少项目将如何加载。

This way each item has its own thread and you need not worry no matter how many items get loaded.

修改还有一件事,你可以从这种方法AsyncTask的code后立即返回浏览量[位置],这样视图将显示为空白(平滑滚动来实现),直到的AsyncTask完成后台工作。但它仍然能够将位图设置为正确的观点,因为它里面的 onPostExecute

EDIT One more thing, you can return pageview[position] from this method right after the AsyncTask code, this way the View will appear blank(smooth scrolling achieved) until AsyncTask completes the background work. But it would still be able to set the Bitmap to the correct View because it has reference inside onPostExecute

这篇关于凡在PagerAdatper到异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆