package com.hztbc.android.HorizontalListView;

/*
 * HorizontalListView.java v1.5
 *
 *
 * The MIT License
 * Copyright (c) 2011 Paul Soucy (paul@dev-smart.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.util.LinkedList;
import java.util.Queue;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.Scroller;

/**
 *水平ListView类. 
 * @author lsq
 *
 */
public class HorizontalListView extends AdapterView<ListAdapter> {

public boolean mAlwaysOverrideTouch = true;
 protected ListAdapter mAdapter;
 private int mLeftViewIndex = -1;
 private int mRightViewIndex = 0;
 protected int mCurrentX;
 protected int mNextX;
 private int mMaxX = Integer.MAX_VALUE;
 private int mDisplayOffset = 0;
 protected Scroller mScroller;
 private GestureDetector mGesture;
 private Queue<View> mRemovedViewQueue = new LinkedList<View>();
 private OnItemSelectedListener mOnItemSelected;
 private OnItemClickListener mOnItemClicked;
 private OnItemLongClickListener mOnItemLongClicked;
 private boolean mDataChanged = false;

public HorizontalListView(Context context, AttributeSet attrs) {
  super(context, attrs);

initView();
 }

private synchronized void initView() {
  mLeftViewIndex = -1;
  mRightViewIndex = 0;
  mDisplayOffset = 0;
  mCurrentX = 0;
  mNextX = 0;
  mMaxX = Integer.MAX_VALUE;
  mScroller = new Scroller(getContext());
  mGesture = new GestureDetector(getContext(), mOnGesture);
 }

@Override
 public void setOnItemSelectedListener(
   AdapterView.OnItemSelectedListener listener) {
  mOnItemSelected = listener;
 }

@Override
 public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
  mOnItemClicked = listener;
 }

@Override
 public void setOnItemLongClickListener(
   AdapterView.OnItemLongClickListener listener) {
  mOnItemLongClicked = listener;
 }

private DataSetObserver mDataObserver = new DataSetObserver() {

@Override
  public void onChanged() {
   synchronized (HorizontalListView.this) {
    mDataChanged = true;
    autoScroll = true;
   }
   invalidate();
   requestLayout();
  }

@Override
  public void onInvalidated() {
   autoScroll = true;
   reset();
   invalidate();
   requestLayout();
  }

};

@Override
 public ListAdapter getAdapter() {
  return mAdapter;
 }

@Override
 public View getSelectedView() {
  return null;
 }

@Override
 public void setAdapter(ListAdapter adapter) {
  if (mAdapter != null) {
   mAdapter.unregisterDataSetObserver(mDataObserver);
  }
  mAdapter = adapter;
  mAdapter.registerDataSetObserver(mDataObserver);
  reset();
 }

private synchronized void reset() {
  initView();
  removeAllViewsInLayout();
  requestLayout();
 }

@Override
 public void setSelection(int position) {
  // scrollTo(position);
 }

private void addAndMeasureChild(final View child, int viewPos) {
  LayoutParams params = child.getLayoutParams();
  if (params == null) {
   params = new LayoutParams(LayoutParams.FILL_PARENT,
     LayoutParams.FILL_PARENT);
  }

addViewInLayout(child, viewPos, params, true);
  child.measure(
    MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST),
    MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST));
 }

@Override
 protected synchronized void onLayout(boolean changed, int left, int top,
   int right, int bottom) {
  super.onLayout(changed, left, top, right, bottom);// 进行控件的位置定位,确定在显示窗体的位置,默认不要修改这里
  if (mAdapter == null) {
   return;
  }

if (mDataChanged) {
   int oldCurrentX = mCurrentX;
   initView();// 初始化计算位置的相关变量
   removeAllViewsInLayout();// 移除全部view,准备重新渲染
   mNextX = oldCurrentX;
   mDataChanged = false;
  }

if (mScroller.computeScrollOffset()) {
   int scrollx = mScroller.getCurrX();
   mNextX = scrollx;
  }

if (mNextX <= 0) {
   mNextX = 0;
   mScroller.forceFinished(true);
  }
  if (mNextX >= mMaxX) {
   mNextX = mMaxX;
   mScroller.forceFinished(true);
  }

int dx = mCurrentX - mNextX;

removeNonVisibleItems(dx);// 移除不可见的item
  fillList(dx);
  positionItems(dx);// 进行每个item的位置的确定!--开始以为是在这里面进行控制,实际不是。
  scrollToRight();
  mCurrentX = mNextX;

if (!mScroller.isFinished()) {
   post(new Runnable() {
    @Override
    public void run() {
     requestLayout();
    }
   });

}
 }

private void fillList(final int dx) {
  int edge = 0;
  View child = getChildAt(getChildCount() - 1);
  if (child != null) {
   edge = child.getRight();
  }
  fillListRight(edge, dx);

edge = 0;
  child = getChildAt(0);
  if (child != null) {
   edge = child.getLeft();
  }
  fillListLeft(edge, dx);

}

