android列表片段-更改方向后,在新单击时未删除详细信息片段
我的应用程序中有两个片段(一个包含标题的listFragment和一个包含详细信息的listFragment)的布局 启动后,一切正常。每次单击listfragment中的条目时,细节片段都会更新 然而,在改变方向后,每个新的细节片段只是在旧的细节片段上打开。(由于我的名声不好,我不能发布任何截图,很抱歉。) 我的活动包含两个片段:android列表片段-更改方向后,在新单击时未删除详细信息片段,android,android-fragments,Android,Android Fragments,我的应用程序中有两个片段(一个包含标题的listFragment和一个包含详细信息的listFragment)的布局 启动后,一切正常。每次单击listfragment中的条目时,细节片段都会更新 然而,在改变方向后,每个新的细节片段只是在旧的细节片段上打开。(由于我的名声不好,我不能发布任何截图,很抱歉。) 我的活动包含两个片段: /** * A fragment representing a list of Items. * <p /> * Large screen dev
/**
* 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();
}
}
}
/**
*表示项目列表的片段。
*
*通过替换ListView支持大屏幕设备(如平板电脑)
*有一个网格视图。
*
*/
公共类MedicinationTitlesFragment扩展了ListFragment
实现AdapterView.OnItemClickListener、SearchView.OnQueryTextListener、SearchView.OnCloseListener、LoaderManager.LoaderCallbacks{
/**
*用于填充ListView/GridView的适配器
*观点。
*/
//私有列表适配器mAdapter;
私有静态最终字符串标记=MedicationTitlesFragment.class.getSimpleName();
私有静态最终整数加载器_ID=1;
//私有OnFragmentInteractionListener mListener;
private OnMedicationSelectedListener mMedicationSelectedListener;
/**
*片段的ListView/GridView。
*/
私有列表视图;
私有LoaderManager.LoaderCallbacks MLLoaderCallbacks;
私有搜索视图;
私有字符串mCurFilter;
私人简易程序或适应程序;
静态最终字符串[]RPINFO\u SUMMARY\u PROJECTION=新字符串[]{
RpEntry.Columns.SNAME,RpEntry.Columns.\u ID,RpEntry.Columns.ONAME,RpEntry.Columns.PHZNR,
RpEntry.Columns.ZNR_格式化,RpEntry.Columns.RSIGN_P1,RpEntry.Columns.RSIGN_P5
};
//TODO:重命名和更改参数类型
公共静态MedicalTitlesFragment newInstance(字符串param1,字符串param2){
MedicationTitlesFragment=新的MedicationTitlesFragment();
Bundle args=新Bundle();
返回片段;
}
/**
*片段管理器实例化
*碎片(如屏幕方向改变时)。
*/
公共药物标题片段(){
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
Log.d(标记“entered onCreate”);
super.onCreate(savedInstanceState);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u item\u list,container,false);
//设置适配器
mListView=(ListView)view.findviewbyd(android.R.id.list);
((AdapterView)mListView).setAdapter(mAdapter);
//设置McClickListener,以便在单击项目时收到通知
mListView.setOnItemClickListener(此);
setEmptyText(“Keine Einträge”);
返回视图;
}
//这确保容器活动已实现
//回调接口。如果不是,则抛出异常
@凌驾
公共事务主任(活动){
超级转速计(活动);
试一试{
mMedicationSelectedListener=(OnMedicationSelectedListener)活动;
//this.getListView().setOnItemClickListener(this);
}catch(ClassCastException e){
抛出新的ClassCastException(activity.toString()
+“必须实现OnFragmentInteractionListener”);
}
}
@凌驾
公共无效连接(){
super.onDetach();
mMedicationSelectedListener=null;
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
Log.d(标记“仅输入列表项单击”);
getListView().setItemChecked(位置,true);
Uri rpDetailUri=Uri.parse(RpInfoContentProvider.CONTENT_Uri+“/”+id);
d(标记“rpDetailUri:+rpDetailUri.toString());
mDiicationSelectedListener.onMedicationSelected(rpDetailUri);
}
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
if(null!=mMedicationSelectedListener){
//通知活动回调接口(活动,如果
//片段被附加到一个)上,表示已选择项目。
//onFragmentInteraction(DummyContent.ITEMS.get(position.id));
}
}
/**
*此片段的默认内容有一个文本视图,当
*列表为空。如果要更改文本,请调用此方法
*提供它应该使用的文本。
*/
public void setEmptyText(字符序列emptyText){
View-emptyView=mListView.getEmptyView();
如果(清空文本视图的文本实例){
((TextView)emptyView.setText(emptyText);
}
}
@凌驾
公共加载器onCreateLoader(int-id,Bundle-args){
//当需要创建新加载程序时,将调用此函数
//示例只有一个加载程序,所以我们不关心ID。
//首先,选择要使用的基本URI,这取决于我们是否
//当前正在筛选。
uribaseuri=null;
if(null!=mCurFilter)
baseUri=Uri.withAppendedPath(RpEntry.CONTENT_Uri,Uri.encode(mCurFilter));
其他的
baseUri=RpEntry.CONTENT\u URI;
//现在创建并返回一个游标加载程序,它将处理
////为正在显示的数据创建光标。
返回新游标装入器(
getActivity().getBaseContext(),
巴塞乌里,
RPINFO\u摘要\u预测,
无效的
无效的
RpEntry.Columns.SNAME+“整理本地化ASC”);
}
@凌驾
公共void onload已完成(加载
void onCreate(Bundle savedState) {
...
if(savedState == null) {
// add your details fragment
} else {
// do not add your details fragment it's already there
}
}