【发布时间】:2017-07-23 20:47:03
【问题描述】:
我正在开发一个具有显示 ListFragment 的 FragmentPagerAdapter 的应用程序。如果用户单击列表中的某个项目,我将尝试执行 FragmentTransaction 并将当前 Fragment 替换为下一个。
但是,当我尝试在 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 的 ListFragment
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):
<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" />
【问题讨论】:
标签: android listview android-studio android-fragments android-listfragment