更新视图分页器中更改的选项卡上的内容 [英] Updating the contents on tab changed in view pager

查看:79
本文介绍了更新视图分页器中更改的选项卡上的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去1天,我一直在sc头,但找不到解决方法. 在我的应用程序中,工具栏下有两个选项卡

I am scracthing my head for past 1 day but unable to find the solution. In mine application there are two tabs under the toolbar

  • 第一个标签为 USER-TAB
  • 第二个是 ADMIN-TAB
  • First tab is USER-TAB
  • the second one is ADMIN-TAB

在两个选项卡中都有listView.单击 USER-TAB 上的ListItem时,将出现一个对话框,用户应采取一些措施.

现在,在此之后,选择"ADMIN-TAB"后,管理员应使用新的数据集刷新.但这不是.选择ADMIN-TAB时,会调用onResume()方法并调用allting,但它无法更新列表.

In both the tabs there are the listView. When a ListItem on the USER-TAB is clicked a dialog appears and user take some action.

Now after this when the ADMIN-TAB is Selected the Admin should get refreshed with new sets of data. But It's not. On selecting the ADMIN-TAB the onResume() method and everyting is getting called but it is not able to update the list.

我将无法编写整个代码,我提供了一些代码片段. 基本上我已经从此链接中获取了代码
https://github.com/codepath/android_guides/wiki/使用PagerSlidingTabStrip滑动标签

I wont be able to write the Whole code, I am giving some snippet. Basically I have taken the code from this link
https://github.com/codepath/android_guides/wiki/Sliding-Tabs-with-PagerSlidingTabStrip

在我的主要活动中,我编写了OpPageChangeListener.

In My Main Activity I have written the OpPageChangeListener.

public class MaterialTab extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.material_main_sample);
        // Get the ViewPager and set it's PagerAdapter so that it can display items
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager()));
        // Give the PagerSlidingTabStrip the ViewPager
        PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        // Attach the view pager to the tab strip
        tabsStrip.setViewPager(viewPager);
        tabsStrip.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                if(position == 0){
                    MileUserFragment userFragment = new MileUserFragment();
                    final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    ft.detach(userFragment);
                    ft.attach(userFragment);
                    ft.commit();
                } if(position == 1){
                    MileAdminFragment adminFragment = new MileAdminFragment();
                    final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    ft.detach(adminFragment);
                    ft.attach(adminFragment);
                    ft.commit();
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }
}

OnPageSelected您可以看到我正在分离并重新连接片段.一切工作正常.两个片段的OnResume()都被调用,但List未被更改.我不明白为什么

OnPageSelected You can see I am detaching and reattaching the fragment.Everything is working fine. Both Fragments OnResume() are getting called but the List is not getting changed. I don't undrstand why

为获得更多帮助,我正在添加一个片段.希望这会给我一些想法,我可能会出错

For additional assistance i am adding snippet one Fragment. Hope this will give some Idea where i might be going wrong

public class MileUserFragment extends Fragment {
@Override
    public void onResume() {
        super.onResume();
        new GetAdminDbTask().execute();

        if(!internetUtil.isConnectedToInternet(getActivity())){
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setEnabled(false);
        }
    }

 public class GetAdminDbTask extends AsyncTask<Admin, Void, String> {
        @Override
        protected String doInBackground(Admin... parmas) {
            _adminList = shipmentDbHandler.getAllAdmin();
            return "";
        }
        @Override
        protected void onPostExecute(String str) {
            mAdminAdapter = new AdminAdapter(getActivity(), _adminList);
            adminListView.setAdapter(mAdminAdapter);
            mAdminAdapter.notifyDataSetChanged();
            // Set the refresh Listener to false after the list has been loaded with new set of data
            if (mSwipeRefreshLayout.isRefreshing()) {
                mSwipeRefreshLayout.setRefreshing(false);
            }

            if(_adminList.size() > 0 ){
                mAdminAdapter = new AdminAdapter(getActivity(), _adminList);
                adminListView.setAdapter(mAdminAdapter);
                mAdminAdapter.notifyDataSetChanged();
            }
        }
    }
}

