實踐--SlidingMenu簡單實例

這裏寫圖片描述

  • 實現的邏輯就是主界面我是用一個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內容不顯示的問題,就無所謂了

發佈了121 篇原創文章 · 獲贊 18 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章