0

我目前正在開發一個簡單的Android應用程序,它基本上是一個項目列表,當點擊顯示第二個屏幕的細節。正確模式的Android應用程序使用片段

這裏的應用層次:

---> MainActivity.java 
    --> MyListFragment.java 
    --> MyDetailFragment.java 

因此,應用程序打開時,通過MyListFragment.java顯示列表,單擊某個項目時,該項目的位置被傳遞到MyDetailFragment.java和片段被替換以顯示該項目的細節。

當MyDetailFragment.java被加載時,我試圖實現一個向上的脫字符按鈕,但是當用戶按下'後退'或者使用上面的脫字符時MyListFragment.java也被加載回來時,按鈕。

在線研究這個問題後,我看到一些用戶使用與片段沿各自的活動,如下所示:

---> MainListActivity.java 
    --> MyListFragment.java 
---> DetailListActivity.java 
    --> MyDetailFragment.java 

我的問題:哪種模式是更好地使用,第一或第二和爲什麼?

謝謝你的時間。

+0

爲什麼碎片呢?爲什麼不是MainListActivity.java - > DetailListActivity.java?你們在哪裏讀到,每個應用程序必須使用片段? – pskink 2014-08-31 10:59:13

+0

我傾向於在Activity中放置儘可能少的邏輯,並在片段中放置操作欄等所有相關選項。這樣,只有爲MyDetailFragment添加片段時,才顯示上面的插入符號。你能分享你目前如何實施上行導航嗎? – cYrixmorten 2014-08-31 12:58:02

+0

片段還允許靈活地安排平板電腦的佈局。我已經添加了getActivity()。getActionBar()。setDisplayHomeAsUpEnabled(true);和setHasOptionsMenu(true);到片段的onCreateView方法。 – 2014-08-31 14:40:49

回答

0

根據您最後的評論也許你有使用標籤,說「ListFragment」添加的片段,並在活動檢查,如果這個片段可見:

Fragment fragment = getFragmentManager().findFragmentByTag("ListFragment"); 
if (fragment != null && fragment.isAdded() && fragment.isVisible()) { 
    getActionBar().setDisplayHomeAsUpEnabled(true); 
} else { 
    getActionBar().setDisplayHomeAsUpEnabled(false); 
} 

這可能在的onResume或完成活動的onCreateOptionsMenu。

對片段使用setHasOptionsMenu(true)允許片段添加屬於片段的更多選項菜單項。但因爲你在呼喚:

getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); 

我相信setHasOptionsMenu(真)在這種情況下,你直接談話的活動沒有影響。這也是爲什麼刪除片段不會更改/刪除插入的插入符號。

另一種可能的解決方案可能是每當片段被分離時手動刪除插入的光標。

public void onDetach() { 
    getActivity().getActionBar().setDisplayHomeAsUpEnabled(false); 
    getActivity().invalidateOptionsMenu(); // <- not sure if needed 
} 

好的,現在我已經查看了您的代碼。這裏是我的建議:

請注意,我沒有測試過這個代碼,並寫在記事本,所以可能包含一些錯別字。

MainActivity: 移動了插入符號責任活動(setOptionsMenu(真)的片段並不意味着反正來處理這個)。

public class MainActivity extends Activity { 
    private static final String DB_NAME = "hisnul.sqlite3"; 

    //A good practice is to define database field names as constants 
    private static final String TABLE_NAME = "en_dua"; 
    private static final String DUA_ID = "_id"; 
    private static final String DUA_TITLE = "title"; 

    private SQLiteDatabase database; 
    private ArrayList duas; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if (savedInstanceState == null) { 
      getFragmentManager().beginTransaction() 
        .add(R.id.container, new FragmentDuaList()) 
        .commit(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_activity_dualist, menu); 
     restoreActionBar(); 
     return true; 
    } 

    private void restoreActionBar() { 
     Fragment detailFragment = getFragmentManager().findFragmentByTag("DetailListFragment"); 
     if (detailFragment != null) { 
      // means a detail fragment has been added and we need the up caret 
      getActionBar().setDisplayHomeAsUpEnabled(true); 
     } else { 
      getActionBar().setDisplayHomeAsUpEnabled(false); 
     } 
    } 