public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 2;
    private String tabTitles[] = new String[] { "Tab1", "Tab2" };
    private FragmentManager fragmentManager;

    public SampleFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fragmentManager = fm;
    }

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

    @Override
    public Fragment getItem(int position) {
        FragmentTransaction ft = null;
        if(position == 0){
            MileUserFragment userFragment = new MileUserFragment();
            return userFragment;

        }
        if(position == 1){
            MileAdminFragment adminFragment = new MileAdminFragment();
            return archiveFragment;
        }

    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }
}

推荐答案

哈.这可能不是一个很好的答案,但至少它是我得到的最接近的答案之一.

Haah.. Finally i got an answer to after an heck of losing almost 1 and half days. It might be not completely good answer but atleast it is one of the closest I got.

首先MainActivity.java如下:

First of all MainActivity.java looks like:

 tabs.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            int scrollPosition = 0;
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(position == 0){
                    scrollPosition = 0;
                }
                if(position == 1){
                    scrollPosition = 1;
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if(state == pager.SCROLL_STATE_IDLE){
                    if(scrollPosition == 0 && application.isActiveAction()){
                        viewPagerAdapter.notifyDataSetChanged();
                        application.setActiveAction(false);
                    }

                    if(scrollPosition == 1 && application.isArchiveAction()){
                        viewPagerAdapter.notifyDataSetChanged();
                        application.setArchiveAction(false);
                    }
                }
            }
        });

现在我在这里所做的是我已经设置OnPageChangeListener并在此我跟踪每当标签正在改变位置.为了我的需要,我创建了两个布尔变量,并在应用程序范围内更改了这些选项卡上的任何内容时进行设置.现在,当一个选项卡上的内容已更改或完成某些操作后,我正在呼叫

Now what I have done here is I have set OnPageChangeListener and in this I am keeping track of the position whenever the tabs are changing. For my needs what i have done is i have created two boolean variables and setting it when any content on those tab are changing in Application scope. Now when the contents on one tab has been changed or some Action are done I am calling

viewPagerAdapter.notifyDataSetChanged()//现在是真正的宝石

viewPagerAdapter.notifyDataSetChanged() // Now this is the real gem

调用此命令后,将调用ViewPagerAdapter函数

after invoking this it will make a call to the ViewPagerAdapter function

@Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;  // This will get invoke as soon as you call notifyDataSetChanged on viewPagerAdapter.
    }

另外一点是,您的ViewPagerAdapter应该扩展FragmentStatePageAdapter.现在,点是

Also the Point is your ViewPagerAdapter should extend FragmentStatePageAdapter. Now the Point is

PagerAdapter.POSITION_NONE

PagerAdapter.POSITION_NONE

将不会缓存该片段,也不会为该选项卡位置重新加载新的片段. 基本思想是,我们不应每次在制表符滑动时都制作或撤消PagerAdapter.POSITION_NONE,因为它会破坏缓存的元素并重新加载影响UI性能的片段.

will not cache the fragment and reload a new fragment for that tab position. Basic idea is we should not make or retutn PagerAdapter.POSITION_NONE everytime on sliding of tab since it destroys the cached element and reload the fragment which affects UI performance.

所以最后,最基本的事情总是检查是否在调用 getItemPosition() viewPagerAdapter.notifyDataSetChanged() /strong>也应被调用.希望它能对别人有所帮助.为了获得更好的性能,您可以根据需要进行更改.

So finally the basic thing is always check that whether on calling viewPagerAdapter.notifyDataSetChanged() the function getItemPosition() should also gets invoked. Hope it will help somebody. For better perfomance you can make changes according to your requirement.

我从这篇文章中获得了必要的突破和理解: @Louth 回答
在Android中从ViewPager删除片段页面

I got the needed breakthrough and understanding from this post : @Louth Answer
Remove Fragment Page from ViewPager in Android

这篇关于更新视图分页器中更改的选项卡上的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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