private void fillListRight(int rightEdge, final int dx) {
  while (rightEdge + dx < getWidth()
    && mRightViewIndex < mAdapter.getCount()) {

View child = mAdapter.getView(mRightViewIndex,
     mRemovedViewQueue.poll(), this);
   addAndMeasureChild(child, -1);
   rightEdge += child.getMeasuredWidth();

if (mRightViewIndex == mAdapter.getCount() - 1) {
    mMaxX = mCurrentX + rightEdge - getWidth();
   }

if (mMaxX < 0) {
    mMaxX = 0;
   }
   mRightViewIndex++;
  }

}

private void fillListLeft(int leftEdge, final int dx) {
  while (leftEdge + dx > 0 && mLeftViewIndex >= 0) {
   View child = mAdapter.getView(mLeftViewIndex,
     mRemovedViewQueue.poll(), this);
   addAndMeasureChild(child, 0);
   leftEdge -= child.getMeasuredWidth();
   mLeftViewIndex--;
   mDisplayOffset -= child.getMeasuredWidth();
  }
 }

private void removeNonVisibleItems(final int dx) {
  View child = getChildAt(0);
  while (child != null && child.getRight() + dx <= 0) {
   mDisplayOffset += child.getMeasuredWidth();
   mRemovedViewQueue.offer(child);
   removeViewInLayout(child);
   mLeftViewIndex++;
   child = getChildAt(0);

}

child = getChildAt(getChildCount() - 1);
  while (child != null && child.getLeft() + dx >= getWidth()) {
   mRemovedViewQueue.offer(child);
   removeViewInLayout(child);
   mRightViewIndex--;
   child = getChildAt(getChildCount() - 1);
  }
 }

// 自动计算每个显示出来的item的位置!自动根据屏幕大小计算出来的!
 private void positionItems(final int dx) {
  if (getChildCount() > 0) {
   // getChildCount()的结果不是全部选择图片的数量,而是这里水平listView显示出现的item的数量.
   mDisplayOffset += dx;
   int left = mDisplayOffset;
   for (int i = 0; i < getChildCount(); i++) {
    View child = getChildAt(i);
    int childWidth = child.getMeasuredWidth();
    child.layout(left, 0, left + childWidth,
      child.getMeasuredHeight());
    left += childWidth;
   }

}
 }

private boolean autoScroll = true;

public synchronized void scrollToRight() {
  if (autoScroll) {
   int maxCount = (int)Math.floor(getWidth()/45.0);
   if (mAdapter.getCount() >=maxCount) {
    mScroller.setFinalX(45 * (mAdapter.getCount()-maxCount));
   } else
    mScroller.setFinalX(0);
   requestLayout();
  }
 }

@Override
 public boolean dispatchTouchEvent(MotionEvent ev) {
  autoScroll = false;
  boolean handled = super.dispatchTouchEvent(ev);
  handled |= mGesture.onTouchEvent(ev);
  return handled;
 }

protected boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
   float velocityY) {
  synchronized (HorizontalListView.this) {
   mScroller.fling(mNextX, 0, (int) -velocityX, 0, 0, mMaxX, 0, 0);
  }
  requestLayout();

return true;
 }

protected boolean onDown(MotionEvent e) {
  mScroller.forceFinished(true);
  return true;
 }

private OnGestureListener mOnGesture = new GestureDetector.SimpleOnGestureListener() {

@Override
  public boolean onDown(MotionEvent e) {
   return HorizontalListView.this.onDown(e);
  }

@Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    float velocityY) {
   return HorizontalListView.this
     .onFling(e1, e2, velocityX, velocityY);
  }

@Override
  public boolean onScroll(MotionEvent e1, MotionEvent e2,
    float distanceX, float distanceY) {
   synchronized (HorizontalListView.this) {
    mNextX += (int) distanceX;
   }
   requestLayout();

return true;
  }

@Override
  public boolean onSingleTapConfirmed(MotionEvent e) {
   for (int i = 0; i < getChildCount(); i++) {
    View child = getChildAt(i);
    if (isEventWithinView(e, child)) {
     if (mOnItemClicked != null) {
      mOnItemClicked.onItemClick(HorizontalListView.this,
        child, mLeftViewIndex + 1 + i,
        mAdapter.getItemId(mLeftViewIndex + 1 + i));
     }
     if (mOnItemSelected != null) {
      mOnItemSelected.onItemSelected(HorizontalListView.this,
        child, mLeftViewIndex + 1 + i,
        mAdapter.getItemId(mLeftViewIndex + 1 + i));
     }
     break;
    }

}
   return true;
  }

@Override
  public void onLongPress(MotionEvent e) {
   int childCount = getChildCount();
   for (int i = 0; i < childCount; i++) {
    View child = getChildAt(i);
    if (isEventWithinView(e, child)) {
     if (mOnItemLongClicked != null) {
      mOnItemLongClicked.onItemLongClick(
        HorizontalListView.this, child, mLeftViewIndex
          + 1 + i,
        mAdapter.getItemId(mLeftViewIndex + 1 + i));
     }
     break;
    }

}
  }