    @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. 
     int id = item.getItemId(); 
     // why not use switch case here? 
     if(id == android.R.id.home) { 
       Toast.makeText(this, "Test Caret", Toast.LENGTH_SHORT).show(); 
       getFragmentManager().popBackStack(); 
       Log.d("Khalid", "Testing CARET"); 
       return true; 
     } if (id == R.id.action_settings) { 
      Toast.makeText(this,"Settings Clicked", Toast.LENGTH_SHORT).show(); 
      return true; 
     } 
     else if (id == R.id.action_bookmarks){ 
      Toast.makeText(this,"Favorites Clicked", Toast.LENGTH_SHORT).show(); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 

     public PlaceholderFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      return inflater.inflate(R.layout.fragment_dua_list, container, false); 
     } 
    } 
} 

FragmentDuaList: 這裏沒有改變

FragmentDuaDetail: 刪除動作條代碼:

public class FragmentDuaDetail extends Fragment { 

    private static final String DB_NAME = "hisnul.sqlite3"; 

    //A good practice is to define database field names as constants 
    private static final String TABLE_NAME = "en_dua"; 
    private static final String DUA_ID = "_id"; 
    private static final String DUA_TITLE = "title"; 

    private static final String DUA_ARABIC = "arabic"; 
    private static final String DUA_TRANSLATION = "translation"; 
    private static final String DUA_REFERENCE = "reference"; 

    private String title; 
    private String arabic; 
    private String translation; 
    private String reference; 

    private String dua_from_list_fragment; 

    private SQLiteDatabase database; 
    private ArrayList duaDetails; 

    private View rootView; 

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

     // Inflate the layout for this fragment 
     rootView = inflater.inflate(R.layout.fragment_dua_detail, container, false); 

     dua_from_list_fragment = getArguments().getString("dua_id"); 

     //Our key helper 
     ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(getActivity(), DB_NAME); 
     database = dbOpenHelper.openDataBase(); 
     //That’s it, the database is open! 
     fillDua(); 
     setUpDuaList(); 

     getActivity().invalidateOptionsMenu(); 

     return rootView; 
    } 

    public void fillDua(){ 
     //Extracting Elements from Database 
     duaDetails = new ArrayList<DuaDetail>(); 
     Cursor friendCursor = database.query(TABLE_NAME, new String[] {DUA_TITLE, 
       DUA_ARABIC, DUA_TRANSLATION, DUA_REFERENCE}, DUA_ID + "=" + dua_from_list_fragment, null, null, null, null); 
     friendCursor.moveToFirst(); 
     if(!friendCursor.isAfterLast()) { 
      do { 
       title = friendCursor.getString(0); 
       arabic = friendCursor.getString(1); 
       translation = friendCursor.getString(2); 
       reference = friendCursor.getString(3); 
       duaDetails.add(new DuaDetail(title, arabic, translation, reference)); 
      } while (friendCursor.moveToNext()); 
     } 
     friendCursor.close(); 
    } 

    public void setUpDuaList(){ 
     TextView tvDuaTitle = (TextView) rootView.findViewById(R.id.txtDuaTitle); 
     TextView tvDuaArabic = (TextView) rootView.findViewById(R.id.txtDuaArabic); 
     TextView tvDuaTranslation = (TextView) rootView.findViewById(R.id.txtDuaTranslation); 
     TextView tvDuaReference = (TextView) rootView.findViewById(R.id.txtDuaReference); 

     Typeface face; 
     face = Typeface.createFromAsset(getActivity().getAssets(), "DroidNaskh-Regular.ttf"); 
     tvDuaArabic.setTypeface(face); 

     tvDuaTitle.setText(title); 
     tvDuaArabic.setText(arabic); 
     tvDuaTranslation.setText(translation); 
     tvDuaReference.setText(reference); 
    } 


} 
+0

對不起,遲到的回覆,我似乎無法得到這個。你可以在GitHub上看一下我的代碼嗎? :https://github.com/khalid-hussain/HisnulMuslim – 2014-09-14 01:49:59

+0

沒問題 - 更新了答案 – cYrixmorten 2014-09-14 19:47:35

+0

我試了一下代碼,但現在我沒有任何插入符號,徽標和應用程序名稱不可點擊。我在模擬器上測試了代碼。感謝您迄今的努力。 – 2014-09-16 02:42:04

相關問題