21

Ich versuche, beide Navigationsschublade und Ansicht Pager in derselben Aktivität zu implementieren. Navigation Schublade funktioniert gut, aber die Ansicht Pager funktioniert nicht, auch ich bekomme Null Zeiger auf rechts wischen, wenn die Navigationsleiste geöffnet ist (Null Zeiger auf Android. Unterstützung. V4. Widget. DrawerLayout. IsContentView (DrawerLayout.java: 840). I unten ist das Haupt-xML-Layout und Code anbringen. Navigationsschublade und Pager in derselben Aktivität anzeigen

<android.support.v4.view.ViewPager 
    android:id="@+id/viewpager" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
</android.support.v4.view.ViewPager> 

<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/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#111" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" /> 
</android.support.v4.widget.DrawerLayout> 

Aktivitätsklasse wird unter

public class MainActivity extends Activity { 
private DrawerLayout mDrawerLayout; 
private ListView mDrawerList; 
private ActionBarDrawerToggle mDrawerToggle; 

private CharSequence mDrawerTitle; 
private CharSequence mTitle; 
private String[] mPlanetTitles; 
private MainActivity mContext; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mContext = this; 
    ViewPager vp = (ViewPager) findViewById(R.id.viewpager); 
    CustomPagerAdapter adapter = new CustomPagerAdapter(mContext); 
    vp.setAdapter(adapter); 
    vp.setPageTransformer(false, new ViewPager.PageTransformer() { 

     @Override 
     public void transformPage(View page, float position) { 
      final float normalizedposition = Math.abs(Math.abs(position) - 1); 
      page.setScaleX(normalizedposition/2 + 0.5f); 
      page.setScaleY(normalizedposition/2 + 0.5f); 
     } 
    }); 

    mTitle = mDrawerTitle = getTitle(); 
    mPlanetTitles = getResources().getStringArray(R.array.planets_array); 
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerList = (ListView) findViewById(R.id.left_drawer); 

    // set a custom shadow that overlays the main content when the drawer 
    // opens 
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 
    // set up the drawer's list view with items and click listener 
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
      R.layout.drawer_list_item, mPlanetTitles)); 
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

    // enable ActionBar app icon to behave as action to toggle nav drawer 
    getActionBar().setDisplayHomeAsUpEnabled(true); 
    getActionBar().setHomeButtonEnabled(true); 

    // ActionBarDrawerToggle ties together the the proper interactions 
    // between the sliding drawer and the action bar app icon 
    mDrawerToggle = new ActionBarDrawerToggle(
      this, /* host Activity */ 
      mDrawerLayout, /* DrawerLayout object */ 
      R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */ 
      R.string.drawer_open, /* 
            * "open drawer" description for 
            * accessibility 
            */ 
      R.string.drawer_close /* 
            * "close drawer" description for 
            * accessibility 
            */ 
      ) { 
       @Override 
       public void onDrawerClosed(View view) { 
        getActionBar().setTitle(mTitle); 
        invalidateOptionsMenu(); // creates call to 
              // onPrepareOptionsMenu() 
       } 

       @Override 
       public void onDrawerOpened(View drawerView) { 
        getActionBar().setTitle(mDrawerTitle); 
        invalidateOptionsMenu(); // creates call to 
              // onPrepareOptionsMenu() 
       } 

      }; 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 

    if (savedInstanceState == null) { 
     selectItem(0); 
    } 
} 

@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
    // If the nav drawer is open, hide action items related to the content 
    // view 
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); 
    return super.onPrepareOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // The action bar home/up action should open or close the drawer. 
    // ActionBarDrawerToggle will take care of this. 
    if (mDrawerToggle.onOptionsItemSelected(item)) { 
     return true; 
    } 
    // Handle action buttons 
    return true; 
} 

/* The click listner for ListView in the navigation drawer */ 
private class DrawerItemClickListener implements ListView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     selectItem(position); 
    } 
} 

private void selectItem(int position) { 

    // update selected item and title, then close the drawer 
    mDrawerList.setItemChecked(position, true); 
    setTitle(mPlanetTitles[position]); 
    mDrawerLayout.closeDrawer(mDrawerList); 
} 

@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; 
} 

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    // Sync the toggle state after onRestoreInstanceState has occurred. 
    mDrawerToggle.syncState(); 
} 

public class CustomPagerAdapter extends PagerAdapter { 

    private Context context; 

    int index = 2; 

    // public CustomPagerAdapter(Context context, Vector<View> pages) { 
    // this.context = context; 
    // this.pages = pages; 
    // } 

    public CustomPagerAdapter(Context context) { 
     this.context = context; 
     this.index = 2; 
    } 

    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     LayoutInflater inflater = (LayoutInflater.from(container.getContext())); 
     // View page = pages.get(position); 
     View view = null; 
     if (position == 0) { 
      view = inflater.inflate(R.layout.page_one_views, null); 
      ((ViewPager) container).addView(view); 

     } 
     else { 
      // page.setBackgroundColor(colors.get(position)); 
      // container.addView(page); 
      view = inflater.inflate(R.layout.page_two, null); 
      ((ViewPager) container).addView(view); 
     } 
     return view; 
    } 

    @Override 
    public int getCount() { 
     return index; 
    } 

    @Override 
    public boolean isViewFromObject(View view, Object object) { 
     return view.equals(object); 
    } 

    @Override 
    public void destroyItem(ViewGroup container, int position, Object object) { 
     container.removeView((View) object); 
    } 

} 

}

gegeben

Bitte helfen Sie mir. Vielen Dank im Voraus

+1

Ich glaube, der drawerlayout das Wurzelelement sein muss. Versuchen Sie es mit dem ViewPager insdie –

Antwort

29

Die DrawerLayout sollte das Stammelement sein. Setzen Sie die ViewPager hinein.

<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"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

    <ListView 
     android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#111" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" /> 
</android.support.v4.widget.DrawerLayout> 
7
<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" > 

<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> 

<ListView 
    android:id="@+id/left_drawer" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:layout_below="@+id/top_bar" 
    android:background="@android:color/darker_gray" 
    android:choiceMode="singleChoice" 
    android:divider="@android:color/background_dark" 
    android:dividerHeight="1dp" /> 

</android.support.v4.widget.DrawerLayout> 
+2

Schublade ist hier nicht sichtbar – Sujay

0

Um so etwas wie dies zu erreichen:

enter image description here

Verwenden einfach:

<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" >  

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay"/> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewpager" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" > 
     </android.support.v4.view.ViewPager> 

     <ListView 
      android:id="@+id/left_drawer" 
      android:layout_width="240dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:background="#111" 
      android:choiceMode="singleChoice" 
      android:divider="@android:color/transparent" 
      android:dividerHeight="0dp" /> 
    </android.support.v4.widget.DrawerLayout>