android list fragment - 在方向更改后,在新单击时未删除详细信息片段

时间:2014-05-06 18:18:58

标签: android android-fragments

我的应用程序中有两个片段的布局(一个listFragment包含标题,另一个包含详细信息。)

启动后一切正常。每次单击listfragment中的条目时,都会更新Detail-Fragment。

然而,在改变方向之后,每个新的细节片段都只是在旧细节片段上打开。 (由于我的声誉很低,我无法发布任何截图,对不起。)

我的活动包含两个片段:

/**
 * A fragment representing a list of Items.
 * <p />
 * Large screen devices (such as tablets) are supported by replacing the ListView
 * with a GridView.
 * <p />
 */
public class MedicationTitlesFragment extends ListFragment
        implements AdapterView.OnItemClickListener, SearchView.OnQueryTextListener, SearchView.OnCloseListener, LoaderManager.LoaderCallbacks<Cursor> {

    /**
     * The Adapter which will be used to populate the ListView/GridView with
     * Views.
     */
    //private ListAdapter mAdapter;

    private static final String TAG = MedicationTitlesFragment.class.getSimpleName();
    private static final int LOADER_ID = 1;

    //private OnFragmentInteractionListener mListener;

    private OnMedicationSelectedListener mMedicationSelectedListener;

    /**
     * The fragment's ListView/GridView.
     */
    private ListView mListView;
    private LoaderManager.LoaderCallbacks<Cursor> mLoaderCallbacks;

    private SearchView mSearchView;
    private String mCurFilter;

    private SimpleCursorAdapter mAdapter;


    static final String[] RPINFO_SUMMARY_PROJECTION = new String[] {
            RpEntry.Columns.SNAME, RpEntry.Columns._ID, RpEntry.Columns.ONAME, RpEntry.Columns.PHZNR,
            RpEntry.Columns.ZNR_FORMATTED, RpEntry.Columns.RSIGN_P1, RpEntry.Columns.RSIGN_P5
    };

    // TODO: Rename and change types of parameters
    public static MedicationTitlesFragment newInstance(String param1, String param2) {
        MedicationTitlesFragment fragment = new MedicationTitlesFragment();
        Bundle args = new Bundle();
        return fragment;
    }

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public MedicationTitlesFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "entered onCreate");
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_item_list, container, false);
        // Set the adapter
        mListView = (ListView) view.findViewById(android.R.id.list);
        ((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);
        // Set OnItemClickListener so we can be notified on item clicks
        mListView.setOnItemClickListener(this);
        setEmptyText("Keine Einträge");

        return view;
    }

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mMedicationSelectedListener = (OnMedicationSelectedListener) activity;
            //this.getListView().setOnItemClickListener(this);
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mMedicationSelectedListener = null;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.d(TAG, "entered onListItemClick");
        getListView().setItemChecked(position, true);
        Uri rpDetailUri = Uri.parse(RpInfoContentProvider.CONTENT_URI + "/" + id);
        Log.d(TAG, "rpDetailUri: " + rpDetailUri.toString());

        mMedicationSelectedListener.onMedicationSelected(rpDetailUri);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (null != mMedicationSelectedListener) {
            // Notify the active callbacks interface (the activity, if the
            // fragment is attached to one) that an item has been selected.
            //mListener.onFragmentInteraction(DummyContent.ITEMS.get(position).id);
        }
    }

    /**
     * The default content for this Fragment has a TextView that is shown when
     * the list is empty. If you would like to change the text, call this method
     * to supply the text it should use.
     */
    public void setEmptyText(CharSequence emptyText) {
        View emptyView = mListView.getEmptyView();

        if (emptyText instanceof TextView) {
            ((TextView) emptyView).setText(emptyText);
        }
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // This is called when a new Loader needs to be created.  This
        // sample only has one Loader, so we don't care about the ID.
        // First, pick the base URI to use depending on whether we are
        // currently filtering.
        Uri baseUri = null;
        if (null != mCurFilter)
            baseUri = Uri.withAppendedPath(RpEntry.CONTENT_URI, Uri.encode(mCurFilter));
        else
            baseUri = RpEntry.CONTENT_URI;

        // Now create and return a CursorLoader that will take care of
//      // creating a Cursor for the data being displayed.

        return new CursorLoader(
                getActivity().getBaseContext(),
                baseUri,
                RPINFO_SUMMARY_PROJECTION,
                null,
                null,
                RpEntry.Columns.SNAME + " COLLATE LOCALIZED ASC");
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        // Der Loader hat fertig geladen, die Daten können nun im UI angezeigt werden
        // switch to handle multiple loaders
        switch(loader.getId()) {
            case LOADER_ID:
                mAdapter.swapCursor(data);
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);
    }

    @Override
    public boolean onClose() {
        if (!TextUtils.isEmpty(mSearchView.getQuery())) {
            mSearchView.setQuery(null, true);
        }
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        Log.d(TAG, "entered onQueryTextSubmit");
        return true;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        Log.d(TAG, "enteredOnQueryTextChanged");
        // Called when the action bar search text has changed.  Update
        // the search filter, and restart the loader to do a new query
        // with this filter.
        mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
        getLoaderManager().restartLoader(LOADER_ID, null, mLoaderCallbacks);
        return true;

    }

    /**
    * This interface must be implemented by activities that contain this
    * fragment to allow an interaction in this fragment to be communicated
    * to the activity and potentially other fragments contained in that
    * activity.
    * <p>
    * See the Android Training lesson <a href=
    * "http://developer.android.com/training/basics/fragments/communicating.html"
    * >Communicating with Other Fragments</a> for more information.
    */
    public interface OnMedicationSelectedListener {
        // TODO: Update argument type and name
        public void onMedicationSelected(Uri uri);
    }

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

        setEmptyText("Keine Arzneimitteldaten geladen");
        setHasOptionsMenu(true);

        String[] dataColums = { RpEntry.Columns.SNAME };
        int[] viewIDs = { android.R.id.text1};

        mAdapter = new SimpleCursorAdapter(
                getActivity().getBaseContext(),
                android.R.layout.simple_list_item_1,
                null,
                dataColums,
                viewIDs,
                0);
        this.setListAdapter(mAdapter);
//        this.setListShown(false);

        mLoaderCallbacks = this;
        LoaderManager lm = getLoaderManager();
        lm.initLoader(LOADER_ID, null, mLoaderCallbacks);
    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        Log.d(TAG, "entered onCreateOptionsMenu");
        // Place an action bar item for searching.
        MenuItem item = menu.add("Search");
        item.setIcon(android.R.drawable.ic_menu_search);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        SearchView sv = new SearchView(getActivity());
        sv.setOnQueryTextListener(this);
        item.setActionView(sv);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case R.id.action_settings:
                Log.d(TAG, "action_settings clicked");
                Toast.makeText(getActivity().getApplicationContext(), "Einstellungen",
                        Toast.LENGTH_LONG).show();


                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public static class MySearchView extends SearchView {
        public MySearchView(Context context) {
            super(context);
        }

        // The normal SearchView doesn't clear its search text when
        // collapsed, so we will do this for it.
        @Override
        public void onActionViewCollapsed() {
            setQuery("", false);
            super.onActionViewCollapsed();
        }
    }

}

非常感谢!

1 个答案:

答案 0 :(得分:0)

请确保您的活动onCreate中有

void onCreate(Bundle savedState) {
   ...
   if(savedState == null) {
      // add your details fragment
   } else {
     // do not add your details fragment it's already there
   }
}