DrawerLayout也叫抽屉布局;
要注意,在XML中,根布局要改成DrawerLayout,是v4包的,加上id,
里面的主内容区可以用FrameLayout,然后替换控件,
侧滑区域可以用其他的布局,
<android.support.v4.widget.DrawerLayout** xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
–主内容区域(可以是其他布局类型)
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
侧滑的区域,, 侧滑菜单的默认背景颜色是半透明–>
android:layout_gravity=”left”是滑出的方法
<RelativeLayout
android:background="#ffffff"
android:id="@+id/drawer_relative"
android:layout_gravity="left"
android:layout_width="200dp"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_touxiang"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:src="@mipmap/ic_launcher"
android:layout_width="80dp"
android:layout_height="80dp"/>
<ListView
android:id="@+id/list_view"
android:layout_below="@+id/image_touxiang"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
在main中,
//给listView设置适配器
List<String> titles = new ArrayList<>();
titles.add("头条");
titles.add("社会");
titles.add("国内");
titles.add("军事");
titles.add("财经");
ArrayAdapter<String>adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,android.R.id.text1, titles);
listView.setAdapter(adapter);
//刚进入页面的时候(可以不写这个)
NewsFragment newsFragment = new NewsFragment();
Bundle bundle = new Bundle();
bundle.putString("name",titles.get(0));
newsFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace( R.id.frame_layout , newsFragment).commit();
//设置点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<,?> adapterView, View view, int i, long l) {
//隐藏侧滑菜单
Bundle bundle = new Bundle();
newsFragment.setArguments(bundle); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,newsFragment).commit();
drawerLayout.openDrawer(relativeLayout);//打开侧滑菜单
}
});
//抽屉的监听事件
drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
/**
*
* @param drawerView
* @param slideOffset 表示的抽屉拖出来的宽度,,像素值
*/
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
/**
* 抽屉打开了
* @param drawerView
*/
@Override
public void onDrawerOpened(View drawerView) {
}
/**
* 抽屉关闭
* @param drawerView
*/
@Override
public void onDrawerClosed(View drawerView) {
}
/**
* 抽屉的状态改变
*
* idle – 表示与导航视图没有交互,静止
dragging – 表示目前有与导航视图的交互,拖拽的状态
settling – 表示有与导航视图的交互,并且导航视图正在关闭或打开
* @param newState
*/
@Override
public void onDrawerStateChanged(int newState) {
Log.i(“zhaungtai—”,newState+”—”);
}
});
//在刚进入页面的类,NewsFragment:: 是一个Fragment类
public class NewsFragment extends Fragment {
private ListView listView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_layout,container,false);
//找到id
listView = view.findViewById(R.id.frament_list_view);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//获取数据
String name = getArguments().getString("name", "");
//判断
if (name.equals("头条")){
name = "top";
}else if (name.equals("军事")){
name = "junshi";
}else if (name.equals("社会")){
name = "shehui";
}else if (name.equals("财经")){
name = "caijing";
}else if (name.equals("国内")){
name = "guonei";
}
getDataFromNet(name);
}
private void getDataFromNet(final String name) {
//异步启动
AsyncTask< Void, Void, String> asyncTask = new AsyncTask< Void, Void, String>() {
@Override
protected String doInBackground(Void... voids) {
获取数据
}
return null;
}
@Override
protected void onPostExecute(String s) {
解析,适配器
}
};
asyncTask.execute();
}
//适配器中::
//优化时可以只是复用的优化
if (view == null){
view = View.inflate(context,android.R.layout.simple_list_item_1,null);
}
TextView textView = view.findViewById(android.R.id.text1);
textView.setText(data.get(i).getTitle());
return view;