片段刷新?

时间:2018-02-20 15:17:01

标签: java android

我的应用程序包含带有两个片段的主要活动,我使用FragmentStatePagerAdapter和标签,我想要的是每次点击第一个片段的标签或滑动它时刷新第一个片段,我&#39我尝试了很多解决方案,但没有使用它们接缝工作,我使用onStart和setUserVisibleHint,所以如果它们都是真的,那么片段应该刷新,但我得到相同的内容。

我的代码在

之下
public class MainActivity extends AppCompatActivity {

    TabLayout tabLayout;
    ViewPager viewPager;
    CustomAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportActionBar().setElevation(0f);


        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager(), getApplicationContext()));

        tabLayout = (TabLayout) findViewById(R.id.tab);
        tabLayout.setupWithViewPager(viewPager);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());

            }
        });


        }
    }




    class CustomAdapter extends FragmentStatePagerAdapter {

        private String[] fragments  = {"Fragment1","Fragment2"};


        public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
            super(supportFragmentManager);
        }


        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return new Fragment1();
                case 1:
                    return new Fragment2();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return fragments.length;
        }
        @Override
        public CharSequence getPageTitle(int position){

            return fragments[position];
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }
public class Fragment1 extends Fragment {


    private Boolean isVisible = false;
    private Boolean isStarted = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

  // Fragments code 

        return rootView;
    }



    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        if (isVisible && isStarted) {
            refrehList();
            Log.i("Tag", "reloading fragment");
        }

    }

    @Override
    public void onStart() {
        super.onStart();
        isStarted = true;

    }
    @Override
    public void onStop() {
        super.onStop();
        isStarted = false;
    }

    public void refrehList() {
        android.support.v4.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.detach(this).attach(this).commit();

    }
}

2 个答案:

答案 0 :(得分:0)

您的代码是正确的。为了在片段对用户可见时刷新片段,您可以使用:

  @Override
    public void setUserVisibleHint(boolean visible) {
        super.setUserVisibleHint(visible);
        if (visible) {
            Log.i("Tag", "reloading fragment");
        }
    }

在您的代码中,似乎您从未将isStarted标志设置为true,因此您可以自行开枪;)

答案 1 :(得分:0)

如果要更改同一片段的内容,知道其位置添加 监听者到viewpager

Handler handler =new Handler();
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

      }

  @Override public void onPageSelected(int position) {
    if(position==0)
      //handler to ensure that method is called after frament creation (rare bag with state restoration)
      handler.post(new Runnable() {
        @Override public void run() {
          if(yourFragment!=null)
            yourFragment.refreshMethod();

        }
      });
  }

      @Override public void onPageScrollStateChanged(int state) {

      }
    });

同时在tabReselected(Tab tab)方法上调用int 虽然您总是只想更新第一个片段,但明智的做法是保留对它的外部引用。通常我建议使用map来保持对位置为关键字的所有片段的引用。