当前位置:   article > 正文

android 侧滑栏简单实现,结合listview 点击跳转Activity_android侧滑栏如何实现跳转

android侧滑栏如何实现跳转

第一步 对应的主布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.v4.widget.DrawerLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/v4_drawerlayout"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context="com.example.samsung.ceshi.DaohanglanActivity">
  9. <FrameLayout
  10. android:id="@+id/v4_drawerlayout_frame"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent" >
  13. <TextView
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. android:id="@+id/v4_text"
  17. android:textSize="22sp"
  18. android:textColor="@color/colorAccent"
  19. android:gravity="center"
  20. />
  21. </FrameLayout>
  22. <ListView
  23. android:layout_width="200dp"
  24. android:layout_height="match_parent"
  25. android:layout_gravity="left"
  26. android:id="@+id/v4_listview"
  27. android:choiceMode="singleChoice"
  28. android:background="@android:color/white" />
  29. </android.support.v4.widget.DrawerLayout>
接下来对应的就是activity

  1. public class DaohanglanActivity extends AppCompatActivity {
  2. private ListView listView;
  3. private DrawerLayout drawerLayout;
  4. private TextView textView;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.daohanglan);
  9. initView();
  10. }
  11. private void initView()
  12. {
  13. listView=(ListView) findViewById(R.id.v4_listview);
  14. drawerLayout=(DrawerLayout) findViewById(R.id.v4_drawerlayout);
  15. textView=(TextView) findViewById(R.id.v4_text);
  16. initDate();
  17. }
  18. private void initDate(){
  19. final List<String> list = new ArrayList<String>();
  20. list.add("网易");
  21. list.add("腾讯");
  22. list.add("新浪");
  23. list.add("搜狐");
  24. ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
  25. listView.setAdapter(adapter);
  26. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  27. @Override
  28. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  29. textView.setText(list.get(position));
  30. if(list.get(position).equals("网易"))
  31. {
  32. Intent intent = new Intent(DaohanglanActivity.this,Main2Activity.class);
  33. startActivity(intent);
  34. }
  35. if(list.get(position).equals("腾讯"))
  36. {
  37. Intent intent = new Intent(DaohanglanActivity.this,MainActivity.class);
  38. startActivity(intent);
  39. }
  40. showDrawerLayout();
  41. }
  42. });
  43. drawerLayout.openDrawer(Gravity.LEFT);//侧滑打开 不设置则不会默认打开
  44. }
  45. private void showDrawerLayout() {
  46. if (!drawerLayout.isDrawerOpen(Gravity.LEFT)) {
  47. drawerLayout.openDrawer(Gravity.LEFT);
  48. } else {
  49. drawerLayout.closeDrawer(Gravity.LEFT);
  50. }
  51. }
  52. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/279467
推荐阅读
相关标签
  

闽ICP备14008679号