实践--SlidingMenu简单实例

实践--SlidingMenu简单实例_第1张图片

  • 实现的逻辑就是主界面我是用一个FrameLayout布局,我点击侧滑菜单的不同的选项,我就同时切换相应的Fragment来加载到主界面的布局就可以了。
  • 再有就是在相应的Fragment中添加相应的控件,添加相应的点击事件的逻辑处理等,
  • 特别注意,我在一个界面添加了一个ViewPager来显示东西,这里ViewPager必须使用PagerAdapter,我用那个FragmentPagerAdapter会出现数据显示不出来的问题,具体什么原因我也没搞清楚,知道的大牛请解答下哈。

  • 侧滑菜单的布局

  • 都是简单布局,这里就不解释了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary">
    <ImageView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@mipmap/ic_launcher"/>

    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="新闻" android:id="@+id/button_news" />

    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="游戏" android:id="@+id/button_game" />

    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="娱乐" android:id="@+id/button_fun" />

    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="时事" android:id="@+id/button_thing" />
</LinearLayout>
  • 主界面的布局
  • 仅仅一个FrameLayout和一个自定义的标题栏
  • 自定义标题栏有疑问,请看自定义控件–标题栏
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.lingzhuo.testslidingmenu02.MainActivity">

    <com.lingzhuo.testslidingmenu02.TitleView  android:id="@+id/titleView" android:layout_width="match_parent" android:layout_height="wrap_content"></com.lingzhuo.testslidingmenu02.TitleView>

    <FrameLayout  android:id="@+id/frameLayout_main" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout>

</LinearLayout>
  • 用于对应菜单的点击事件的切换的Fragment都大同小异,这里就贴出其中一个的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">


        <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是娱乐的页面"/>
</LinearLayout>
  • 再然后就是对应的Fragment的对象
public class FragmentFun extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.layout_fragment_fun,null);
        return view;
    }
}
  • 最后是活动的逻辑代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private SlidingMenu slidingMenu;
    private TitleView titleView;
    private Button button_top;
    private TextView textView_top;
    private FragmentManager manager;
    private Button button_news,button_game,button_fun,button_thing;
    private Fragment fragment_news,fragment_game,fragment_fun,fragment_thing;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initListener();
        initSlidingMenu();
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.frameLayout_main, fragment_news, "lifecycle");
        transaction.commit();
        textView_top.setText("新闻");
    }

    private void initListener() {
        button_top.setOnClickListener(this);
    }

    private void initSlidingMenu() {
        slidingMenu=new SlidingMenu(this);
        //设置滑动菜单在左边还是右边
        slidingMenu.setMode(SlidingMenu.LEFT);
        // 设置触摸屏幕的模式
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        slidingMenu.setShadowWidthRes(R.dimen.shadow_width);
        // 设置滑动菜单视图的宽度
        slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        // 设置渐入渐出效果的值
        slidingMenu.setFadeDegree(0.35f);
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        //为侧滑菜单设置布局
        slidingMenu.setMenu(R.layout.layout_slidingmenu_left_);



        button_news= (Button) slidingMenu.findViewById(R.id.button_news);
        button_news.setOnClickListener(this);

        button_game= (Button) slidingMenu.findViewById(R.id.button_game);
        button_game.setOnClickListener(this);

        button_fun= (Button) slidingMenu.findViewById(R.id.button_fun);
        button_fun.setOnClickListener(this);

        button_thing= (Button) slidingMenu.findViewById(R.id.button_thing);
        button_thing.setOnClickListener(this);


    }

    private void init() {
        titleView= (TitleView) findViewById(R.id.titleView);
        textView_top= (TextView) titleView.findViewById(R.id.textView_title);
        button_top= (Button) titleView.findViewById(R.id.button_top);

        fragment_news=new FragmentNews();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_top:
                slidingMenu.toggle();
                break;
            case R.id.button_news:
                //每次点击,我们对用对应的Fragment替换当前添加到主界面的Fragemtn就可以了
                fragment_news=new FragmentNews();
                FragmentTransaction transaction_news = manager.beginTransaction();
                transaction_news.replace(R.id.frameLayout_main, fragment_news);
                transaction_news.commit();
                textView_top.setText("新闻");
                break;
            case R.id.button_game:
                fragment_game=new FragmentGame();
                FragmentTransaction transaction_game = manager.beginTransaction();
                transaction_game.replace(R.id.frameLayout_main, fragment_game);
                transaction_game.commit();
                textView_top.setText("游戏");
                break;
            case R.id.button_fun:
                fragment_fun=new FragmentFun();
                FragmentTransaction transaction_fun = manager.beginTransaction();
                transaction_fun.replace(R.id.frameLayout_main, fragment_fun);
                transaction_fun.commit();
                textView_top.setText("娱乐");
                break;
            case R.id.button_thing:
                fragment_thing=new FragmentThing();
                FragmentTransaction transaction_thing = manager.beginTransaction();
                transaction_thing.replace(R.id.frameLayout_main, fragment_thing);
                transaction_thing.commit();
                textView_top.setText("时事");
                break;
        }
    }
}
  • 这是包含ViewPager的Fragment的代码逻辑
  • 都是简单逻辑,就不再注释了。
public class FragmentGame extends Fragment {
    private List<View> dataList;
    private ViewPager viewPager;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        dataList = new ArrayList<>();
        View view = inflater.inflate(R.layout.layout_fragment_game, null);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager_game);
        View view1=inflater.inflate(R.layout.layout_fragment_thing,null);
        View view2=inflater.inflate(R.layout.layout_fragment_fun,null);
        View view3=inflater.inflate(R.layout.layout_fragment_news,null);
        dataList.add(view1);
        dataList.add(view2);
        dataList.add(view3);

        ((Button)view1.findViewById(R.id.but)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "我被点击了", Toast.LENGTH_SHORT).show();
            }
        });

        MyPagerAdapter adapter = new MyPagerAdapter(dataList);
        viewPager.setAdapter(adapter);


        Button button1 = (Button) view.findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0,false);
                Log.d("FragmentGame", "点击事件");
            }
        });
        Button button2 = (Button) view.findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1,false);
                Log.d("FragmentGame", "点击事件");
            }
        });
        Button button3 = (Button) view.findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(2,false);
                Log.d("FragmentGame", "点击事件");
            }
        });
        return view;
    }
}

最后在啰嗦一句,ViewPager的Adapter,尽量继承自PagerAdapter,当然了如果知道怎么解决继承FragmentPagerAdapter内容不显示的问题,就无所谓了

你可能感兴趣的:(viewpager,SlideMenu,侧滑菜单)