我如何在ViewPager中刷新片段

时间:2017-06-28 07:40:17

标签: android android-fragments android-tablayout

我有一个MainActivity和一个HomeTabs,其中有三个标签(A B C),所有标签都是Fragment。我在标签C上设置了onRefresh。当我触发onRefresh时,其视图消失了。这是为什么?我无法解决它。

任何帮助将不胜感激。提前谢谢。

这是我的HomeTabs

public class HomeTabs extends Fragment {
    private Context context;
    private TabLayout tabLayout;

    private int[] imageResId = {R.drawable.input_icon_color,
            R.drawable.note_icon_color,
            R.drawable.analytics_icon_color};

    public HomeTabs() {
        // Required empty public constructor
    }

    public static HomeTabs newInstance() {
        return new HomeTabs();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_tabs, container, false);

        tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
        tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.tab1)));
        tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.tab2)));
        tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.tab3)));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPagerDataReport);

        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(viewPagerAdapter);

        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        tabLayout.getTabAt(0).setIcon(R.drawable.input_icon);
        tabLayout.getTabAt(1).setIcon(imageResId[1]);
        tabLayout.getTabAt(2).setIcon(imageResId[2]);

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

                int id = tab.getPosition();
                if (id == 0) {
                    tabLayout.getTabAt(0).setIcon(R.drawable.input_icon);
                    tabLayout.getTabAt(1).setIcon(imageResId[1]);
                    tabLayout.getTabAt(2).setIcon(imageResId[2]);
                }

                if (id == 1) {
                    tabLayout.getTabAt(0).setIcon(imageResId[0]);
                    tabLayout.getTabAt(1).setIcon(R.drawable.note_icon);
                    tabLayout.getTabAt(2).setIcon(imageResId[2]);
                }

                if (id == 2) {
                    tabLayout.getTabAt(0).setIcon(imageResId[0]);
                    tabLayout.getTabAt(1).setIcon(imageResId[1]);
                    tabLayout.getTabAt(2).setIcon(R.drawable.analytics_icon);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        // My tab C position is 2 , when i refresh that i want to show tab C
        if (getActivity().getIntent().hasExtra("refresh")){
            tabLayout.getTabAt(2).select();
        }

        return view;
    }
}

以下是关于刷新的标签C Fragment

@Override
public void onRefresh() {
    refreshLayout.setRefreshing(false);

    MainActivity mainActivity = (MainActivity) getActivity();
    mainActivity.refreshFragment();
}

MainActivity

public class MainActivity extends AppCompatActivity {

    private FrameLayout frameLayout;
    private Toolbar toolBar;

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

        toolBar = (Toolbar) findViewById(R.id.toolBar);
        setSupportActionBar(toolBar);
        getSupportActionBar().setTitle("");
        toolBar.setTitle(R.string.myAppName);
        toolBar.setNavigationIcon(R.drawable.medical_history32);
        toolBar.setTitleTextColor(Color.WHITE);

        frameLayout = (FrameLayout) findViewById(R.id.frameLayout);

        if (savedInstanceState == null) {
            switchFragment(HomeTabs.newInstance());
        }


    }

    public void switchFragment(Fragment fragment) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.frameLayout, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }



    public void setTitle(String title) {
        getSupportActionBar().setTitle(title);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new DialogHandler(this).quickDialog(
                    getResources().getString(R.string.quick),
                    getResources().getString(R.string.confirm),
                    getResources().getString(R.string.cancel),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            finish();
                        }
                    }, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
        }
        return super.onKeyDown(keyCode, event);
    }


    public void refreshFragment() {
        //switchFragment(HomeTabs.newInstance());
        Intent intent = new Intent(this,MainActivity.class);
        intent.putExtra("refresh",true);
        startActivity(intent);
    }

}

这是我的标签C:

public class MyLineChart extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
    private View view;
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my_line_chart, container, false);

        //................set my data

        return view;
    }

    @Override
    public void onRefresh() {
        refreshLayout.setRefreshing(false);

    }

3 个答案:

答案 0 :(得分:3)

有很多方法可以做到这一点。 如果您使用的是ViewPager,则找到最佳解决方案here

您必须在页面适配器中实现(覆盖)getItemPosition方法以返回(未更改位置)

 @Override
 public int getItemPosition(Object object) {
     return POSITION_NONE;
 }

然后从任何地方进行更新,只需将更改通知给适配器

mPagerAdapter.notifyDataSetChanged();

答案 1 :(得分:2)

以下代码重新加载您的片段:

 FragmentTransaction ft = getFragmentManager().beginTransaction();                                   

 ft.detach(YourFragmentName.this).attach(YourFragmentName.this).commit();

答案 2 :(得分:1)

首先将标记设置为您的片段"片段"

 @Override
public void onRefresh() 
{
    Fragment currentFragment = getFragmentManager().findFragmentByTag("FRAGMENT");
    FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
    fragTransaction.detach(currentFragment);
    fragTransaction.attach(currentFragment);
    fragTransaction.commit();
}