2014-10-06 64 views
0

我已經遵循了一堆用於在所有活動中實現相同NavigationDrawer的示例,但迄今爲止沒有成功。 我有2個問題:在所有活動中都具有相同的NavigationDrawer

1. 我定義了一個BaseActivity並覆蓋了它的setContentView。 但在setContentView我似乎無法訪問包含我的NavDrawer項目的ListView。 (drawerListView返回null)

  • 我怎麼重用我NavDrawer的代碼?我在這裏找不到一個好例子。 我不喜歡將DrawerLayout,FrameLayout和ListView添加到我的所有片段佈局xmls中。 現在唯一的解決辦法有NavigationDrawer在我所有的網頁是在我腦海中是這樣的:
  • 我改變我的片段佈局,使整個XML嵌入的幀結構是這樣的: 使我所有的碎片佈局都有自己的NavDrawer。這是我不喜歡的,我知道的是 一個不好的解決方案。 (我甚至不知道這是否會工作,並在我的BaseActivity我不會面對具有相同的ID(「drawerList」),並使用相同的ID很多FrameLayouts許多困難列表視圖....

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> 
    <android.support.v4.widget.DrawerLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/drawer_layout" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
    
    <FrameLayout 
        android:id="@+id/mainContent" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" > 
    
    
        all my xml content to form a fragment 
    
    </FrameLayout> 
    
    <!-- Use any Views you like --> 
    <ListView 
        android:id="@+id/drawerList" 
        android:layout_width="240dp" 
        android:layout_height="match_parent" 
        android:divider="@android:color/transparent" 
        android:dividerHeight="0dp" 
        android:background="#ffffff" 
        android:layout_gravity="left" 
        /> 
    </android.support.v4.widget.DrawerLayout> 
    

    那我該怎麼辦?有什麼好處和明顯的例子是高度讚賞。

    的代碼爲我BaseActivity

    public class BaseActivity extends ActionBarActivity implements OnItemClickListener 
    { 
    private DrawerLayout drawerLayout; 
    private String[] properties; 
    private ListView drawerListView; 
    
    
    protected DrawerLayout fullLayout; 
    protected FrameLayout actContent; 
    
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, 
         long id) { 
        Intent intent2=new Intent(BaseActivity.this, ActivitySearchForSale.class); 
        startActivity(intent2); 
    
    } 
    @Override 
    public void setContentView(int layoutResID) { 
        fullLayout= (DrawerLayout) getLayoutInflater().inflate(layoutResID, null); // Your base layout here 
        actContent= (FrameLayout) fullLayout.findViewById(R.id.drawer_layout); 
        drawerListView = (ListView) findViewById(R.id.drawerList); 
        properties = getResources().getStringArray(R.array.propertyTypeArray); 
        drawerListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, properties)); 
        drawerListView.setOnItemClickListener((OnItemClickListener) this); 
    
        getLayoutInflater().inflate(layoutResID, actContent, true); // Setting the content of layout your provided to the act_content frame 
    
        super.setContentView(fullLayout); 
    } 
    

    我所定義的DrawerLayout和我不爲我的activity_main.xml中我的代碼不知道如何將其包含在我的其他碎片佈局中。

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> 
    <android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    
    <FrameLayout 
    android:id="@+id/mainContent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <TextView 
        style="@style/StyleForSimpleTextView" 
        android:text="zzzzz" 
        /> 
    </FrameLayout> 
    
    <!-- Use any Views you like --> 
    <ListView 
        android:id="@+id/drawerList" 
        android:layout_width="240dp" 
        android:layout_height="match_parent" 
        android:divider="@android:color/transparent" 
        android:dividerHeight="0dp" 
        android:background="#ffffff" 
        android:layout_gravity="left" 
        /> 
    </android.support.v4.widget.DrawerLayout> 
    

    我activity_search_for_sale.xml

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    tools:context="com.*.*.ActivitySearchForSale" 
    tools:ignore="MergeRootFrame" /> 
    

    我fragment_search_for_sale

    <?xml version="1.0" encoding="utf-8" standalone="no"?> 
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:tools="http://schemas.android.com/tools" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:paddingBottom="@dimen/activity_vertical_margin" 
        android:paddingLeft="@dimen/activity_horizontal_margin" 
        android:paddingRight="@dimen/activity_horizontal_margin" 
        android:paddingTop="@dimen/activity_vertical_margin" 
        > 
    
    <LinearLayout android:id="@+id/llSearchForSale" 
    style="@style/StyleForParentVerticalLinearLayouts"> 
    . 
    
    . 
    
    
    . 
    
        </LinearLayout> 
        </ScrollView> 
    
    的活動

    Java類:

    public class ActivitySearchForSale extends BaseActivity{ 
    
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState); 
         setContentView(R.layout.activity_search_for_sale); 
         if (savedInstanceState == null) { 
          getSupportFragmentManager().beginTransaction().add(R.id.container, FragmentSearchForSale.newInstance(3)).commit(); 
         } 
        } 
    
        @Override 
        public boolean onCreateOptionsMenu(Menu menu) { 
         return true; 
        } 
    
        @Override 
        public boolean onOptionsItemSelected(MenuItem item) { 
         return super.onOptionsItemSelected(item); 
        } 
    
    
    } 
    

    而且我有一個Java類爲我的片段,該片段onCreateView如同fo llows:

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

    回答

    1

    好像你忘了在剛膨脹的佈局中找到drawerList視圖。

    相反,您嘗試對尚未設置的活動內容嘗試findViewById

    此外,它看起來像你試圖將DrawerLayout投射到一個FrameLayout,這可能不是你想要的。

    創建容納抽屜的佈局(例如,drawer_layout.xml):

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> 
    <android.support.v4.widget.DrawerLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/drawer_layout" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
    
    <ListView 
        android:id="@+id/drawerList" 
        android:layout_width="240dp" 
        android:layout_height="match_parent" 
        android:divider="@android:color/transparent" 
        android:dividerHeight="0dp" 
        android:background="#ffffff" 
        android:layout_gravity="left"/> 
    </android.support.v4.widget.DrawerLayout> 
    

    覆蓋在你的BaseActivitysetContentView這樣的:

    @Override 
    public void setContentView(int layoutResID) { 
        // All activities share the drawer 
        drawer = (DrawerLayout) getLayoutInflater().inflate(R.layout.drawer_layout, null); 
        drawerListView = (ListView) fullLayout.findViewById(R.id.drawerList); 
        properties = getResources().getStringArray(R.array.propertyTypeArray); 
        drawerListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, properties)); 
        drawerListView.setOnItemClickListener((OnItemClickListener) this); 
    
        // After creating the drawer add the activity's layout as the first child 
        ViewGroup layout = getLayoutInflater().inflate(layoutResID, null); 
        drawer.addView(layout, 0); 
    
        super.setContentView(fullLayout); 
    } 
    

    現在你activity_main.xml看起來像:

    setContentView(R.layout.activity_main); 
    

    <FrameLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/mainContent" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" > 
        <TextView 
         style="@style/StyleForSimpleTextView" 
         android:text="zzzzz"/> 
    </FrameLayout> 
    

    你有設置

    只要他們extend BaseActivity這將適用於您的所有活動。

    +0

    感謝您的回答。 actContent =(FrameLayout)fullLayout.findViewById(R.id.mainContent);和drawerListView =(ListView)fullLayout.findViewById(R.id.drawerList); (從你的回答)努力獲得列表視圖。但是佈局文件呢?我應該如何改變它們? – Tina 2014-10-06 13:20:14

    +0

    這隻適用於我的main_activity而不適用於其他 – Tina 2014-10-06 13:25:01

    +0

    @Tina我已更新我的回答以反映您的第一條評論。您的第二條評論尚不清楚,請進一步解釋。 – Simas 2014-10-06 13:28:50

    相關問題