slidingmenu实现左侧滑

时间:2021-05-12 19:28:00

效果图
slidingmenu实现左侧滑
首先要导入slidingmenu的library文件,然后依赖,主页面和左菜单是两个Fragment


BaseFragment

让ContentFragment和LeftmenuFragment继承的基Fragment,实现代码的复用,避免两个子Fragment写重复的代码,initView方法使用抽象修饰,所以子Fragment必须实现。initData方法是可以不用实现的

public abstract class BaseFragment extends Fragment {
//得到activity
public Context context;

public BaseFragment() {
super();
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context=getActivity();//最终Fragment要绑定在MainActivity
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return initView();
}
//让孩子实现自己的视图,实现特有的效果
public abstract View initView();

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initData();//数据绑定上面的视图
}
//如果自页面没有数据,联网请求数据,并且绑定到初始化initView的视图,有数据就直接绑定
public void initData(){

}
}

ContentFragment

主视图

public class ContentFragment extends BaseFragment {


private TextView textView;
@Override
public View initView() {
textView=new TextView(context);
LogUtil.d("初始化主视图View");
return textView;
}

@Override
public void initData() {
super.initData();
LogUtil.d("初始化主视图Data");
textView.setText("主视图");
}
}

LeftmenuFragment

左菜单视图

public class LeftmenuFragment extends BaseFragment{


private TextView textView;
@Override
public View initView() {
textView=new TextView(context);
LogUtil.d("初始化左菜单View");
return textView;
}

@Override
public void initData() {
super.initData();
LogUtil.d("初始化左菜单Data");
textView.setText("左菜单");
}
}

MainActivity

public class MainActivity extends SlidingFragmentActivity{
public static final String CONTENT_FRAGMENT = "content_fragment";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置主页面
setContentView(R.layout.main_activity);
//设置左侧菜单
setBehindContentView(R.layout.activity_leftmenu);
//
SlidingMenu slidingMenu=getSlidingMenu();

slidingMenu.setMode(SlidingMenu.LEFT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
//设置主页占据的宽度
slidingMenu.setBehindOffset(DensityUtil.dip2px(MainActivity.this,200));

//初始化Fragment
initFragment();

}

private void initFragment() {
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction transaction=fm.beginTransaction();
transaction.replace(R.id.ll_main_content,new ContentFragment(),CONTENT_FRAGMENT);//主页面
transaction.replace(R.id.fl_leftmenu,new LeftmenuFragment(), "leftmenu_fragment");//左侧菜单
transaction.commit();

}
}

transaction.replace()是把Fragement栈的所有Fragment都清除再加入指定栈顶

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/ll_main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!--<TextView-->
<!--android:text="主页面"-->
<!--android:textSize="50sp"-->
<!--android:gravity="center"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent" />-->

</LinearLayout>

activity_leftmenu.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fl_leftmenu"
android:layout_width="match_parent" android:layout_height="match_parent">

<!--<TextView-->
<!--android:text="左侧菜单"-->
<!--android:textSize="30sp"-->
<!--android:layout_gravity="center"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent" />-->
</FrameLayout>