sld22525482
「已注销」
采纳率12.6%
2016-06-15 07:38 阅读 2.3k

关于ViewPager+GridView,实现GridView横向水平滑动的的问题

在网上看了好多DEMO效果都不是想要的然后发现了这个例子下面贴出来

package com.example.bmvod.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class MainActivity extends Activity {
private LinearLayout linear01;
private LinearLayout linear02;
private List> listView;
private int next = 0;
private ViewPager adViewPager;
private AdPageAdapter adapter;
private ImageView[] imageViews;
private ImageView imageView;
private AtomicInteger atomicInteger = new AtomicInteger(0);
private boolean isContinue = true;
private List gridViewlist = new ArrayList();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
}

private void initView() {
    System.out.println("initView()");
    linear01 = (LinearLayout) findViewById(R.id.view_pager_content);
    linear02 = (LinearLayout) findViewById(R.id.viewGroup);
    listView = new ArrayList<Map<String, Object>>();
    //创建ViewPager
    adViewPager = new ViewPager(this);

    //获取屏幕像素相关信息
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    //根据屏幕信息设置ViewPager广告容器的宽高
    //adViewPager.setLayoutParams(new LayoutParams(dm.widthPixels, dm.heightPixels));

    //将ViewPager容器设置到布局文件父容器中
    linear01.addView(adViewPager);

    getView();

    initCirclePoint();

    adViewPager.setAdapter(adapter);
    adViewPager.addOnPageChangeListener(new AdPageChangeListener());

}

/**
 * ViewPager 页面改变监听器
 */
private final class AdPageChangeListener implements OnPageChangeListener {

    /**
     * 页面滚动状态发生改变的时候触发
     */
    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    /**
     * 页面滚动的时候触发
     */
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    /**
     * 页面选中的时候触发
     */
    @Override
    public void onPageSelected(int arg0) {
        //获取当前显示的页面是哪个页面
        System.out.println("onPageSelected");
        atomicInteger.getAndSet(arg0);
        //重新设置原点布局集合
        for (int i = 0; i < imageViews.length; i++) {
            imageViews[arg0].setBackgroundResource(R.drawable.point_focused);
            if (arg0 != i) {
                imageViews[i].setBackgroundResource(R.drawable.point_unfocused);
            }
        }
    }
}

