我正在處理具有顯示ListFragment的FragmentPagerAdapter的應用程序。如果用戶點擊列表中的項目,我試圖執行FragmentTransaction並將當前的Fragment替換爲下一個。當列表項單擊時ListFragment不會替換ListFragment
但是,當我嘗試切換onItemClick內的片段時,沒有任何反應,沒有錯誤,但列表片段沒有更改。我附上了我目前正在嘗試的相關代碼。我認爲這是因爲我使用了錯誤的fragmentManager,但我不確定如何得到正確的。
FragmentPagerAdapter返回與應用程序主要部分之一對應的片段。
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
// The first section of the app is the most interesting -- it offers
// a launchpad into the other demonstrations in this example application.
return new LaunchpadSectionFragment();
case 1:
return new LibrarySectionFragment();
case 2:
return new LipshapeSectionFragment();
default:
// The other sections of the app are dummy placeholders.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position)
{
switch (position)
{
case 0:
return "Practice";
case 1:
return "Library";
case 2:
return "Help";
}
return "Untitled";
}
}
這裏是它具有的onClick
public static class LipshapeSectionFragment extends ListFragment implements AdapterView.OnItemClickListener
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_library, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ContentResolver resolver = this.getContext().getContentResolver();
// if you only want one column do it like this
// String[] projection = new String[]{BaseColumns._ID, VideoFilesContract.videoFiles.FILENAME};
Cursor lipshapeCursor =
resolver.query(LipshapeContract.lipshapes.CONTENT_URI,
LipshapeContract.lipshapes.PROJECTION_ALL,
null,
null,
null);
lipshapeCursor.moveToFirst();
// Setup cursor adapter using cursor from last step
LipshapeCursorAdapter lipshapeAdapter = new LipshapeCursorAdapter(this.getContext(), lipshapeCursor);
// Attach cursor adapter to the ListView
setListAdapter(lipshapeAdapter);
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
Cursor cursor = ((Cursor) getListView().getItemAtPosition(position));
ContentResolver resolver = this.getContext().getContentResolver();
Cursor wordCursor =
resolver.query(WordContract.wordItems.CONTENT_URI,
WordContract.wordItems.PROJECTION_ALL,
"lipshape_id=?",
new String[]{cursor.getString(cursor.getColumnIndex("_id"))},
null);
int count = wordCursor.getCount();
TestSectionFragment fragment = new TestSectionFragment();
FragmentManager fragmentManager = getChildFragmentManager();
fragmentManager.beginTransaction().replace(R.id.lvLibrary, fragment).addToBackStack(null).commit();
//Toast.makeText(getActivity(), "Words within this group: " + count, Toast.LENGTH_SHORT).show();
}
}
的ListFragment(fragment_section_library)佈局文件ListFragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lvLibrary"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
的測試片段,我試圖取代ListFragment附:
*/
public static class TestSectionFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false);
Bundle args = getArguments();
((TextView) rootView.findViewById(android.R.id.text1)).setText(
"Test Worked");
return rootView;
}
}
TestFragment的佈局文件(fragment_section_dummy):
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="24sp"
android:padding="32dp" />