您好,登錄后才能下訂單哦!
本篇文章為大家展示了Android中怎么實現美團頂部的滑動菜單,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
首先需要一個代表每個活動主題的 JavaBean
public class Subject { //主題名 private String name; //主題圖標資源ID private int icon; public Subject(String name, int icon) { this.name = name; this.icon = icon; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getIcon() { return icon; } public void setIcon(int icon) { this.icon = icon; } }
然后用一個 List< Subject >
來承載需要顯示的所有主題內容,將這些數據傳給 GridViewAdapter 。因為 ViewPager 有幾個頁面就有幾個GridView ,每個GridView對應的是哪些數據都需要在其內部根據頁面索引進行計算
/** * Created by CZY on 2017/6/23. */ public class GridViewAdapter extends BaseAdapter { private List<Subject> subjectList; private LayoutInflater layoutInflater; //當前頁索引 private int currentIndex; //占滿屏幕時每頁展示的主題個數 private int pageSize; public GridViewAdapter(Context context, List<Subject> subjectList, int currentIndex, int pageSize) { this.layoutInflater = LayoutInflater.from(context); this.subjectList = subjectList; this.currentIndex = currentIndex; this.pageSize = pageSize; } /** * 如果剩余數據能夠完全占滿當前頁,則返回 pageSize * 如果不能,則返回剩余的數據個數 */ @Override public int getCount() { return subjectList.size() > (currentIndex + 1) * pageSize ? pageSize : (subjectList.size() - currentIndex * pageSize); } /** * 計算出正確的索引 */ @Override public Object getItem(int position) { return subjectList.get(position + currentIndex * pageSize); } @Override public long getItemId(int position) { return position + currentIndex * pageSize; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { convertView = layoutInflater.inflate(R.layout.item_grid_view, parent, false); viewHolder = new ViewHolder(); viewHolder.tv_subject = (TextView) convertView.findViewById(R.id.tv_subject); viewHolder.iv_subject = (ImageView) convertView.findViewById(R.id.iv_subject); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } int pos = position + currentIndex * pageSize; viewHolder.tv_subject.setText(subjectList.get(pos).getName()); viewHolder.iv_subject.setImageResource(subjectList.get(pos).getIcon()); return convertView; } private class ViewHolder { private TextView tv_subject; private ImageView iv_subject; } }
需要使用到的布局文件 item_grid_view 如下所示
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="10dp" android:paddingTop="10dp"> <ImageView android:id="@+id/iv_subject" android:layout_width="40dp" android:layout_height="40dp" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/tv_subject" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/iv_subject" android:layout_centerHorizontal="true" android:layout_marginTop="3dp" android:textSize="12sp" /> </RelativeLayout>
使用到了 ViewPager,自然也需要一個 ViewPagerAdapter
/** * Created by CZY on 2017/6/23. */ public class ViewPagerAdapter extends PagerAdapter { private List<View> viewList; public ViewPagerAdapter(List<View> viewList) { this.viewList = viewList; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(viewList.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(viewList.get(position)); return (viewList.get(position)); } @Override public int getCount() { return viewList == null ? 0 : viewList.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } }
在 MainActivity 中進行數據填充
public class MainActivity extends AppCompatActivity { private String[] titles = {"美食", "電影", "酒店住宿", "休閑娛樂", "甜品飲品", "水上樂園", "汽車服務", "美發", "麗人", "景點", "足療按摩", "運動健身", "健身", "超市", "買菜", "今日新單", "外賣", "自助餐", "KTV", "機票/火車票", "周邊游", "小吃快餐", "面膜", "美甲美睫", "火鍋", "生日蛋糕", "母嬰親子", "生活服務", "婚紗攝影", "學習培訓", "家裝", "結婚"}; private List<Subject> subjectList; private LinearLayout ll_dot; //每頁展示的主題個數 private final int pageSize = 10; //當前頁索引 private int currentIndex; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager); ll_dot = (LinearLayout) findViewById(R.id.ll_dot); subjectList = new ArrayList<>(); for (String title : titles) { subjectList.add(new Subject(title, R.drawable.icon)); } //需要的頁面數 int pageCount = (int) Math.ceil(subjectList.size() * 1.0 / pageSize); List<View> viewList = new ArrayList<>(); for (int i = 0; i < pageCount; i++) { GridView gridView = (GridView) getLayoutInflater().inflate(R.layout.layout_grid_view, viewPager, false); gridView.setAdapter(new GridViewAdapter(this, subjectList, i, pageSize)); viewList.add(gridView); gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { int pos = position + currentIndex * pageSize; Toast.makeText(MainActivity.this, subjectList.get(pos).getName(), Toast.LENGTH_SHORT).show(); } }); } viewPager.setAdapter(new ViewPagerAdapter(viewList)); for (int i = 0; i < pageCount; i++) { ll_dot.addView(getLayoutInflater().inflate(R.layout.view_dot, null)); } //使第一個小圓點呈選中狀態 ll_dot.getChildAt(0).findViewById(R.id.v_dot).setBackgroundResource(R.drawable.dot_selected); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { public void onPageSelected(int position) { ll_dot.getChildAt(currentIndex).findViewById(R.id.v_dot).setBackgroundResource(R.drawable.dot_normal); ll_dot.getChildAt(position).findViewById(R.id.v_dot).setBackgroundResource(R.drawable.dot_selected); currentIndex = position; } public void onPageScrolled(int arg0, float arg1, int arg2) { } public void onPageScrollStateChanged(int arg0) { } }); } }
每一個小圓點都對應一個布局文件,其中只包含一個View,重點是設置是設備其 background 屬性,使其呈現圓形
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <View android:id="@+id/v_dot" android:layout_width="8dp" android:layout_height="8dp" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:background="@drawable/dot_normal" /> </RelativeLayout>
上述內容就是Android中怎么實現美團頂部的滑動菜單,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。