如何使用ViewPager和导航抽屉创建自定义操作栏(左侧)

时间:2015-11-27 11:01:39

标签: android android-actionbar android-viewpager navigation-drawer

如何使用视图寻呼机和导航创建自定义操作栏  抽屉(左侧)。请帮助我,我已经为所有事情做了逻辑。但是我  我没有得到如何包含自定义操作栏布局的解决方案  查看寻呼机和导航抽屉(左侧)布局

Drawer_layout.xml

 <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/pager"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         > 
     </android.support.v4.view.ViewPager>

 <ListView
     android:id="@+id/list_slidermenu"
     android:layout_width="240dp"
     android:layout_height="match_parent"
     android:layout_gravity="end"
     android:choiceMode="singleChoice"

     android:dividerHeight="1dp"       
    /> </android.support.v4.widget.DrawerLayout>

profile_header.xml

 <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"   
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_gravity="center"
  android:weightSum="3"
  android:background="@color/blue"
 android:orientation="horizontal">     

 <ImageView
     android:id="@+id/left"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
    android:layout_weight="0.30"
     android:paddingLeft="15dp"
    android:paddingBottom="5dp"
     android:paddingTop="5dp"
     android:scaleType="fitStart"
      />

 <TextView
     android:id="@+id/heading"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1.60"
     android:paddingBottom="8dp"
     android:paddingRight="8dp"
     android:paddingTop="8dp"
     android:textSize="16sp"
     android:textColor="#ffffff"
     android:text="@string/profile" />

<ImageView
     android:id="@+id/drawer"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1.13"
     android:paddingBottom="5dp"

     android:scaleType="fitEnd"
     android:paddingRight="15dp"
     android:paddingTop="7dp"
     android:src="@drawable/ic_drawer" />

  </LinearLayout>

ProfileActivity.java

 public class ProfileActivity extends ActionBarActivity implements     
 ActionBar.TabListener {



private ViewPager viewPager;
private ActionBar.Tab mTab;
private TabsPagerAdapter mAdapter;
ActionBar mActionBar;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;


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

    mActionBar = getSupportActionBar();
    mActionBar.setDisplayHomeAsUpEnabled(true);
    mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM );
    mActionBar.setDisplayHomeAsUpEnabled(true);

    getActionBar().setCustomView(R.layout.profile_header);
    ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    p.gravity = Gravity.CENTER;

    View cView = getLayoutInflater().inflate(R.layout.profile_header, null);
    ImageView drawer = (ImageView) cView.findViewById(R.id.drawer);

    drawer.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    //fare_ll = (TextView) actionBar.getCustomView().findViewById(R.id.fare_ll); Get id of your custom view like this ...

    mActionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#002C86")));

    mActionBar.setDisplayShowCustomEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.pager);
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(mAdapter);
    viewPager.setCurrentItem(0);

    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mTab = mActionBar.newTab().setTabListener(this);
    mTab.setText("Profile");

    mActionBar.addTab(mTab);
    //mActionBar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#f7f7f7")));

    mTab = mActionBar.newTab().setTabListener(this);
    mTab.setText("Cars");
    mActionBar.addTab(mTab);

    viewPager.setOnPageChangeListener(
            new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    mActionBar.setSelectedNavigationItem(position);
                }
            });
    navMenuTitles= ProfileActivity.this.getResources().getStringArray(R.array.option_menu);

    mTitle = mDrawerTitle = getTitle();

    navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView)mDrawerLayout.findViewById(R.id.list_slidermenu);

    navDrawerItems = new ArrayList<NavDrawerItem>();

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, -1)));

    Log.d("TestTag","navDrawerItems:"+navDrawerItems);

    navMenuIcons.recycle();

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    adapter = new NavDrawerListAdapter(ProfileActivity.this,
            navDrawerItems);
    mDrawerList.setAdapter(adapter);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.app_name, R.string.app_name
            ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);

            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);

            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {

        displayView(0);
    }
}


private class SlideMenuClickListener implements
ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        displayView(position);
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    switch (item.getItemId()) {
    case R.id.action_settings:
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}


private void displayView(int position) {

    /*switch (position) {
    case 0:
        Intent intent = new Intent(this, ProfileActivity.class);
        startActivity(intent);
        finish();
        break;
    case 1:
        Intent intent1 = new Intent(this, RegisterActivity.class);
        startActivity(intent1);
        finish();
        break;
    case 2:
        Intent intent2 = new Intent(this, RegisterActivity.class);
        startActivity(intent2);
        finish();
        break;
    case 3:
        Intent intent3 = new Intent(this, RegisterActivity.class);
        startActivity(intent3);
        finish();
        break;
    case 4:
        Intent intent4 = new Intent(this, RegisterActivity.class);
        startActivity(intent4);
        finish();
        break;
    case 5:
        Intent intent5 = new Intent(this, RegisterActivity.class);
        startActivity(intent5);
        finish();
        break;
    default:
        break;
    }
    mDrawerList.setItemChecked(position, true);
    mDrawerList.setSelection(position);
    mDrawerLayout.closeDrawer(mDrawerList);*/

}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

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

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}


@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    viewPager.setCurrentItem(tab.getPosition(), true);
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

public void onPageScrolled(int i, float v, int i2) {
}

public void onPageSelected(int i) {
    mActionBar.getTabAt(i).select();
}

public void onPageScrollStateChanged(int i) {
}
}

1 个答案:

答案 0 :(得分:0)

使用比操作栏更灵活的工具栏,并尝试使用design support library,以便更好,更轻松地实现上述所有要求。