更新特定页面中的Viewpager项目

时间:2017-01-18 05:39:23

标签: android performance android-viewpager

我想每次都获取新列表并在viewpager特定位置时更新视图。我尝试了以下方式,但它更滞后。我在第一页更新我的列表,我在3页中得到了更新的结果。所以我需要每次在这3页上获得新结果并更新viewpager.Any sugg pls

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            check=position;
            if(position==3) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        recentList=new ArrayList<PlayerModel>();
                        mySQLiteAdapter = new SQLiteAdapter(MultHomePageActivity.this);
                        mySQLiteAdapter.openToWrite();
                        Cursor mFavoritesCursor = mySQLiteAdapter.fetchRecentData(AppConstants.AUDIO_TABLE);
                        LogUtil.error("Count",mFavoritesCursor.getCount()+"-->");
                        if(mFavoritesCursor!=null)
                            recentList= getAudioFavRecentsList(mFavoritesCursor);
                        mySQLiteAdapter.close();
                        adapter.notifyDataSetChanged();                        }
                });



            }

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

我试图把我的代码放在像这样的单独的线程中。它是滞后的

    private class AsyncTaskRunner extends AsyncTask<String, String, String> {

    private String resp;

    @Override
    protected String doInBackground(String... params) {
        recentList=new ArrayList<PlayerModel>();
        mySQLiteAdapter = new SQLiteAdapter(MultHomePageActivity.this);
        mySQLiteAdapter.openToWrite();
        Cursor mFavoritesCursor = mySQLiteAdapter.fetchRecentData(AppConstants.AUDIO_TABLE);
        LogUtil.error("Recent Count",mFavoritesCursor.getCount()+"-->");
        if(mFavoritesCursor!=null)
            recentList= getAudioFavRecentsList(mFavoritesCursor);
        mySQLiteAdapter.close();
        adapter.setFragment(fragmentMultCards.newInstance(recentList,AppConstants.
                AUDIO_PLAYER,AppConstants.RECENT_PLAYER),3,getString(R.string.msg_recents));

        return resp;
    }


    @Override
    protected void onPostExecute(String result) {
        adapter.notifyDataSetChanged();
    }


    @Override
    protected void onPreExecute() {
    }


    @Override
    protected void onProgressUpdate(String... text) {
    }
}

    private List<PlayerModel> getAudioFavRecentsList(Cursor mAudioCursor) {
    List<PlayerModel> cursorList=new ArrayList<>();
    if (mAudioCursor != null) {
        if(mAudioCursor.getCount()>0)
            do{

                cursorList.add(new PlayerModel(
                        mAudioCursor.getInt(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_ID)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_DISPLAY_NAME)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_SIZE)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_DATE_TAKEN)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_DURATION)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_ALBUM)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_ALBUM_ID)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_ARTIST)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_COMPOSER)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_DATA)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_TRACK)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_MIME)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_IS_ALARAM)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_BOOKMARK)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_TITLE)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_IS_NOTIFICATION)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_IS_MUSIC)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_RECENT_DATE)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_FAVORITE)),
                        mAudioCursor.getString(mAudioCursor.getColumnIndexOrThrow(AppConstants.KEY_AUDIO))

                ));

            }while (mAudioCursor.moveToNext());
    }
    return cursorList;
}

1 个答案:

答案 0 :(得分:0)

我认为您需要在单独的线程中获取数据,一旦获得数据,您就会更新它。现在,您正在主线程中获取数据,这是其滞后的原因。 我希望它适合你:)