Android开发之开发者头条(二)实现左滑菜单

2019-12-10 18:16:58王旭

3.左侧菜单item选中背景的布局文件 selector_left_menu_item.xml。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://www.easck.com/apk/res/android">
<item android:drawable="@color/menu_left_item_select" android:state_selected="true"/>
<item android:drawable="@color/white_normal"/>
</selector> 

4.ContentFragment 显示内容的Fragment 这里我加了一个设置内容的方法,就是用来点击左侧切换显示用的。

public class ContentFragment extends Fragment{
private TextView tvContent;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
View rootView=LayoutInflater.from(getActivity()).inflate(R.layout.fragment_content, null);
tvContent=(TextView) rootView.findViewById(R.id.tv_content);
return rootView;
}
public void setContent(String content){
tvContent.setText(content);
}
}

5.drawerLayout与Fragment是什么关系?

我们看到很多使用drawerLayout的代码中都同时使用了Fragment,这会造成误解,以为使用drawerLayout必须用到Fragment,其实这是错误的,使用Fragment是因为在侧滑菜单被点击的时候,主内容区如果内容比较复杂,用Fragment去填充会更容易,如果你的主内容区只是一个简单的字符串,只想在不同菜单点击的时候更新一下字符串的内容,我觉得没必要用Fragment。我这边用Fragment所做的就是更新字符串内容这么简单。

以上内容是针对Android开发之开发者头条(二)实现左滑菜单的全部介绍,希望对大家有所帮助!



注:相关教程知识阅读请移步到Android开发频道。