private boolean isEventWithinView(MotionEvent e, View child) {
   Rect viewRect = new Rect();
   int[] childPosition = new int[2];
   child.getLocationOnScreen(childPosition);
   int left = childPosition[0];
   int right = left + child.getWidth();
   int top = childPosition[1];
   int bottom = top + child.getHeight();
   viewRect.set(left, top, right, bottom);
   return viewRect.contains((int) e.getRawX(), (int) e.getRawY());
  }
 };
}

水平ListView类的更多相关文章

  1. 解决水平ListView在ScrollView中出现的滑动冲突

      解决的问题有两个:  1)实现水平滑动的ListView.重写AdapterView,上代码: package com.liucanwen.horizontallistview.view; imp ...

  2. 水平ListView

    /* * HorizontalListView.java v1.5 * * * The MIT License * Copyright (c) 2011 Paul Soucy (paul@dev-sm ...

  3. Flutter常用组件(Widget)解析-ListView

    一个可滚动的列表组件 不管在哪,列表组件都尤为重要和常用. 首先来看个例子: import 'package:flutter/material.dart'; void main () => ru ...

  4. ListView总结

    ListView类作为在Android开发中经常会使用到的组件,作为新手,还是感到这一块变化形式还是很多的,需要慢慢学习.现在这里大概总结一下. 基于数组的ListView:使用android:ent ...

  5. Android开发--ListView的应用

    1.简介 ListView用于以列表的形式展示数据.它在装载数据时,不能使用ListView类的add()等相关方法添加,而要借助Adapter对象进行添加.另外,由于 系统提供的Adapter往往不 ...

  6. Scrollview嵌套listview

    //建立Scrollview类 public class MyScrollView extends ScrollView { public MyScrollView(Context context, ...

  7. ListView的属性及方法详解

    本文转载于:http://blog.csdn.net/vector_yi/article/details/23195411 近期在重新学习Android控件知识,目前进行到ListView,感觉这是一 ...

  8. C# ListView用法详解

    一.ListView类 1.常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. (2) GridLines:设 ...

  9. {Reship}{ListView}C# ListView用法详解

    ======================================================================== This aritcle came from http ...

随机推荐

  1. 白皮 Chapter 1

    6.29 今天主要做了一些1.5中的小结和练习,果然换语言思路也要跟着变么…各种不爽啊不爽… scanf各种忘记&,还有各种忘记return 0… average temperature su ...

  2. Struts2配置文件详解

    解决在断网环境下,配置文件无提示的问题我们可以看到Struts.xml在断网的情况下,前面有一个叹号,这时,我们按alt+/ 没有提示,这是因为” http://struts.apache.org/d ...

  3. MicroERP更新记录2.2:增加B/S查询功能

    提供电脑版及手机版两种浏览方式,仅仅作为一个DEMO展示,由于系统内置报表比较复杂,待广泛收集用户需求之后再逐步完善. 网页文件在安装包内web文件夹中. 下载地址:60.2.39.130/micro ...

  4. JS数组中every(),filter(),forEach(),map(),some()方法学习笔记!

    ES5中定义了五种数组的迭代方法:every(),filter(),forEach(),map(),some(). 每个方法都接受两个参数:要在每一项运行的函数(必选)和运行该函数的作用域的对象-影响 ...

  5. 003-Tuple、Array、Map与文件操作入门实战

    003-Tuple.Array.Map与文件操作入门实战 Tuple 各个元素可以类型不同 注意索引的方式 下标从1开始 灵活 Array 注意for循环的until用法 数组的索引方式 上面的for ...

  6. iOS App 获取从后台返回前台时的页面

    产品美美的给小伙伴提了一个需求,当程序从后台进入前台时,如果是指定的页面,则弹出提示框. 大家首先想到的方法就是通过 AppDelegate.h 进行控制,相对复杂的步骤就是 在程序进入后台时对当前页 ...

  7. block的常见用法

    一.声明和定义 1.声明 声明方式:返回值(^block)(参数).声明时,参数变量名可以省略:使用时,参数变量名不能省略,不然会无法调用传入的参数 void(^block)(); void(^blo ...

  8. ArcMap计算PolyLine中点VBA

    Dim pGeo As IGeometrySet pGeo = [Shape]Dim pPolyline As IPolylineSet pPolyline = pGeoDim pCurve As I ...

  9. utf8转gbk,libcurl中文乱码处理

    这两个转码在网页客户端处理用很常见,所使用的平台为VS2010,字符集采用多字节字符集 utf8转gbk string UTF8ToGBK(const std::string& strUTF8 ...

  10. 黑马程序员——【Java高新技术】——案例:银行业务调度系统

    ---------- android培训.java培训.期待与您交流! ---------- 一.银行业务调度系统需求 Ø 银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗 ...