抽屉

activity_xml 布局

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android=“http://schemas.android.com/apk/res/android
xmlns:app=“http://schemas.android.com/apk/res-auto
xmlns:tools=“http://schemas.android.com/tools
android:id="@+id/dl_root"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“com.example.lenovo.a06mo1.MainActivity”>

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_purple"
    ></FrameLayout>
<LinearLayout
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#595353"
    android:orientation="vertical"
    >
   <ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="center_horizontal"
       android:src="@mipmap/ic_launcher_round"
       />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="网易云"
        />
    <ListView
        android:id="@+id/list_drawer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>
</LinearLayout>

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

MainActivity页面

package com.example.lenovo.a06mo1;

import android.support.annotation.NonNull;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private static final String TAG=“MainActivity”;
private DrawerLayout dlRoot;
private ListView lvDrawer;
private List list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dlRoot=findViewById(R.id.dl_root);
lvDrawer=findViewById(R.id.list_drawer);

    list=new ArrayList<>();
    list.add("我的音乐");
    list.add("我的会员");
    list.add("我的网盘");
    list.add("我的关注");

    lvDrawer.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,list));
    lvDrawer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            NewsFragment fragment=NewsFragment.newInstance(list.get(position));
            getSupportFragmentManager().beginTransaction().replace(R.id.content,fragment).commit();
            dlRoot.closeDrawer(Gravity.START);
        }
    });
    dlRoot.addDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

        }

        @Override
        public void onDrawerOpened(@NonNull View drawerView) {

        }

        @Override
        public void onDrawerClosed(@NonNull View drawerView) {

        }

        @Override
        public void onDrawerStateChanged(int newState) {
          switch (newState){
              case DrawerLayout.STATE_SETTLING:
                  Log.i(TAG,"STATE_SETTLING");
                  break;
              case DrawerLayout.STATE_DRAGGING:
                  Log.i(TAG,"STATE_DRAGGING");
                  break;
              case DrawerLayout.STATE_IDLE:
                  Log.i(TAG,"STATE_IDLE");
                  break;
          }
        }
    });
}

}

fragment_xml页面

<?xml version="1.0" encoding="utf-8"?>



NewsFragment页面

package com.example.lenovo.a06mo1;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**

扫描二维码关注公众号,回复: 3695030 查看本文章
  • Created by lenovo on 2018-10-20.
    */

public class NewsFragment extends Fragment{
private static final String FLAG=“按入”;
private TextView txtShow;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_news, null, false);
txtShow=v.findViewById(R.id.txt_show);
return v;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Bundle bundle=getArguments();
    String result=bundle.getString(FLAG);
    txtShow.setText(result);
}

/**
 * 静态传值
 * @param
 * @return
 */
public static NewsFragment newInstance(String str) {
    NewsFragment fragment=new NewsFragment();
    Bundle bundle=new Bundle();
    bundle.putString(FLAG,str);

    fragment.setArguments(bundle);
    return fragment;
}

}

猜你喜欢

转载自blog.csdn.net/chuanchuandaxia/article/details/83210588