2017-06-28 63 views
0

我有一個MainActivity和一個HomeTabs三個選項卡(A B C),我設置刷新tab C如何完成TabLayout的片段?

我的結構是,當我在標籤c激活onRefresh,我將切換到MainActivity 並再次裝入數據,以顯示HomeTabs

我的問題是當我點擊返回finish();,佈局將顯示tab C

我試圖完成Fragment使用,如: getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();((MainActivity)getActivity()).removeFragment(getActivity());

他們兩人都沒有工作,當我點擊了,我還不能馬上完成的應用程序。

有人可以教我什麼我想念它,這將不勝感激。

HomeTabs擴展Fragment它使用ViewPagerTabLayout添加三個選項卡

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); 
frameLayout = (FrameLayout) findViewById(R.id.frameLayout); 
     // Load the main Fragment 
     if (savedInstanceState == null) { 
      switchFragment(HomeTabs.newInstance()); 
     } 
     //take the onRefresh data,send data to HomeTabs and open tab C 
     if (getIntent().hasExtra("refresh")) { 
      boolean isRefresh = getIntent().getExtras().getBoolean("refresh"); 
      if (isRefresh) { 

       Bundle bundle = new Bundle(); 
       bundle.putBoolean("refresh", isRefresh); 
       HomeTabs homeTabs = new HomeTabs(); 
       homeTabs.setArguments(bundle); 
       switchFragment(homeTabs); 
      } 
     } 
    } 

    public void switchFragment(Fragment fragment) { 
     FragmentManager manager = getSupportFragmentManager(); 
     FragmentTransaction transaction = manager.beginTransaction(); 
     transaction.replace(R.id.frameLayout, fragment); 
     transaction.addToBackStack(null); 
     transaction.commit(); 
    } 
    // I try to finsh my tab C , it's no working 
    public void removeFragment(Activity activity) { 
     activity.onBackPressed(); 
    } 
@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) { 
          // If i onRefrsh three times , i will finsh three times... here is my issue. 
          finish(); 
         } 
        }, new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 

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

這裏是我的標籤C語言程序段刷新回調方法:

public class MyLineChart extends Fragment implements SwipeRefreshLayout.OnRefreshListener{ 

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

     //..................... 

     return view; 
    } 
     @Override 
     public void onRefresh() { 
      refreshLayout.setRefreshing(false); 
      // Both of them are no working. 
      //((MainActivity)getActivity()).removeFragment(getActivity()); 
      //getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit(); 
      Intent intent = new Intent(getActivity(),MainActivity.class); 
      intent.putExtra("refresh", true); 
      startActivity(intent); 
     } 

最後我HomeTabs片段取日期並顯示標籤C:

Bundle bundle = getArguments(); 
    if (bundle != null) { 
     boolean isRefresh = bundle.getBoolean("refresh"); 
     if (isRefresh) { 
      //tab C position is 2 
      tabLayout.getTabAt(2).select(); 
     } 
    } 

回答

1

嘗試這種解決方案添加下面當你在Refresh點擊重新開始活動代碼: -

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); 

OR

更新您的MainActivity.java

1)添加功能MainActivity

public void refreshFragment() { 
    Bundle bundle = new Bundle(); 
    bundle.putBoolean("refresh", isRefresh); 
    HomeTabs homeTabs = new HomeTabs(); 
    homeTabs.setArguments(bundle); 
    switchFragment(homeTabs); 
} 

現在稱之爲從片段標籤Ç剛剛更換startActivity(刷新)代碼:

MainActivity mainActivity = (MainActivity) getActivity(); 
mainActivity.refreshFragment(); 
+0

感謝您的回覆,但是當我在我的'startActivity(意圖)之前嘗試您的代碼時,'沒有關於我的刷新工作。 –

+0

嘗試再次替換TAb C,而不是活動重新啓動並刷新。 –

+0

試試我更新的答案。 –

1

您可以添加在您的片段的聽衆可以在這意味着你需要在你的fragmentC代碼添加接口

public class MyLineChart extends Fragment implements SwipeRefreshLayout.OnRefreshListener { 
    private MyLineChartListener fragmentListener; 

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

     //..................... 

     return view; 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 

     if (context instanceof MyLineChartListener) { 
      fragmentListener = (MyLineChartListener) context; 
     } else { 
      // throw an error 
     } 

    } 

    @Override 
    public void onRefresh() { 
     refreshLayout.setRefreshing(false); 
     // Both of them are no working. 
     //((MainActivity)getActivity()).removeFragment(getActivity()); 
     //getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit(); 
     fragmentListener.onSettingRefresh(); 
    } 

    public interface MyLineChartListener { 
     void onSettingRefresh(); 
    } 
} 

,那麼你需要實現監聽你的父活動 觸發功能在MainActivity代碼如下

public class MainActivity extends AppCompatActivity implements MyLineChart.MyLineChartListener { 

    private FrameLayout frameLayout; 
    private Toolbar toolBar; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     frameLayout = (FrameLayout) findViewById(R.id.frameLayout); 
     // Load the main Fragment 
     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(); 
    } 

    // I try to finsh my tab C , it's no working 
    public void removeFragment(Activity activity) { 
     activity.onBackPressed(); 
    } 

    // this function will be called when the fragment is refreshed 
    @Override 
    public void onSettingRefresh() { 
     Bundle bundle = new Bundle(); 
     bundle.putBoolean("refresh", true); 
     HomeTabs homeTabs = new HomeTabs(); 
     homeTabs.setArguments(bundle); 
     switchFragment(homeTabs); 
    } 

    @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) { 
          // If i onRefrsh three times , i will finsh three times... here is my issue. 
          finish(); 
         } 
        }, new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 

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

感謝您的建議,我一步一步地嘗試您的代碼,當我刷新我的'完成'問題已解決,但我的選項卡C佈局消失了,關於我的刷新,我只是使用相同的'if(savedInstanceState == null){ switchFragment(HomeTabs。的newInstance()); }'爲什麼我的'選項卡C'消失?我可以知道爲什麼嗎?再次感謝。 –