1
我的活動擴展了FragmentActivity並實現了ActionBar.TabListener。在FragmentActivity中實現導航抽屜
所以現在我要實現的抽屜式導航,讓我改變我的佈局在此:
<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" >
<!-- The main content view -->
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<!-- The navigation drawer -->
<ListView
android:id="@+id/drawer"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#F3F3F4"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" ></ListView>
</android.support.v4.widget.DrawerLayout>
和我的代碼Java中的提取物是:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
ab = getActionBar();
ab.setHomeButtonEnabled(false);
ab.setDisplayHomeAsUpEnabled(true);
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
ab.addTab(ab.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
ab.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
Bundle extras = getIntent().getExtras();
rowID = extras.getString(Home.ROW_ID);
Data = extras.getStringArrayList(Home.ROW_ID1);
status = extras.getInt(Home.ROW_ID2);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
_initMenu();
mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
mDrawer.setDrawerListener(mDrawerToggle);
}
但我有錯誤我的logcat:
01-14 15:26:03.671: E/AndroidRuntime(5806): FATAL EXCEPTION: main
01-14 15:26:03.671: E/AndroidRuntime(5806): android.content.res.Resources$NotFoundException: String resource ID #0x0
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.content.res.Resources.getText(Resources.java:242)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.widget.TextView.setText(TextView.java:3783)
01-14 15:26:03.671: E/AndroidRuntime(5806): at felastech.websourcerequest.NsMenuAdapter.getView(NsMenuAdapter.java:94)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.widget.AbsListView.obtainView(AbsListView.java:2461)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.widget.ListView.makeAndAddView(ListView.java:1775)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.widget.ListView.fillDown(ListView.java:678)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.widget.ListView.fillFromTop(ListView.java:739)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.widget.ListView.layoutChildren(ListView.java:1628)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.widget.AbsListView.onLayout(AbsListView.java:2296)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.View.layout(View.java:14055)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.ViewGroup.layout(ViewGroup.java:4604)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:702)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.View.layout(View.java:14055)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.ViewGroup.layout(ViewGroup.java:4604)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.View.layout(View.java:14055)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.ViewGroup.layout(ViewGroup.java:4604)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.View.layout(View.java:14055)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.ViewGroup.layout(ViewGroup.java:4604)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.View.layout(View.java:14055)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.ViewGroup.layout(ViewGroup.java:4604)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1992)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1813)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1112)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4472)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.Choreographer.doFrame(Choreographer.java:525)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.os.Handler.handleCallback(Handler.java:615)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.os.Handler.dispatchMessage(Handler.java:92)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.os.Looper.loop(Looper.java:137)
01-14 15:26:03.671: E/AndroidRuntime(5806): at android.app.ActivityThread.main(ActivityThread.java:4898)
01-14 15:26:03.671: E/AndroidRuntime(5806): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 15:26:03.671: E/AndroidRuntime(5806): at java.lang.reflect.Method.invoke(Method.java:511)
01-14 15:26:03.671: E/AndroidRuntime(5806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-14 15:26:03.671: E/AndroidRuntime(5806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-14 15:26:03.671: E/AndroidRuntime(5806): at dalvik.system.NativeStart.main(Native Method)
,這是錯誤的logcat中的代碼,在NsMenuAdapter類:
public View getView(int position, View convertView, ViewGroup parent) {
NsMenuItemModel item = getItem(position);
ViewHolder holder = null;
View view = convertView;
if (view == null) {
int layout = R.layout.ns_menu_row_counter;
if (item.isHeader)
layout = R.layout.ns_menu_row_header;
view = LayoutInflater.from(getContext()).inflate(layout, null);
TextView text1 = (TextView) view.findViewById(R.id.menurow_title);
ImageView image1 = (ImageView) view.findViewById(R.id.menurow_icon);
TextView textcounter1 = (TextView) view.findViewById(R.id.menurow_counter);
view.setTag(new ViewHolder(text1, image1,textcounter1));
}
if (holder == null && view != null) {
Object tag = view.getTag();
if (tag instanceof ViewHolder) {
holder = (ViewHolder) tag;
}
}
if(item != null && holder != null)
{
if (holder.textHolder != null)
holder.textHolder.setText(item.title);
if (holder.textCounterHolder != null){
if (item.counter > 0){
holder.textCounterHolder.setVisibility(View.VISIBLE);
holder.textCounterHolder.setText(""+item.counter);
}else{
holder.textCounterHolder.setVisibility(View.GONE);
}
}
if (holder.imageHolder != null) {
if (item.iconRes > 0) {
holder.imageHolder.setVisibility(View.VISIBLE);
holder.imageHolder.setImageResource(item.iconRes);
} else {
holder.imageHolder.setVisibility(View.GONE);
}
}
}
return view;
}
http://stackoverflow.com/questions/20088247/navigation-drawer-with-backword-compatibility-android/20088711#20088711。檢查這個 – Raghunandan
什麼是item.title? – Nfear
我不能使用FragmentActivity? – FelasDroid