FragmentPagerAdapter
의 간단한 활동으로 Fragments
에 몇 가지 차트가 표시됩니다. ActionBar
의 탭을 클릭하거나 한쪽 또는 다른 쪽을 스 와이프하여 해당 항목을 탐색 할 수 있습니다.onOptionsItemSelected 후크가 잘못된 프래그먼트로 호출되었습니다.
public static class ChartFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private int section;
/**
* Returns a new instance of this fragment for the given section number.
*/
public static ChartFragment newInstance(int sectionNumber) {
ChartFragment fragment = new ChartFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public ChartFragment() {
// enable menu for fragment
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int arg = getArguments().getInt(ARG_SECTION_NUMBER);
section = arg;
return generateChart(arg);
}
private View generateChart() {
// generate the chart
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.action_button:
Log.e(TAG, "Section: " + section);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
(귀하의 정보 : 코드 샘플은 ADT 내장이 하나를 기반으로) 클릭 가져
나는 각 조각에 대한 ActionBar
있는 메뉴를 사용하도록 설정하고 Fragment
가 어떻게 보이는지 때문이다 onOptionsItemSelected
후크로 처리했습니다. ActionBar
에서 탭을 수동으로 선택할 때 모든 항목이 올바르게 작동하지만, Fragment
으로 이동 한 직후에 조각을 스 와이프하고 ActionBar
버튼 action_button
을 클릭하면 이전에 표시된 Fragment
의 섹션 번호가 표시됩니다.
스 와이프 할 때도 "정상적으로"작동하도록하려면 어떻게해야합니까?