我有一個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
它使用ViewPager
和TabLayout
添加三個選項卡
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();
}
}
感謝您的回覆,但是當我在我的'startActivity(意圖)之前嘗試您的代碼時,'沒有關於我的刷新工作。 –
嘗試再次替換TAb C,而不是活動重新啓動並刷新。 –
試試我更新的答案。 –