欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-03-06 10:28:07
先给大家展示下效果图,如果大家大家感觉不错,请参考实现思路及代码 1 viewpager类提供了多界面切换的新效果。 新效果有如下特征: [1] 当前显示一组...

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

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

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;
@override
protected 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>();
///定义第一个gridview
gridview gridview1 =
(gridview) layoutinflater.inflate(r.layout.grid_fragment_home, null);
mygridviewadapter mygridviewadapter1 = new mygridviewadapter(0, 8);
gridview1.setadapter(mygridviewadapter1);
///定义第二个gridview
gridview gridview2 = (gridview)
layoutinflater.inflate(r.layout.grid_fragment_home, null);
mygridviewadapter mygridviewadapter2 = new mygridviewadapter(1, 8);
gridview2.setadapter(mygridviewadapter2);
///定义第三个gridview
gridview 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的pageradapter
viewpagerhomefragment.setadapter(new pageradapter() {
@override
public boolean isviewfromobject(view arg0, object arg1) {
// todo auto-generated method stub
return arg0 == arg1;
}
@override
public int getcount() {
// todo auto-generated method stub
return gridviews.size();
}
@override
public void destroyitem(viewgroup container, int position,
object object) {
// todo auto-generated method stub
container.removeview(gridviews.get(position));
//super.destroyitem(container, position, object);
}
@override
public object instantiateitem(viewgroup container, int position) {
// todo auto-generated method stub
container.addview(gridviews.get(position));
return gridviews.get(position);
}
});
///注册viewpager页选择变化时的响应事件
viewpagerhomefragment.setonpagechangelistener(new viewpager.onpagechangelistener() {
@override
public void onpagescrollstatechanged(int position) {
// todo auto-generated method stub
}
@override
public void onpagescrolled(int arg0, float arg1, int arg2) {
// todo auto-generated method stub
}
@override
public void onpageselected(int position) {
// todo auto-generated method stub
radiobutton radiobutton = (radiobutton)
radiogroup_index_fragment.getchildat(position);
radiobutton.setchecked(true);
}
});
}
///定影gridview的adapter
public class mygridviewadapter extends baseadapter {
private int page;
private int count;
public mygridviewadapter(int page, int count) {
this.page = page;
this.count = count;
}
@override
public int getcount() {
// todo auto-generated method stub
return 8;
}
@override
public catogray getitem(int position) {
// todo auto-generated method stub
return catogarys.get(page * count + position);
}
@override
public long getitemid(int position) {
// todo auto-generated method stub
return 0;
}
@override
public view getview(int position, view convertview, viewgroup parent) {
// todo auto-generated method stub
viewholder 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() {
@override
public void onclick(view view) {
// todo auto-generated method stub
toast.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实现横向滑动功能,希望对大家有所帮助