Android开发基于ViewPager+GridView实现仿大众点评横向滑动功能

Android开发基于ViewPager+GridView实现仿大众点评横向滑动功能,第1张

概述先给大家展示下效果图,如果大家大家感觉不错,请参考实现思路及代码1ViewPager类提供了多界面切换的新效果。

先给大家展示下效果图,如果大家大家感觉不错,请参考实现思路及代码

1 VIEwPager类提供了多界面切换的新效果。

新效果有如下特征:

[1] 当前显示一组界面中的其中一个界面。

[2] 当用户通过左右滑动界面时,当前的屏幕显示当前界面和下一个界面的一部分。

[3]滑动结束后,界面自动跳转到当前选择的界面中

2 介绍里面几个比较重要的方法与接口

1,OnPagechangelistener VIEwPager页面进行切换监听接口 其中我们经常要实现public voID onPageSelected(int arg0),public voID onPageScrolled(int arg0,float arg1,int arg

2),public voID onPageScrollStateChanged(int arg0)着三个方面,其中第一个方法是更多的用到;

2,setCurrentItem(int item),来设置跳转到当前的页面;

3,要显示VIEwPager,当然还少不了PagerAdapter类,进行适配;其中我们经常也要实现以下方法才可以:public voID destroyItem(VIEw arg0,int arg1,Object arg2),public int getCount(),public Object instantiateItem(VIEw arg0,int arg1),public boolean isVIEwFromObject(VIEw arg0,Object arg1),具体的使用方法等会看例子就ok了

3 例子代码

java代码

