Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發基於ViewPager+GridView實現仿大眾點評橫向滑動功能

Android開發基於ViewPager+GridView實現仿大眾點評橫向滑動功能

編輯:關於Android編程

先給大家展示下效果圖,如果大家大家感覺不錯,請參考實現思路及代碼

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實現橫向滑動功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved