将类活动添加到导航抽屉Android

时间:2016-10-05 10:51:38

标签: android

您能告诉我如何在导航抽屉中添加课堂活动吗?我将创建一个应用程序,我需要有多个类。所有这些类都扩展了活动。我知道如何将片段添加到导航抽屉中,但我不明白如何将类活动添加到导航抽屉中。

1 个答案:

答案 0 :(得分:0)

您可以为此代码添加活动....

public class test extends Activity实现了View.OnClickListener {

private HeaderBar topActionBar;
private DrawerLayoutAdapter drawerLayoutAdapter;
private DrawerLayout drawerLayout;
private ListView leftDrawer;
private String[] drawerTitle = {"", "Test1", "Test2", "Test3", "Test4", "Test5"};
private int[] drawerIcons = {0,
        R.drawable.Test1,
        R.drawable.Test2,
        R.drawable.Test3,
        R.drawable.Test4,
        R.drawable.Test5};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    drawerLayoutAdapter = new DrawerLayoutAdapter(test.this, drawerTitle, drawerIcons);
    setContentView(R.layout.activity_test);
    setId();
    setHeaderBar();
    changeStatusBarColor();
    DrawerLayout.LayoutParams layoutParams = (DrawerLayout.LayoutParams) leftDrawer.getLayoutParams();
    leftDrawer.setLayoutParams(layoutParams);
    leftDrawer.setAdapter(drawerLayoutAdapter);
    if (drawerLayout.isDrawerOpen(leftDrawer)) {
        drawerLayout.closeDrawer(leftDrawer);
    }
    leftDrawer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int pos, long id) {
            drawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
                @Override
                public void onDrawerClosed(View drawerView) {
                    super.onDrawerClosed(drawerView);
                }
            });
            drawerLayout.closeDrawer(leftDrawer);
            displayView(pos);
        }
    });
}

private void changeStatusBarColor() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setStatusBarColor(getResources().getColor(R.color.blue));
    }
}
private void setId() {

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    leftDrawer = (ListView) findViewById(R.id.left_drawer);
    leftDrawer = (ListView) findViewById(R.id.left_drawer);
}

private void setHeaderBar() {
    try {
        topActionBar.ivLeft.setVisibility(View.VISIBLE);
        topActionBar.ivLeft.setBackgroundResource(R.drawable.menu);
        topActionBar.ivLeft.setOnClickListener(this);
        topActionBar.tvTitle.setText(R.string.tilte);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void openDrawer() {
    try {
        leftDrawer.invalidateViews();
        drawerLayout.openDrawer(leftDrawer);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void closeDrawer() {
    try {
        if (drawerLayout.isDrawerOpen(leftDrawer))
            drawerLayout.closeDrawer(leftDrawer);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void displayView(final int positionTitle) {
    closeDrawer();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            switch (positionTitle) {
                case 0:
                    break;
                case 1:
                    Intent a = new Intent(test.this, act1.class);
                    startActivity(a);
                    break;
                case 2:
                    Intent b = new Intent(test.this, act2.class);
                    startActivity(b);
                    break;
                case 3:
                    Intent c = new Intent(test.this, act3.class);
                    startActivity(c);
                    break;
                case 4:
                    Intent d = new Intent(test.this, act4.class);
                    startActivity(d);
                    break;
                case 5:
                    Intent e = new Intent(test.this, act5.class);
                    startActivity(e);
                    break;
                default:
                    break;
            }
        }
    }, 200);
}

@Override
public void onClick(View v) {
    try {
        switch (v.getId()) {
            case R.id.ivLeft:
                openDrawer();
                break;
            default:
                break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}