如何使用Fragements中的视图寻呼机刷新标签页

时间:2018-02-16 09:30:11

标签: android android-viewpager

我使用viewpager在我的片段中有两个标签。当我转到选项卡2(时钟)并单击选项卡1(仪表板)时,我在两个选项卡中都有API休息调用我想刷新选项卡1我想再次调用API休息调用,但它不刷新。请帮助我。 这是我的查看寻呼机代码:

public class TabSampleFragment extends Fragment {
    TabLayout gs_tabLayout;
    ViewPager gs_viewPager;
    ViewPagerAdapter mViewPagerAdapter;

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

        final View view = inflater.inflate(R.layout.activity_frag_tabs, container, false);
        gs_tabLayout = (TabLayout)view.findViewById(R.id.gs_tabLayout);
        gs_viewPager = (ViewPager)view.findViewById(R.id.gs_viewPager);
        mViewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
        mViewPagerAdapter.addFragments(new Progress(), "Dashboard");
        mViewPagerAdapter.addFragments(new ClockInDetails(), "Clocking");
        gs_viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }  @Override
              public void onPageSelected(int position) {
                     mViewPagerAdapter.notifyDataSetChanged(); //this line will force all pages to be loaded fresh when changing between fragments
                                          }   
            @Override
            public void onPageScrollStateChanged(int state) { 
            }
        });
        gs_viewPager.setAdapter(mViewPagerAdapter);
        gs_tabLayout.setupWithViewPager(gs_viewPager);
        gs_tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        mViewPagerAdapter.notifyDataSetChanged();
        return view;
    }    
}

这是我的适配器代码:当我点击时如何刷新仪表板选项卡,片段生命周期也没有调用(onstart,onpause,onresume,onstop单击标签时)

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
  ArrayList<Fragment> fragments = new ArrayList<>();
    ArrayList<String> tabTitles = new ArrayList<>();

    public void addFragments(Fragment fragments, String titles){
        Log.i("RR","on add-gragment");
        this.fragments.add(fragments);
        this.tabTitles.add(titles);
    }

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }    
    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);

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

}

这是我的仪表板代码。我在oncreate和uservisiblehint方法中使用字符串请求调用REST API。如果我没有在uservisiblehint方法中调用rest调用它的工作,但如果我调用它意味着它显示问题

enter code here
public class Progress extends Fragment {
    ProgressBar gs_circularProgressBar;
    public ProgressDialog mProgDialog;
    ImageView gs_image_userProfileimage;
    private TextView gs_text_username, gs_text_companyname, gs_text_completedTaskasValue,  gs_text_assignedTaskasValue, gs_assignedTaskTextView, gs_text_totalDistanceValue, gs_totalDistanceTextView, gs_kmsLabel, gs_totalHoursTextView, gs_text_totalHoursasValue;
    Typeface tf1, tf2, tf3, tf4;
    Float gs_var_dist_travel;
    String gs_var_userimage,gs_var_username,gs_var_companyname,gs_var_total_task,gs_var_complete_task,gs_var_timetaken,gs_var_roleid,gs_var_userid;
    int gs_var_networkx=2;
    public Progress() {
        // Required empty public constructor
    }

ProgressBar progressBar1;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.con_fragment_progress, container, false);
        gs_circularProgressBar = (ProgressBar)view.findViewById (R.id.gs_circularProgressBar);
        GS_User_Dashboard(gs_var_roleid,gs_var_userid);

        return view;
    }



    private void GS_User_Dashboard( String gs_var_roleid, String gs_var_userid) {
       StringRequest stringRequest = new StringRequest(Request.Method.GET, ServerUtils.Gs_User_Dashboard+"gs_userId="+gs_var_userid+"&gs_role="+gs_var_roleid,
                new com.android.volley.Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                    }
                },
                new com.android.volley.Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        //Dismissing the progress dialog
                        clearProgressDialog();
                        progressBar1.setVisibility(View.GONE);
                        //Showing toast
                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new Hashtable<String, String>();
                return params;
            }
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                return params;
            }

            @Override protected void deliverResponse(String response) {
                super.deliverResponse(response);
            }
            @Override public void deliverError(VolleyError error)
            {
                super.deliverError(error);
              //  progressBar1.setVisibility(View.GONE);
                Log.e("deliverResponse", "getNetworkTimeMs : " + error);
            }
        };

        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        //Adding request to the queue
        requestQueue.add(stringRequest);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            // load data here and api call
        GS_User_Dashboard(gs_var_roleid,gs_var_userid);
         Log.i("EE","visible--progress");
        }else{

        }
    }
}

1 个答案:

答案 0 :(得分:1)

  

默认情况下,查看寻呼机将在当前选项卡的右侧和左侧选项卡上加载至少1个。它是这样做的,主要是因为当两个标签的某些区域可见时,你可以滑动viewpager。对于那些平滑过渡,需要预加载。

但由于您只有两个标签,这是一个问题,当您转到第二个标签时,两个标签都已预先加载而不会刷新。

我建议你覆盖setUserVisibleHint()方法并在那里进行api调用。

尝试以下代码:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
 super.setUserVisibleHint(isVisibleToUser);
 if (isVisibleToUser) {
   // load data here and api call
 }else{

 }
}