Android GridView添加头部问题的解决

我们都知道ListView有addHeaderView和addFooterView两个方法。其中addHeaderView是添加头部布局,addFooterView是添加底部布局。但是GridView却没有这两个方法这个时候就需要重写GridView了。geogle官方给出了重写的HeaderGridView不知道为什么没有添加到官方api里面。代码如下:

public class HeaderGridView extends GridView {
  private static final String TAG = "HeaderGridView";
  /**
   * A class that represents a fixed view in a list, for example a header at the top
   * or a footer at the bottom.
   */
  private static class FixedViewInfo {
    /** The view to add to the grid */
    public View view;
    public ViewGroup viewContainer;
    /** The data backing the view. This is returned from {@link ListAdapter#getItem(int)}. */
    public Object data;
    /** <code>true</code> if the fixed view should be selectable in the grid */
    public boolean isSelectable;
  }
  private ArrayList<FixedViewInfo> mHeaderViewInfos = new ArrayList<FixedViewInfo>();
  private void initHeaderGridView() {
    super.setClipChildren(false);
  }
  public HeaderGridView(Context context) {
    super(context);
    initHeaderGridView();
  }
  public HeaderGridView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initHeaderGridView();
  }
  public HeaderGridView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initHeaderGridView();
  }
  @SuppressLint("NewApi")
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    ListAdapter adapter = getAdapter();
    if (adapter != null && adapter instanceof HeaderViewGridAdapter) {
      ((HeaderViewGridAdapter) adapter).setNumColumns(getNumColumns());
    }
  }
  @Override
  public void setClipChildren(boolean clipChildren) {
    // Ignore, since the header rows depend on not being clipped
  }
  /**
   * Add a fixed view to appear at the top of the grid. If addHeaderView is
   * called more than once, the views will appear in the order they were
   * added. Views added using this call can take focus if they want.
   * <p>
   * NOTE: Call this before calling setAdapter. This is so HeaderGridView can wrap
   * the supplied cursor with one that will also account for header views.
   *
   * @param v The view to add.
   * @param data Data to associate with this view
   * @param isSelectable whether the item is selectable
   */
  public void addHeaderView(View v, Object data, boolean isSelectable) {
    ListAdapter adapter = getAdapter();
    if (adapter != null && ! (adapter instanceof HeaderViewGridAdapter)) {
      throw new IllegalStateException(
          "Cannot add header view to grid -- setAdapter has already been called.");
    }
    FixedViewInfo info = new FixedViewInfo();
    FrameLayout fl = new FullWidthFixedViewLayout(getContext());
    fl.addView(v);
    info.view = v;
    info.viewContainer = fl;
    info.data = data;
    info.isSelectable = isSelectable;
    mHeaderViewInfos.add(info);
    // in the case of re-adding a header view, or adding one later on,
    // we need to notify the observer
    if (adapter != null) {
      ((HeaderViewGridAdapter) adapter).notifyDataSetChanged();
    }
  }
  /**
   * Add a fixed view to appear at the top of the grid. If addHeaderView is
   * called more than once, the views will appear in the order they were
   * added. Views added using this call can take focus if they want.
   * <p>
   * NOTE: Call this before calling setAdapter. This is so HeaderGridView can wrap
   * the supplied cursor with one that will also account for header views.
   *
   * @param v The view to add.
   */
  public void addHeaderView(View v) {
    addHeaderView(v, null, true);
  }
  public int getHeaderViewCount() {
    return mHeaderViewInfos.size();
  }
  /**
   * Removes a previously-added header view.
   *
   * @param v The view to remove
   * @return true if the view was removed, false if the view was not a header
   *     view
   */
  public boolean removeHeaderView(View v) {
    if (mHeaderViewInfos.size() > 0) {
      boolean result = false;
      ListAdapter adapter = getAdapter();
      if (adapter != null && ((HeaderViewGridAdapter) adapter).removeHeader(v)) {
        result = true;
      }
      removeFixedViewInfo(v, mHeaderViewInfos);
      return result;
    }
    return false;
  }
  private void removeFixedViewInfo(View v, ArrayList<FixedViewInfo> where) {
    int len = where.size();
    for (int i = 0; i < len; ++i) {
      FixedViewInfo info = where.get(i);
      if (info.view == v) {
        where.remove(i);
        break;
      }
    }
  }
  @SuppressLint("NewApi")
  @Override
  public void setAdapter(ListAdapter adapter) {
    if (mHeaderViewInfos.size() > 0) {
      HeaderViewGridAdapter hadapter = new HeaderViewGridAdapter(mHeaderViewInfos, adapter);
      int numColumns = getNumColumns();
      if (numColumns > 1) {
        hadapter.setNumColumns(numColumns);
      }
      super.setAdapter(hadapter);
    } else {
      super.setAdapter(adapter);
    }
  }
  private class FullWidthFixedViewLayout extends FrameLayout {
    public FullWidthFixedViewLayout(Context context) {
      super(context);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      int targetWidth = HeaderGridView.this.getMeasuredWidth()
          - HeaderGridView.this.getPaddingLeft()
          - HeaderGridView.this.getPaddingRight();
      widthMeasureSpec = MeasureSpec.makeMeasureSpec(targetWidth,
          MeasureSpec.getMode(widthMeasureSpec));
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
  }
  /**
   * ListAdapter used when a HeaderGridView has header views. This ListAdapter
   * wraps another one and also keeps track of the header views and their
   * associated data objects.
   *<p>This is intended as a base class; you will probably not need to
   * use this class directly in your own code.
   */
  private static class HeaderViewGridAdapter implements WrapperListAdapter, Filterable {
    // This is used to notify the container of updates relating to number of columns
    // or headers changing, which changes the number of placeholders needed
    private final DataSetObservable mDataSetObservable = new DataSetObservable();
    private final ListAdapter mAdapter;
    private int mNumColumns = 1;
    // This ArrayList is assumed to NOT be null.
    ArrayList<FixedViewInfo> mHeaderViewInfos;
    boolean mAreAllFixedViewsSelectable;
    private final boolean mIsFilterable;
    public HeaderViewGridAdapter(ArrayList<FixedViewInfo> headerViewInfos, ListAdapter adapter) {
      mAdapter = adapter;
      mIsFilterable = adapter instanceof Filterable;
      if (headerViewInfos == null) {
        throw new IllegalArgumentException("headerViewInfos cannot be null");
      }
      mHeaderViewInfos = headerViewInfos;
      mAreAllFixedViewsSelectable = areAllListInfosSelectable(mHeaderViewInfos);
    }
    public int getHeadersCount() {
      return mHeaderViewInfos.size();
    }
    @Override
    public boolean isEmpty() {
      return (mAdapter == null || mAdapter.isEmpty()) && getHeadersCount() == 0;
    }
    public void setNumColumns(int numColumns) {
      if (numColumns < 1) {
        throw new IllegalArgumentException("Number of columns must be 1 or more");
      }
      if (mNumColumns != numColumns) {
        mNumColumns = numColumns;
        notifyDataSetChanged();
      }
    }
    private boolean areAllListInfosSelectable(ArrayList<FixedViewInfo> infos) {
      if (infos != null) {
        for (FixedViewInfo info : infos) {
          if (!info.isSelectable) {
            return false;
          }
        }
      }
      return true;
    }
    public boolean removeHeader(View v) {
      for (int i = 0; i < mHeaderViewInfos.size(); i++) {
        FixedViewInfo info = mHeaderViewInfos.get(i);
        if (info.view == v) {
          mHeaderViewInfos.remove(i);
          mAreAllFixedViewsSelectable = areAllListInfosSelectable(mHeaderViewInfos);
          mDataSetObservable.notifyChanged();
          return true;
        }
      }
      return false;
    }
    @Override
    public int getCount() {
      if (mAdapter != null) {
        return getHeadersCount() * mNumColumns + mAdapter.getCount();
      } else {
        return getHeadersCount() * mNumColumns;
      }
    }
    @Override
    public boolean areAllItemsEnabled() {
      if (mAdapter != null) {
        return mAreAllFixedViewsSelectable && mAdapter.areAllItemsEnabled();
      } else {
        return true;
      }
    }
    @Override
    public boolean isEnabled(int position) {
      // Header (negative positions will throw an ArrayIndexOutOfBoundsException)
      int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns;
      if (position < numHeadersAndPlaceholders) {
        return (position % mNumColumns == 0)
            && mHeaderViewInfos.get(position / mNumColumns).isSelectable;
      }
      // Adapter
      final int adjPosition = position - numHeadersAndPlaceholders;
      int adapterCount = 0;
      if (mAdapter != null) {
        adapterCount = mAdapter.getCount();
        if (adjPosition < adapterCount) {
          return mAdapter.isEnabled(adjPosition);
        }
      }
      throw new ArrayIndexOutOfBoundsException(position);
    }
    @Override
    public Object getItem(int position) {
      // Header (negative positions will throw an ArrayIndexOutOfBoundsException)
      int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns;
      if (position < numHeadersAndPlaceholders) {
        if (position % mNumColumns == 0) {
          return mHeaderViewInfos.get(position / mNumColumns).data;
        }
        return null;
      }
      // Adapter
      final int adjPosition = position - numHeadersAndPlaceholders;
      int adapterCount = 0;
      if (mAdapter != null) {
        adapterCount = mAdapter.getCount();
        if (adjPosition < adapterCount) {
          return mAdapter.getItem(adjPosition);
        }
      }
      throw new ArrayIndexOutOfBoundsException(position);
    }
    @Override
    public long getItemId(int position) {
      int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns;
      if (mAdapter != null && position >= numHeadersAndPlaceholders) {
        int adjPosition = position - numHeadersAndPlaceholders;
        int adapterCount = mAdapter.getCount();
        if (adjPosition < adapterCount) {
          return mAdapter.getItemId(adjPosition);
        }
      }
      return -1;
    }
    @Override
    public boolean hasStableIds() {
      if (mAdapter != null) {
        return mAdapter.hasStableIds();
      }
      return false;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      // Header (negative positions will throw an ArrayIndexOutOfBoundsException)
      int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns ;
      if (position < numHeadersAndPlaceholders) {
        View headerViewContainer = mHeaderViewInfos
            .get(position / mNumColumns).viewContainer;
        if (position % mNumColumns == 0) {
          return headerViewContainer;
        } else {
          if (convertView == null) {
            convertView = new View(parent.getContext());
          }
          // We need to do this because GridView uses the height of the last item
          // in a row to determine the height for the entire row.
          convertView.setVisibility(View.INVISIBLE);
          convertView.setMinimumHeight(headerViewContainer.getHeight());
          return convertView;
        }
      }
      // Adapter
      final int adjPosition = position - numHeadersAndPlaceholders;
      int adapterCount = 0;
      if (mAdapter != null) {
        adapterCount = mAdapter.getCount();
        if (adjPosition < adapterCount) {
          return mAdapter.getView(adjPosition, convertView, parent);
        }
      }
      throw new ArrayIndexOutOfBoundsException(position);
    }
    @Override
    public int getItemViewType(int position) {
      int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns;
      if (position < numHeadersAndPlaceholders && (position % mNumColumns != 0)) {
        // Placeholders get the last view type number
        return mAdapter != null ? mAdapter.getViewTypeCount() : 1;
      }
      if (mAdapter != null && position >= numHeadersAndPlaceholders) {
        int adjPosition = position - numHeadersAndPlaceholders;
        int adapterCount = mAdapter.getCount();
        if (adjPosition < adapterCount) {
          return mAdapter.getItemViewType(adjPosition);
        }
      }
      return AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER;
    }
    @Override
    public int getViewTypeCount() {
      if (mAdapter != null) {
        return mAdapter.getViewTypeCount() + 1;
      }
      return 2;
    }
    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
      mDataSetObservable.registerObserver(observer);
      if (mAdapter != null) {
        mAdapter.registerDataSetObserver(observer);
      }
    }
    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
      mDataSetObservable.unregisterObserver(observer);
      if (mAdapter != null) {
        mAdapter.unregisterDataSetObserver(observer);
      }
    }
    @Override
    public Filter getFilter() {
      if (mIsFilterable) {
        return ((Filterable) mAdapter).getFilter();
      }
      return null;
    }
    @Override
    public ListAdapter getWrappedAdapter() {
      return mAdapter;
    }
    public void notifyDataSetChanged() {
      mDataSetObservable.notifyChanged();
    }
  }
}