private void initCirclePoint() {
    System.out.println("initCirclePoint()");
    imageViews = new ImageView[gridViewlist.size()];
    //广告栏的小圆点图标
    for (int i = 0; i < gridViewlist.size(); i++) {
        //创建一个ImageView, 并设置宽高. 将该对象放入到数组中
        imageView = new ImageView(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
        layoutParams.setMargins(10, 0, 10, 0);
        imageView.setLayoutParams(layoutParams);
        imageViews[i] = imageView;

        //初始值, 默认第0个选中
        if (i == 0) {
            imageViews[i].setBackgroundResource(R.drawable.point_focused);
        } else {
            imageViews[i].setBackgroundResource(R.drawable.point_unfocused);
        }
        //将小圆点放入到布局中
        linear02.addView(imageViews[i]);

    }
}

private void getView() {

    int[] intView = {R.drawable.one, R.drawable.two, R.drawable.three, R.drawable.four, R.drawable.five, R.drawable.six, R.drawable.seven, R.drawable.eight, R.drawable.nine, R.drawable.ten
            , R.drawable.eleven, R.drawable.twelve, R.drawable.thirteen, R.drawable.fourteen, R.drawable.fifteen, R.drawable.sixteen, R.drawable.seventeen};
    for (int i = 0; i < intView.length; i++) {
        Map<String, Object> mapView = new HashMap<String, Object>();
        mapView.put("image", intView[i]);
        listView.add(mapView);
    }
    getGridView();
}

private void getGridView() {
    boolean bool = true;
    while (bool) {
        int result = next + 9;
        if (listView.size() != 0 && result < listView.size()) {
            GridView gridView = new GridView(this);
            gridView.setNumColumns(5);
            gridView.setVerticalSpacing(30);
            gridView.setHorizontalSpacing(0);
            List<Map<String, Object>> gridlist = new ArrayList<Map<String, Object>>();
            for (int i = next; i < result; i++) {
                gridlist.add(listView.get(i));

            }
            MyAdapter myAdapter = new MyAdapter(gridlist);
            gridView.setAdapter(myAdapter);
            next = result;
            gridViewlist.add(gridView);

        } else if (result - listView.size() <= 9) {
            System.out.println("剩余多少" + (result - listView.size()));
            List<Map<String, Object>> gridlist = new ArrayList<Map<String, Object>>();
            for (int i = next; i < listView.size(); i++) {
                gridlist.add(listView.get(i));
            }
            GridView gridView = new GridView(this);
            gridView.setNumColumns(5);
            MyAdapter myAdapter = new MyAdapter(gridlist);
            gridView.setAdapter(myAdapter);
            next = listView.size() - 1;
            gridViewlist.add(gridView);
            bool = false;
        } else {
            bool = false;
        }
    }
    adapter = new AdPageAdapter(gridViewlist);
}

private final class AdPageAdapter extends PagerAdapter {
    private List<View> views = null;

    /**
     * 初始化数据源, 即View数组
     */
    public AdPageAdapter(List<View> views) {
        this.views = views;
    }

    /**
     * 从ViewPager中删除集合中对应索引的View对象
     */

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView(views.get(position));
    }

    /**
     * 获取ViewPager的个数
     */
    @Override
    public int getCount() {
        return views.size();
    }

    /**
     * 从View集合中获取对应索引的元素, 并添加到ViewPager中
     */
    @Override
    public Object instantiateItem(View container, final int position) {
        ((ViewPager) container).addView(views.get(position), 0);

// views.get(position).setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// System.out.println("position"+position);
// Intent intent=new Intent(MainActivity.this,OtherViewPage.class);
// MainActivity.this.startActivity(intent);
// }
// });
return views.get(position);
}

    /**
     * 是否将显示的ViewPager页面与instantiateItem返回的对象进行关联
     * 这个方法是必须实现的
     */
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}

private class MyAdapter extends BaseAdapter {
    List<Map<String, Object>> listgrid;

    private MyAdapter(List<Map<String, Object>> listgrid) {
        this.listgrid = listgrid;
    }

    @Override
    public int getCount() {
        return listgrid.size();
    }

    @Override
    public Object getItem(int position) {
        return listgrid.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = getLayoutInflater().inflate(R.layout.grid_view_item, null);
        ImageView getViewLinear = (ImageView) convertView.findViewById(R.id.getViewLinear);
        getViewLinear.setBackgroundResource(Integer.parseInt(listgrid.get(position).get("image").toString()));
        return convertView;
    }

}

}


只有两个布局文件一个只有上下两个布局里面的ViewPager和小圆点都写在代码里,另一个就是GRIDVIEW的ITEM 经过测试 gridView.setVerticalSpacing(30);只对当前页有效 翻到第二页上下两个图片间距为0,,,gridView.setHorizontalSpacing(5);无效!求解啊!!!!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • sld22525482 「已注销」 2016-06-15 07:41

    gridView.setVerticalSpacing(30)要设置两边 因为有两页 这个问题已经发现了 可gridView.setHorizontalSpacing(5);依旧无效!

    点赞 评论 复制链接分享
  • bobokill Pekingese_Weber 2016-06-15 07:57

    这个代码有点复杂,比较乱,建议自己做封装,gradView的效果的话,可以结合stretchMode分析下,这个会影响VSpace和HSpace,是gridview的排版策略,其实主要的问题应该就是gridView这块使用的不熟练

    点赞 评论 复制链接分享

相关推荐