Android實(shí)現(xiàn)九宮格橫向左右滑動(dòng)
項(xiàng)目中大多都會(huì)有很多的分類(lèi),且左右滑動(dòng),如美團(tuán)首頁(yè)(下圖):
不難發(fā)現(xiàn)包含2部分內(nèi)容:1.左右滑動(dòng)的頁(yè)面,2.指示器。
大度一般都會(huì)想到,viewPager+GridView,這里介紹另外的的一種方法,也做下記錄;
GridViewPager+MagicIndicator(萬(wàn)能指示器)
一、引入build.gradle
compile 'com.yhy:gvp:1.1.0' compile 'com.github.hackware1993:MagicIndicator:1.5.0'
如果報(bào)錯(cuò),在項(xiàng)目build.gradle中加入:
repositories { ... maven { url "https://jitpack.io" } }
二、布局代碼
<LinearLayout android:gravity="center_horizontal" android:layout_gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <com.yhy.gvp.widget.GridViewPager android:paddingLeft="15dp" android:paddingRight="15dp" android:id="@+id/grid_viewpager" android:layout_width="match_parent" android:layout_height="150dp" app:num_columns="5" app:page_size="10"></com.yhy.gvp.widget.GridViewPager> <net.lucode.hackware.magicindicator.MagicIndicator android:id="@+id/indicator_container" android:layout_width="wrap_content" android:layout_height="30dp"> </net.lucode.hackware.magicindicator.MagicIndicator> </LinearLayout>
屬性說(shuō)明:
三、關(guān)鍵代碼:
@InjectView(R.id.grid_viewpager) GridViewPager gridViewpager; @InjectView(R.id.indicator_container) MagicIndicator indicatorContainer; 使用ButterKnife這里不多介紹 indexTypeAdapter=new IndexTypeAdapter(getActivity(),R.layout.item_index_type,typeDatas);//頁(yè)面內(nèi)容適配器 gridViewpager.setGVPAdapter(indexTypeAdapter); Log.i("datas",(int) Math.ceil(typeDatas.size() / 10)+""); CommonNavigator commonNavigator = new CommonNavigator(context);//指示器 commonNavigator.setAdapter(new CommonNavigatorAdapter() { @Override public int getCount() { int num=typeDatas.size()/10; if(typeDatas.size() % 10>0){ num++; } return typeDatas==null?0:num; } @Override public IPagerTitleView getTitleView(Context mContext, final int i) { CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(context); View view=View.inflate(context,R.layout.single_image_layout,null); final ImageView iv_image=view.findViewById(R.id.iv_image); iv_image.setImageResource(R.drawable.point_unfocused); commonPagerTitleView.setContentView(view);//指示器引入外部布局,可知指示器內(nèi)容可根據(jù)需求設(shè)置,多樣化 commonPagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() { @Override public void onSelected(int i, int i1) { iv_image.setImageResource(R.drawable.point_focused); } @Override public void onDeselected(int i, int i1) { iv_image.setImageResource(R.drawable.point_unfocused); } @Override public void onLeave(int i, int i1, float v, boolean b) { } @Override public void onEnter(int i, int i1, float v, boolean b) { } }); return commonPagerTitleView; } @Override public IPagerIndicator getIndicator(Context context) { return null; } }); indicatorContainer.setNavigator(commonNavigator); ViewPagerHelper.bind(indicatorContainer, gridViewpager);//頁(yè)面內(nèi)容與指示器關(guān)聯(lián)
四、左右滑動(dòng)頁(yè)面內(nèi)容適配器adapter
public class IndexTypeAdapter extends GVPAdapter<IndexAllTypeBean.TypeListBean> { private Context context; public IndexTypeAdapter(Context context,int layoutResId, @Nullable List<IndexAllTypeBean.TypeListBean> data) { super(layoutResId, data); this.context=context; } @Override public void bind(View item, int position, IndexAllTypeBean.TypeListBean data) { CircleImageView iv_image=item.findViewById(R.id.iv_image); TextView tv_type_name=item.findViewById(R.id.tv_type_name); Picasso.with(context).load(data.getImageUrl()).into(iv_image); tv_type_name.setText(data.getName()); } } //IndexAllTypeBean.TypeListBean 為數(shù)據(jù)內(nèi)容實(shí)體類(lèi),不做介紹
五、內(nèi)容item布局
<LinearLayout android:orientation="vertical" android:paddingBottom="5dp" android:layout_marginLeft="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content"> //自定義圓形圖片,可用ImageView 替代 <com.example.administrator.takeout.ui.widght.CircleImageView android:id="@+id/iv_image" android:gravity="center_horizontal" android:layout_gravity="center_horizontal" android:layout_width="40dp" android:layout_height="40dp" /> <TextView android:layout_marginTop="5dp" android:gravity="center_horizontal" android:layout_gravity="center_horizontal" android:id="@+id/tv_type_name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 九宮格的實(shí)現(xiàn)方法
- android 九宮格滑動(dòng)解鎖開(kāi)機(jī)實(shí)例源碼學(xué)習(xí)
- Android實(shí)現(xiàn)九宮格(GridView中各項(xiàng)平分空間)的方法
- Android打造流暢九宮格抽獎(jiǎng)活動(dòng)效果
- 輕松實(shí)現(xiàn)Android自定義九宮格圖案解鎖
- Android實(shí)現(xiàn)九宮格解鎖
- Android開(kāi)發(fā)之實(shí)現(xiàn)GridView支付寶九宮格
- Android編程之九宮格實(shí)現(xiàn)方法實(shí)例分析
- 輕松實(shí)現(xiàn)安卓(Android)九宮格解鎖
- Android實(shí)現(xiàn)圖片九宮格
相關(guān)文章
Android?中?FrameLayout?布局及屬性的使用詳解
這篇文章主要介紹了Android?中?FrameLayout?布局及屬性的使用,FrameLayout?在實(shí)現(xiàn)簡(jiǎn)單布局時(shí)非常方便,特別適用于疊加式布局,如顯示疊加的圖層或浮動(dòng)按鈕等,需要的朋友可以參考下2024-03-03Android使用Volley框架定制PostUploadRequest上傳文件
這篇文章主要為大家詳細(xì)介紹了Android使用Volley框架定制PostUploadRequest上傳文件或圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android如何實(shí)現(xiàn)設(shè)備的異顯功能詳解
這篇文章主要給大家介紹了關(guān)于Android如何實(shí)現(xiàn)設(shè)備的異顯功能的相關(guān)資料,這篇文章通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02Android系統(tǒng)默認(rèn)對(duì)話框添加圖片功能
這篇文章主要介紹了Android系統(tǒng)默認(rèn)對(duì)話框添加圖片的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01Android 使用jarsigner給apk簽名的方法詳細(xì)介紹
這篇文章主要介紹了Android 使用jarsigner給apk簽名的方法詳細(xì)介紹的相關(guān)資料,APP 完成需要在一些APP 商店進(jìn)行上傳審核,供用戶下載使用,APP 需要簽名認(rèn)證,需要的朋友可以參考下2016-12-12Android自定義View實(shí)現(xiàn)投票進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)投票進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Android 應(yīng)用適配 Android 7.0 權(quán)限要求詳解
今天小編就為大家分享一篇Android 應(yīng)用適配 Android 7.0 權(quán)限要求詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Android仿微信5實(shí)現(xiàn)滑動(dòng)導(dǎo)航條
這篇文章主要為大家詳細(xì)介紹了Android仿微信5實(shí)現(xiàn)滑動(dòng)導(dǎo)航條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08