0

我正在處理具有顯示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" /> 

回答

0

我已經設法找到了問題。主要的問題是,你必須以編程方式添加一個片段,然後才能更換,這裏是修復問題所需的變化,它顯示了一個列表視圖,它被替換爲另一個列表視圖上的listItem點擊:

/** 
    * A lipshape list fragment 
    */ 
    public static class LipshapeSectionFragment extends ListFragment implements AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> 
    { 

    public LipshapeCursorAdapter lipshapeAdapter; 
    @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 onCreate(Bundle savedInstanceState) { 
     super.onCreate(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); 


     // Setup cursor adapter using cursor from last step 
     lipshapeAdapter = new LipshapeCursorAdapter(this.getContext(), lipshapeCursor); 
     // Attach cursor adapter to the ListView 

     //setListAdapter(lipshapeAdapter); 
     //getListView().setOnItemClickListener(this); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     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(); 

     WordListSectionFragment fragment = new WordListSectionFragment(); 

     Bundle args = new Bundle(); 

     args.putString("lipshape_selected", cursor.getString(cursor.getColumnIndex("_id"))); 
     fragment.setArguments(args); 

     FragmentManager fragmentManager = getFragmentManager(); 
     fragmentManager.beginTransaction().replace(R.id.svBody, fragment).addToBackStack(null).commit(); 


     //Toast.makeText(getActivity(), "Words within this group: " + count, Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { 
     return null; 
    } 

    @Override 
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { 

    } 

    @Override 
    public void onLoaderReset(Loader<Cursor> loader) { 

    } 
} 

WordListFragment

/** 
    * 
    * WordListFragment 
    */ 
    public static class WordListSectionFragment extends ListFragment implements AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> { 

     public static final String ARG_SECTION_NUMBER = "section_number"; 

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

      return rootView; 
     } 

     @Override 
     public void onActivityCreated(Bundle savedInstanceState) { 
      super.onActivityCreated(savedInstanceState); 

      ContentResolver resolver = getContext().getContentResolver(); 

      Bundle args = getArguments(); 

      Cursor wordCursor = 
        resolver.query(WordContract.wordItems.CONTENT_URI, 
          WordContract.wordItems.PROJECTION_ALL, 
          "lipshape_id=?", 
          new String[]{args.getString("lipshape_selected")}, 
          null); 

      wordCursor.moveToFirst(); 

      // Setup cursor adapter using cursor from last step 
      WordCursorAdapter wordAdapter = new WordCursorAdapter(this.getContext(), wordCursor); 
      // Attach cursor adapter to the ListView 
      setListAdapter(wordAdapter); 
      getListView().setOnItemClickListener(this); 

     } 

      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int position,long id) 
      { 
       Cursor cursor = ((Cursor) getListView().getItemAtPosition(position)); 

       String test = cursor.getString(cursor.getColumnIndex("description")); 
       int word_id = cursor.getInt(cursor.getColumnIndex("_id")); 

       Intent intent = new Intent(getActivity(), CollectionDemoActivity.class); 
       intent.putExtra("word_id", word_id); 
       startActivity(intent); 

     } 

     @Override 
     public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { 
      return null; 
     } 

     @Override 
     public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { 

     } 

     @Override 
     public void onLoaderReset(Loader<Cursor> loader) { 

     } 
    } 

LibrarySectionFragment

/** 
    * 
    * A library fragment 
    */ 
    public static class LibrarySectionFragment extends Fragment { 

     @Override 
     public void onActivityCreated(@Nullable Bundle savedInstanceState) { 

      LipshapeSectionFragment fragment = new LipshapeSectionFragment(); 

      FragmentManager fragmentManager = getFragmentManager(); 
      fragmentManager.beginTransaction().add(R.id.svBody, fragment).commit(); 

      super.onActivityCreated(savedInstanceState); 
     } 

     @Override 
     public void onCreate(@Nullable Bundle savedInstanceState) { 

      super.onCreate(savedInstanceState); 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 

      View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false); 
      Bundle args = getArguments(); 

      return rootView; 
     } 
    } 

主要的改變是與第一個列表的片段:

<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> 

然後接下來我們有個鹼基的片段它位於內:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/svBody" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin"> 

</LinearLayout>