用法和ListView的addHeaderView一样。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 详解如何在Android Studio中添加RecyclerView-v7支持包

    一直知道RecyclerView可以代替ListView.GridView使用,听说功能很强大,但还没有去学习过.今天想学习,没想到还没开始便撞墙了.输入Recycler,只有这两个东西,没有提示RecyclerView,说明支持包中没有. 最后一番百度后,终于解决(真不敢想象没有网络的情况下,怎么开发.怎么解决问题). 1.打开SDK Manager,在Extras树下找到Android Support Library,下载好支持包.RecyclerView在v7-21版本就出来了.我这里不用

  • Android中多行文本末尾添加图片排版问题的解决方法

    前言 最近在项目中需要在某个多行英文文本末尾增加一个图片,尝试了很多方法,最后用了一个比较Tricky的方法解决了,当然这种方法不一定是最好最优的解决办法,记录一下. 问题 如果直接使用drawableRight或者drawableEnd来将图片放置到文本末尾,结果会是这样: 图片会在TextView右边竖直方向的中间位置显示,而不是我们期望的在最后一行位置显示. 这时我们可以尝试使用ImageSpan来将图片放置在最后一行: final TextView text = findViewById

  • Android系统添加自己写的工具

    在android系统的源码有很多工具的源码.存放路径如下 android/system 底层文件系统库.应用及组件,linux自带的 android/external android 使用的一些开源的模组 android/frameworks/base/cmds 一些android重要命令:am.app_proce等 实际上这些工具都是小应用.调试串口接电脑,开启控制台可以使用这些工具.如果USB连电脑,用adb调试工具,输入adb shell指令进入控制台 . dumpsys:能dump系统服

  • Android添加指纹解锁功能的实现代码

    前言 指纹解锁技术成为当前验证用户信息的重要手段,基本上当前手机都配置了指纹解锁.当开发的APP需要加密验证时可以考虑添加系统指纹解锁功能. 添加指纹解锁功能步骤很简单,大致过程如下: 1 添加权限 在Manifest.xml文件中添加访问用户指纹的权限. <uses-permission android:name="android.permission.USE_FINGERPRINT"/> 2 声明系统提供的指纹管理类对象 private FingerprintManag

  • Android百度地图添加Marker失真问题的解决方案

    Marker失真问题 由于公司项目原因,用了很多次百度地图API,基础的地图定位.显示地图就不多说了,这里主要说一下百度地图添加Marker图标. 最开始接触百度地图添加Marker图标的时候,发现自己设置的图标是多大地图上就显示多大,感觉有点失真,看起来很不舒服,但通过网上搜索,并没有找到解决办法,就没怎么注意图标失真的问题,毕竟是一个小项目,不是面向大众的,最近开发的一个项目同样有这个需求,而且是面向大众开发的,我就想为什么摩拜单车的图标那么清晰,我的图标却失真. 就是这么清晰 通过Reso

  • Android中APK签名工具之jarsigner和apksigner详解

    一.工具介绍 jarsigner是JDK提供的针对jar包签名的通用工具, 位于JDK/bin/jarsigner.exe apksigner是Google官方提供的针对Android apk签名及验证的专用工具, 位于Android SDK/build-tools/SDK版本/apksigner.bat 不管是apk包,还是jar包,本质都是zip格式的压缩包,所以它们的签名过程都差不多(仅限V1签名), 以上两个工具都可以对Android apk包进行签名. 1.V1和V2签名的区别 在An

  • Android手机屏幕同步工具asm.jar

    有时候可能需要将手机上的一些操作投影出来,比如一些App Demo的展示等.其实,有专门的硬件设备能干这件事儿,但没必要专门为展示个Demo去花钱买硬件设备.正好,对于Android系统的手机,有一个开源的jar包能干这事儿:Android Screen Monitor(asm.jar) 步骤: 一 . 下载附件 asm.jar 官网 https://code.google.com/p/android-screen-monitor/ 二. 放到任意目录下, 这里放到D盘根目录; 将asm.jar

  • Android实现EditText添加下划线

    在安卓高版本,默认是有下划线的,其默认下划线的颜色是由其主题颜色来控制的! 控制如下: <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item>

  • Android GridView仿微信添加多图效果

    本文实例为大家分享了GridView仿微信添加多图效果展示的具体代码,供大家参考,具体内容如下 栗子惯例,先上GIF 在项目中这种添加⑨图的效果应该是非常常见的,后面有个添加的按钮应该怎么实现,这也许让一部分小白抓狂了吧~来吧,淡漠带你飞,走起~~啦啦啦...... 起飞前先说下,本篇只是讲解九宫格添加图片的效果,至于选择图片的效果是别人写的库,我只是接过来做选择图片用的~ 1.首先这是用GridView实现的 xml布局就一个GridView <GridView android:id="

  • Android编程实现将时间转化成几分钟前、几天前等形式的工具类

    本文实例讲述了Android编程实现将时间转化成几分钟前.几天前等形式的工具类.分享给大家供大家参考,具体如下: 描述: 在Android开发客户端的时候,是在会显示时间是多久之前,比如10分钟前,8小时前,一月前等等.下面提供一个工具类. 代码: public class TimeUtil { private final static long minute = 60 * 1000;// 1分钟 private final static long hour = 60 * minute;// 1

  • Android实现添加商品到购物车动画效果

    本文实例为大家分享了Android添加商品到购物车的具体代码,供大家参考,具体内容如下 实现需求 在商品列表页面中,从列表item添加商品时,实现一个动画,给人感觉像是在添加商品到购物车. 思路 1.获取各个动画执行对象的起点和终点的坐标,利用PathMeasure绘制绘制贝塞尔曲线: 2.为商品图片设置属性动画: 3.为动画设置addUpdateListene监听器,更新view的坐标. 效果图: MainActivity.java package com.zlw.yzm.demo; impo

  • Android添加用户组及自定义App权限的方法

    Android:4.4.4 一.应用场景 在Android设备上,现在我们外接了一个USB转串口的设备,设备节点是/dev/ttyUSB0: # ls -l /dev/ttyUSB0 crw-rw---- 1 root root 188, 0 /dev/ttyUSB0 信息显示:该设备的用户及其所属组别都是root,root的持有者对该设备具有读写权限.但是,我们的App是被排除在root之外的,总之无法读写该设备. 一个解决方案是:赋予others以读写权限.但是这样,任何其他第三方应用都可以

  • Android系统添加Linux驱动

    Linux内核是可配置,进入到linux目录,输入make menuconfig 将会有模块选择界面,前两句是 scripts/kconfig/mconf Kconfig .config - Linux/x86 3.10.65 Kernel Configuration Kconfig是默认选项,.config是当前内核配置文件. 这里介绍如何添加有依赖关系的linux驱动. 1.driver驱动下添加新的模块类 drivers/Makefile文件添加obj-$(CONFIG_TEST_DEVI

  • Android中recyclerView底部添加透明渐变效果

    前言 最近实现一个recyclerView透明渐变的效果,遇到了一些坑,尝试了一些方法,这里记录一下. 效果图 图片在上面显示2列,文字在下面显示1列:底部要有个透明渐变的效果,直到完全看不到. gridLayoutManager动态设置列数 大概是分两类,一类以图片为item 一行2个,一类以文字为item 一行一个. 这个第一反应是用viewType去区分图片类型,但是由于起初不知道gridLayout可以动态列数.就在上面两列,下面一列上为难起来了. 如果统一用一列吧,那就把两个image

  • Android系统工具类详解

    本文实例为大家分享了Android系统工具类的具体代码,供大家参考,具体内容如下 系统工具类 public class systemUtil { //隐藏ipad底部虚拟按键栏 @RequiresApi(api = Build.VERSION_CODES.KITKAT) public static void closeBottomBar(Activity activity){ Window _window = activity.getWindow(); WindowManager.LayoutP

  • 超实用的android网络工具类

    在实际开发中,往往一些工具类对我们的帮助是非常大的,借此,我在前人各位前辈的基础上,整理了一个网络的工具类,特此献上: /** * @类名:NetUtil * @类描述:网络判断处理类 * @创建时间:2015年2月12日-上午9:34:32 * @修改人: * @修改时间: * @修改备注: * @版本: */ public class NetUtil { /* 网络状态 */ public static boolean isNet = true; public static enum net

随机推荐