के साथ मैं एक सारमेनैक्टिविटी चाहता हूं जो नेविगेशन ड्रॉवर बनाता है। वहां मुझे मेनू आइटम पर क्लिक को भी संभालना चाहिए और फिर नई गतिविधियां कॉल करना चाहिए। उन गतिविधियों में, मैं फिर से उसी नेविगेशन ड्रॉवर का उपयोग करना चाहता हूं।एंड्रॉइड नेविगेशन ड्रॉवर (कॉलिंग गतिविधियां) सारमेनमाइटीविटी
मैं सार तत्वों के साथ उप-वर्गों में विस्तारित करता हूं और प्रत्येक उपclass से getLayoutResourceID को अलग-अलग कॉल करता हूं (जैसा कि यहां बताया गया है: android how to create my own Activity and extend it?)।
समस्या यह है कि अब सार तत्वमात्रता में जहां मैं नेविगेशन ड्रॉवर बनाना चाहता हूं, मेरे पास नेविगेशन ड्रॉवर लेआउट (xml) तत्व तक कोई पहुंच नहीं है, क्योंकि मैं निश्चित रूप से एक अलग बेस लेआउट रखना चाहता हूं उपclasses।
क्या मुझे सभी सबक्लास लेआउट फ़ाइलों में "लेआउट शामिल करना" होगा? लेकिन यह काम नहीं करता है, अगर मैं नेविगेशन ड्रॉवर के साथ टुकड़ों की बजाय क्रियाकलापों का उपयोग करना चाहता हूं तो मैं क्या गलत करूं?
public abstract class MainActivity extends Activity {
private String[] menuItems;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
setContentView(getLayoutResourceId());
menuItems = getResources().getStringArray(R.array.menu_items);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuItems));
// Set the list's click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
protected abstract int getLayoutResourceId();
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectItem(position);
}
/** Swaps fragments in the main content view */
private void selectItem(int position) {
//Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
// args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
Intent intent = new Intent(MainActivity.this, ProductListActivity.class);
startActivity(intent);
}
}
public class ProductListActivity extends MainActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
@Override
protected int getLayoutResourceId() {
// TODO Auto-generated method stub
return R.layout.activity_product_list;
}
यह (activity_product_list.xml) उत्पाद सूची उप-वर्ग के लेआउट है:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_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"
tools:context=".ProductList" >
<include layout="@layout/activity_main"/>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>
यह नेविगेशन ड्रॉर (activity_main.xml) के लेआउट है:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="300dp"
android:layout_height="500dp" >
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#c3c3c3"/>
लेकिन यह काम नहीं करता है, लेकिन अगर मेरे पास यह नहीं है, तो मुझे शून्य-सूचक अपवाद मिलते हैं जब मेरा सबक्लास क्लास को अमूर्त कक्षा का निर्माण करता है, जहां मैं नेविगेशन ड्रॉवर बनाना चाहता हूं, इसे लेआउट-तत्व नहीं मिलते हैं सूचियां और लेआउट सेट करें (R.id.left_drawer या R.id.drawer_layout)!
आपने क्या किया? मेरे पास एक ही सवाल है। – theblang
@mattblang फ़ॉलो करें [यह] (http://naddydroid.blogspot.in/) लिंक। – Naddy