package mm.shandong.com.testvIEwpagergrID;import androID.content.res.TypedArray;import androID.support.v4.vIEw.PagerAdapter;import androID.support.v4.vIEw.VIEwPager;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.BaseAdapter;import androID.Widget.GrIDVIEw;import androID.Widget.ImageVIEw;import androID.Widget.Radiobutton;import androID.Widget.RadioGroup;import androID.Widget.TextVIEw;import androID.Widget.Toast;import java.util.ArrayList;import java.util.List;import mm.shandong.com.testvIEwpagergrID.entity.Catogray;public class TestVIEwpagerGrIDActivity extends AppCompatActivity {VIEwPager vIEwPagerHomeFragment;List<Catogray> catogarys;String[] catogary_names;int[] catogary_resourceIDs;LayoutInflater layoutInflater;List<VIEw> grIDVIEws;RadioGroup radioGroup_index_fragment;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.activity_test_vIEwpager_grID);catogary_names = getResources().getStringArray(R.array.catogary_names);TypedArray typedArray = getResources().obtainTypedArray(R.array.catogary_resourceIDs);catogary_resourceIDs = new int[typedArray.length()];for (int i = 0; i < typedArray.length(); i++) {catogary_resourceIDs[i] = typedArray.getResourceID(i,0);}catogarys = new ArrayList<Catogray>();vIEwPagerHomeFragment = (VIEwPager) findVIEwByID(R.ID.vIEwPagerHomeFragment);layoutInflater = getLayoutInflater();radioGroup_index_fragment =(RadioGroup) findVIEwByID(R.ID.radioGroup_index_fragment);for (int i = 0; i < catogary_names.length; i++) {Catogray catogary = new Catogray();catogary.setname(catogary_names[i]);catogary.setimage_source(catogary_resourceIDs[i]);catogarys.add(catogary);}initVIEwPager();}public voID initVIEwPager() {grIDVIEws = new ArrayList<VIEw>();///定义第一个GrIDVIEwGrIDVIEw grIDVIEw1 =(GrIDVIEw) layoutInflater.inflate(R.layout.grID_fragment_home,null);MyGrIDVIEwAdapter myGrIDVIEwAdapter1 = new MyGrIDVIEwAdapter(0,8);grIDVIEw1.setAdapter(myGrIDVIEwAdapter1);///定义第二个GrIDVIEwGrIDVIEw grIDVIEw2 = (GrIDVIEw)layoutInflater.inflate(R.layout.grID_fragment_home,null);MyGrIDVIEwAdapter myGrIDVIEwAdapter2 = new MyGrIDVIEwAdapter(1,8);grIDVIEw2.setAdapter(myGrIDVIEwAdapter2);///定义第三个GrIDVIEwGrIDVIEw grIDVIEw3 = (GrIDVIEw)layoutInflater.inflate(R.layout.grID_fragment_home,null);MyGrIDVIEwAdapter myGrIDVIEwAdapter3 = new MyGrIDVIEwAdapter(2,8);grIDVIEw3.setAdapter(myGrIDVIEwAdapter3);grIDVIEws.add(grIDVIEw1);grIDVIEws.add(grIDVIEw2);grIDVIEws.add(grIDVIEw3);///定义vIEwpager的PagerAdaptervIEwPagerHomeFragment.setAdapter(new PagerAdapter() {@OverrIDepublic boolean isVIEwFromObject(VIEw arg0,Object arg1) {// Todo auto-generated method stubreturn arg0 == arg1;}@OverrIDepublic int getCount() {// Todo auto-generated method stubreturn grIDVIEws.size();}@OverrIDepublic voID destroyItem(VIEwGroup container,int position,Object object) {// Todo auto-generated method stubcontainer.removeVIEw(grIDVIEws.get(position));//super.destroyItem(container,position,object);}@OverrIDepublic Object instantiateItem(VIEwGroup container,int position) {// Todo auto-generated method stubcontainer.addVIEw(grIDVIEws.get(position));return grIDVIEws.get(position);}});///注册vIEwPager页选择变化时的响应事件vIEwPagerHomeFragment.setonPagechangelistener(new VIEwPager.OnPagechangelistener() {@OverrIDepublic voID onPageScrollStateChanged(int position) {// Todo auto-generated method stub}@OverrIDepublic voID onPageScrolled(int arg0,int arg2) {// Todo auto-generated method stub}@OverrIDepublic voID onPageSelected(int position) {// Todo auto-generated method stubRadiobutton radiobutton = (Radiobutton)radioGroup_index_fragment.getChildAt(position);radiobutton.setChecked(true);}});}///定影GrIDVIEw的Adapterpublic class MyGrIDVIEwAdapter extends BaseAdapter {private int page;private int count;public MyGrIDVIEwAdapter(int page,int count) {this.page = page;this.count = count;}@OverrIDepublic int getCount() {// Todo auto-generated method stubreturn 8;}@OverrIDepublic Catogray getItem(int position) {// Todo auto-generated method stubreturn catogarys.get(page * count + position);}@OverrIDepublic long getItemID(int position) {// Todo auto-generated method stubreturn 0;}@OverrIDepublic VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {// Todo auto-generated method stubVIEwHolder vIEwHolder = null;final Catogray catogary = catogarys.get(page * count + position);if (convertVIEw == null) {vIEwHolder = new VIEwHolder();convertVIEw = layoutInflater.inflate(R.layout.grID_fragment_home_item,null);vIEwHolder.grID_fragment_home_item_img =(ImageVIEw) convertVIEw.findVIEwByID(R.ID.grID_fragment_home_item_img);vIEwHolder.grID_fragment_home_item_txt =(TextVIEw) convertVIEw.findVIEwByID(R.ID.grID_fragment_home_item_txt);convertVIEw.setTag(vIEwHolder);} else {vIEwHolder = (VIEwHolder) convertVIEw.getTag();}vIEwHolder.grID_fragment_home_item_img.setimageResource(catogary.getimage_source());vIEwHolder.grID_fragment_home_item_txt.setText(catogary.getname());vIEwHolder.grID_fragment_home_item_img.setonClickListener(new VIEw.OnClickListener() {@OverrIDepublic voID onClick(VIEw vIEw) {// Todo auto-generated method stubToast.makeText(TestVIEwpagerGrIDActivity.this,catogary.getname(),Toast.LENGTH_SHORT).show();}});return convertVIEw;}}public class VIEwHolder {public ImageVIEw grID_fragment_home_item_img;public TextVIEw grID_fragment_home_item_txt;}}

以上所述是小编给大家介绍的AndroID开发仿大众点评基于VIEwPager+GrIDVIEw实现横向滑动功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android开发基于ViewPager+GridView实现仿大众点评横向滑动功能全部内容,希望文章能够帮你解决Android开发基于ViewPager+GridView实现仿大众点评横向滑动功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1148124.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存