侧滑菜单。。。。。。。。。。。。。。
布局:
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.shenhuiran_1205.MainActivity">
<!--主页面的内容 ctrl+alt+l 格式化代码-->
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<Button
android:id="@+id/btnOpen"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Hello!"/>
<!--用来放置 碎片的容器 -->
<FrameLayout android:id="@+id/frcontent"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="@+id/btnOpen">
</FrameLayout>
</RelativeLayout>
<!---菜单内容 android:layout_gravity="start":菜单在左边 end 菜单在右边 侧滑菜单一般不要超过 320dp -->
<RelativeLayout android:id="@+id/menu"
android:layout_height="match_parent"
android:layout_width="170dp"
android:background="#f00"
android:layout_gravity="start">
<ImageView android:id="@+id/img"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@mipmap/ic_launcher"/>
<ListView android:id="@+id/lv"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="@+id/img"/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
===================================================================
主要代码:
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<String> data=new ArrayList<>();
private DrawerLayout drawerLayout;
private RelativeLayout menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = findViewById(R.id.drawer);
menu = findViewById(R.id.menu);
Button b=findViewById(R.id.btnOpen);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawerLayout.openDrawer(menu);//打开菜单
}
});
ListView listView=findViewById(R.id.lv);
for(int i=0;i<20;i++){
data.add("item"+i);
}
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//关闭菜单
drawerLayout.closeDrawer(menu);
//动态添加fragment
getSupportFragmentManager().beginTransaction().replace(R.id.frcontent,new MyFragment()).commit();
}
});
//添加监听事件
drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
Log.d("zzz","--"+slideOffset);
}
//
@Override
public void onDrawerOpened(View drawerView) {
Toast.makeText(MainActivity.this,"打开了!",Toast.LENGTH_SHORT).show();
}
@Override
public void onDrawerClosed(View drawerView) {
Toast.makeText(MainActivity.this,"关闭了!",Toast.LENGTH_SHORT).show();
}
/**
* 当抽屉滑动状态改变的时候被调用
* 状态值是STATE_IDLE(闲置--0), STATE_DRAGGING(拖拽的--1), STATE_SETTLING(固定--2)中之一。
* 抽屉打开的时候,点击抽屉,drawer的状态就会变成STATE_DRAGGING,然后变成STATE_IDLE
*/
@Override
public void onDrawerStateChanged(int newState) {
}
});
}
}
=========================================================================
Fragment 类
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by jane on 2017/12/8.
*/
public class MyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fitem,container,false);
}
}
====================================================================
fitem.xml布局文件
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.constraint.ConstraintLayout>