您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Android如何實現美團APP的底部滑動菜單”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Android如何實現美團APP的底部滑動菜單”這篇文章吧。
在現在的APP的應用中,類似仿美團APP的底部滑動菜單,應用是挺多的,例如QQ,微信,支付寶都應用到。開發流程如下
1. 底部按鈕
底部按鈕使用RadioButton。
// 按鈕布局<LinearLayout android:id="@+id/llradiogroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"> <RadioGroup android:id="@+id/rg_menu" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/rbtn_home" android:checked="true" android:drawableTop="@drawable/home" android:text="首頁" /> <RadioButton android:id="@+id/rbtn_vest" android:drawableTop="@drawable/investment" android:text="投資" /> <RadioButton android:id="@+id/rbtn_photo" android:drawableTop="@drawable/shooting" android:text="拍拍" /> <RadioButton android:id="@+id/rbtn_mine" android:drawableTop="@drawable/recom_member" android:text="我的" /> <RadioButton android:id="@+id/rbtn_more" android:drawableTop="@drawable/more" android:text="更多" /></RadioGroup>
// style樣式<style name="RadioButton"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">60dp</item> <item name="android:gravity">center</item> <item name="android:layout_weight">1</item> <item name="android:button">@null</item> <item name="android:background">@drawable/menueselector</item></style>
style樣式中的menueselector為背景選擇器,使按鈕選中變色
在Res目錄下新建文件夾(drawable-nodpi),在里面建立新的xml文件,選擇資源類型為Drawable的selector,在每一個selector中建立item選項
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <!--選中背景顏色--> <item android:drawable="@color/checked" android:state_checked="true" /> <!--未選中背景顏色--> <item android:drawable="@color/nochecked" android:state_checked="false" /></selector>
2. 中間的滑動窗口
<android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/llradiogroup"> </android.support.v4.view.ViewPager>
3. 往滑動窗口添加按鈕對應的Fragment, 并監聽相應事件
下面的代碼注意兩點
MainActivity需繼承于FragmentActivity,這樣才能找到其中的getSupportFragmentManager()方法 在書寫Fragment時切記引入的是android.support.v4.app.Fragment,而非android.app.Fragment(android.app.Fragment是在3.0引入的,為了兼容更低版本首選android.support.v4.app.Fragment)
package com.lsw.wealthapp.activity;import android.support.v4.app.Fragment;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import android.support.v4.view.ViewPager;import android.widget.RadioButton;import android.widget.RadioGroup;import com.lsw.wealthapp.R;import com.lsw.wealthapp.fragment.CaptureFragment;import com.lsw.wealthapp.fragment.HomeFragment;import com.lsw.wealthapp.fragment.InvestmentFragment;import com.lsw.wealthapp.fragment.MoreFragment;import com.lsw.wealthapp.fragment.MyFragment;import java.util.ArrayList;import java.util.List;public class MainActivity extends FragmentActivity { // HomeFragmentIndex private static final int HomeViewPagerIndex = 0; // InvestmentFragmentIndex private static final int InvsetViewPagerIndex = 1; // CaptureFragmentIndex private static final int CaptureViewPagerIndex = 2; // MyFragmentIndex private static final int MyViewPagerIndex = 3; // MoreFragmentIndex private static final int MoreViewPagerIndex = 4; private ViewPager viewPager; // 主頁頁面 private HomeFragment homeFragment; // 投資頁面 private InvestmentFragment investmentFragment; //拍照頁面 private CaptureFragment captureFragment; // 我的頁面 private MyFragment myFragment; // 更多頁面 private MoreFragment moreFragment; // Fragment集合 private List<Fragment> fragmentList; // FragmentAdapter private MyPageFramgentAdapter myPageFramgentAdapter; // 菜單RadioGroup private RadioGroup radioGroup; // 主頁按鈕 private RadioButton rbtnHome; // 投資按鈕 private RadioButton rbtnInvest; // 拍照按鈕 private RadioButton rbtnCapture; // 我的按鈕 private RadioButton rbtnMine; // 更多按鈕 private RadioButton rbtnMore; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } private void initViews() { viewPager = (ViewPager) findViewById(R.id.viewPager); homeFragment = new HomeFragment(); investmentFragment = new InvestmentFragment(); captureFragment = new CaptureFragment(); myFragment = new MyFragment(); moreFragment = new MoreFragment(); fragmentList = new ArrayList<Fragment>(); radioGroup = (RadioGroup) findViewById(R.id.rg_menu); rbtnHome = (RadioButton) findViewById(R.id.rbtn_home); rbtnInvest = (RadioButton) findViewById(R.id.rbtn_vest); rbtnCapture = (RadioButton) findViewById(R.id.rbtn_photo); rbtnMine = (RadioButton) findViewById(R.id.rbtn_mine); rbtnMore = (RadioButton) findViewById(R.id.rbtn_more); // 按鈕選中,viewPager展示對應的頁面 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i) { case R.id.rbtn_home: viewPager.setCurrentItem(HomeViewPagerIndex); break; case R.id.rbtn_vest: viewPager.setCurrentItem(InvsetViewPagerIndex); break; case R.id.rbtn_photo: viewPager.setCurrentItem(CaptureViewPagerIndex); break; case R.id.rbtn_mine: viewPager.setCurrentItem(MyViewPagerIndex); break; case R.id.rbtn_more: viewPager.setCurrentItem(MoreViewPagerIndex); break; } } }); //將Fragment加入集合中 fragmentList.add(homeFragment); fragmentList.add(investmentFragment); fragmentList.add(captureFragment); fragmentList.add(myFragment); fragmentList.add(moreFragment); FragmentManager fm = getSupportFragmentManager(); myPageFramgentAdapter = new MyPageFramgentAdapter(fm); viewPager.setAdapter(myPageFramgentAdapter); // viewPager發生改變,對應的按鈕狀態變為選中 viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position) { case HomeViewPagerIndex: rbtnHome.setChecked(true); break; case InvsetViewPagerIndex: rbtnInvest.setChecked(true); break; case CaptureViewPagerIndex: rbtnCapture.setChecked(true); break; case MyViewPagerIndex: rbtnMine.setChecked(true); break; case MoreViewPagerIndex: rbtnMore.setChecked(true); break; default: break; } } @Override public void onPageScrollStateChanged(int state) { } }); } // viewPager所需的適配器 class MyPageFramgentAdapter extends FragmentPagerAdapter { public MyPageFramgentAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } }}
以上是“Android如何實現美團APP的底部滑動菜單”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。