android水平循环滚动控件使用详解

本文实例为大家分享了android水平循环滚动控件的具体代码,供大家参考,具体内容如下

CycleScrollView.java

package com.example.test; 

import android.content.Context;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller; 

@SuppressWarnings("deprecation")
public class CycleScrollView<T> extends ViewGroup implements OnGestureListener { 

 static final String TAG = "CycleScrollView";
 Context mContext; 

 /**
  * Scroll velocity.
  */
 public static final long SCROLL_VELOCITY = 50; 

 /**
  * Scroll offset.
  */
 public static final int SCROLL_OFFSET = -1; 

 /**
  * Touch delay.
  */
 public static final long TOUCH_DELAYMILLIS = 2000; 

 /**
  * Fling duration.
  */
 public static final int FLING_DURATION = 2000; 

 /**
  * Filing max velocity x.
  */
 public static final int MAX_VELOCITY_X = 1000; 

 private GestureDetector detector;
 private Handler mHandler;
 private Scroller mScroller; 

 /**
  * Callback interface adapter and OnItemClick.
  */
 private CycleScrollAdapter<T> mAdapter;
 private OnItemClickListener mOnItemClickListener; 

 /**
  * Scroll index
  */
 private int mPreIndex;
 private int mCurrentIndex;
 private int mNextIndex;
 private View mCurrentView;
 private View mPreView;
 private View mNextView; 

 private float mLastMotionX; 

 // The reLayout is false can not invoke onLayout.
 private boolean reLayout = false; 

 // If the item count more than screen that can scroll.
 private boolean canScroll = false; 

 // A flag for switch current view.
 private boolean mCurrentViewAtLeft = true; 

 // Fling distance.
 private int mFlingX = 0; 

 private boolean isMoveAction = false; 

 private int defaultItemY = 10; 

 private int maxItemCount = 7; 

 private int initItemX = 20; 

 /**
  * The screen width.
  */
 private int screenWidth; 

 /**
  * Item view height.
  */
 private int itemHeight; 

 /**
  * Item view width.
  */
 private int itemWidth; 

 /**
  * Item view layout x.
  */
 private int itemX = getInitItemX(); 

 /**
  * Item view layout y.
  */
 private int itemY = defaultItemY; 

 // Auto scroll view task.
 private final Runnable mScrollTask = new Runnable() { 

  @Override
  public void run() {
   if (canScroll) {
    scrollView(SCROLL_OFFSET);
    mHandler.postDelayed(this, SCROLL_VELOCITY);// Loop self.
   }
  }
 }; 

 public CycleScrollView(Context context) {
  super(context);
  onCreate(context);
 } 

 public CycleScrollView(Context context, AttributeSet attrs) {
  super(context, attrs);
  onCreate(context);
 } 

 public CycleScrollView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  onCreate(context);
 } 

 private void onCreate(Context context) {
  mContext = context;
  detector = new GestureDetector(this);
  mHandler = new Handler();
  mScroller = new Scroller(context);
 } 

 /**
  * Create scroll index.
  */
 public void createIndex() {
  if (canScroll) {
   mPreIndex = maxItemCount - 1;
   mCurrentIndex = 0;
   mNextIndex = 1;
   mPreView = getChildAt(mPreIndex);
   mCurrentView = getChildAt(mCurrentIndex);
   mNextView = getChildAt(mNextIndex);
  }
 } 

 /**
  * Set item click callback.
  *
  * @param onItemClickListener
  *   The callback
  */
 public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
  mOnItemClickListener = onItemClickListener;
 } 

 /**
  * Set itemAdapter for addItem and bindItem.
  *
  * @param itemAdapter
  */
 public void setAdapter(CycleScrollAdapter<T> adapter) {
  mAdapter = adapter;
 } 

 /**
  * Start auto scroll.
  */
 public void startScroll() {
  if (canScroll) {
   mHandler.post(mScrollTask);
  }
 } 

 /**
  * Stop auto scroll and filing scroll task.
  */
 public void stopScroll() {
  mHandler.removeCallbacks(mScrollTask);
 } 

 /**
  * Delay start auto scroll task.
  */
 public void delayStartScroll() {
  if (canScroll) {
   mHandler.postDelayed(mScrollTask, TOUCH_DELAYMILLIS);
  }
 } 

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  int count = this.getChildCount();
  for (int i = 0; i < count; i++) {
   View child = this.getChildAt(i);
   child.measure(widthMeasureSpec, heightMeasureSpec);
  }
 } 

 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  /**
   * On layout set the child view layout by x y width and height.
   */
  if (reLayout) {// Run one times.
   for (int i = 0; i < getChildCount(); i++) {
    View child = this.getChildAt(i);
    child.setVisibility(View.VISIBLE);
    child.layout(itemX, getItemY(), itemX + getItemWidth(),
      getItemY() + getItemHeight());
    itemX += getItemMargin();
   }
   reLayout = !reLayout;
  }
 } 

 /**
  * When fling view run the fling task scroll view.
  */
 @Override
 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
   float velocityY) { 

  if (e1 == null || e2 == null) {
   return false;
  } 

  // When deltaX and velocityX not good return false.
  if (Math.abs(velocityX) < MAX_VELOCITY_X) {
   return false;
  } 

  // Get the delta x.
  float deltaX = (e1.getX() - e2.getX()); 

  /**
   * If can fling stop other scroll task at first , delay the task after
   * fling.
   */
  mHandler.removeCallbacks(mScrollTask);
  if (canScroll) {
   mHandler.postDelayed(mScrollTask, TOUCH_DELAYMILLIS
     + FLING_DURATION - 1000);
  } 

  /**
   * The flingX is fling distance.
   */
  mFlingX = (int) deltaX; 

  // Start scroll with fling x.
  mScroller.startScroll(0, 0, mFlingX, 0, FLING_DURATION);
  return false;
 } 

 @Override
 public void computeScroll() {
  if (canScroll && mScroller.computeScrollOffset()) {
   /**
    * The Scroller.getCurrX() approach mFlingX , the deltaX more and
    * more small.
    */
   int deltaX = mFlingX - mScroller.getCurrX();
   scrollView(-deltaX / 10);
   postInvalidate();
  }
 } 

 /**
  * When touch event is move scroll child view.
  */
 @Override
 public boolean onTouchEvent(MotionEvent ev) { 

  // Get event x,y at parent view.
  final float x = ev.getX(); 

  /**
   * Get event x,y at screen.
   */
  final int rawX = (int) ev.getRawX();
  final int rawY = (int) ev.getRawY(); 

  switch (ev.getAction()) {
  case MotionEvent.ACTION_DOWN:
   // Reset isMoveAction.
   isMoveAction = false;
   // Get motionX.
   mLastMotionX = x;
   break;
  case MotionEvent.ACTION_MOVE:
   // When action move set isMoveAction true.
   isMoveAction = true;
   // Only support one pointer.
   if (ev.getPointerCount() == 1) {
    // Compute delta X.
    int deltaX = 0;
    deltaX = (int) (x - mLastMotionX);
    mLastMotionX = x;
    // When canScroll is true, scrollView width deltaX.
    if (canScroll) {
     scrollView(deltaX);
    }
   }
   break;
  case MotionEvent.ACTION_UP:
   /**
    * If not move find click item and invoke click event.
    */
   if (!isMoveAction) {
    View view = getClickItem(rawX, rawY);
    if (view != null) {
     mOnItemClickListener.onItemClick(Integer.valueOf(view
       .getTag().toString()));
    }
   }
   break;
  }
  return this.detector.onTouchEvent(ev);
 } 

 /**
  * Get click item view by rawX and rawY.
  * @param rawX the x at screen.
  * @param rawY the y at screen.
  * @return the click item view.
  */
 private View getClickItem(final int rawX, final int rawY) {
  for (int i = 0; i < getChildCount(); i++) {
   View child = getChildAt(i);
   // Get item view rect.
   Rect rect = new Rect();
   child.getGlobalVisibleRect(rect);
   // If click point on the item view, invoke the click event.
   if (rect.contains(rawX, rawY)) {
    return child;
   }
  }
  return null;
 } 

 /**
  * Scroll view by delta x.
  *
  * @param deltaX
  *   The scroll distance.
  */
 private void scrollView(int deltaX) {
  // Move child view by deltaX.
  moveChildView(deltaX);
  // After move change index.
  if (deltaX < 0) {// move left
   // If current at right switch current view to left.
   switchCurrentViewToLeft();
   // change previous current next index.
   moveToNext();
  } else {// move right
   // If current at left switch current view to right.
   switchCurrentViewToRight();
   // change previous current next index.
   moveToPre();
  }
  invalidate();
 } 

 /**
  * Move view by delta x.
  *
  * @param deltaX
  *   The move distance.
  */
 private void moveChildView(int deltaX) {
  for (int i = 0; i < getChildCount(); i++) {
   View child = getChildAt(i);
   child.layout(child.getLeft() + deltaX, child.getTop(),
     child.getRight() + deltaX, child.getBottom());
  }
 } 

 /**
  * Current event is move to left, if current view at right switch current
  * view to left.
  */
 private void switchCurrentViewToLeft() {
  if (!mCurrentViewAtLeft) {
   mPreIndex = mCurrentIndex;
   mCurrentIndex = mNextIndex;
   mNextIndex++;
   if (mNextIndex > maxItemCount - 1) {
    mNextIndex = 0;
   }
   mCurrentView = getChildAt(mCurrentIndex);
   mPreView = getChildAt(mPreIndex);
   mNextView = getChildAt(mNextIndex);
   mCurrentViewAtLeft = !mCurrentViewAtLeft;
  }
 } 

 /**
  * Current event is move to right, if current view at left switch current
  * view to right.
  */
 private void switchCurrentViewToRight() {
  if (mCurrentViewAtLeft) {
   mNextIndex = mCurrentIndex;
   mCurrentIndex = mPreIndex;
   mPreIndex--;
   if (mPreIndex < 0) {
    mPreIndex = maxItemCount - 1;
   }
   mCurrentView = getChildAt(mCurrentIndex);
   mPreView = getChildAt(mPreIndex);
   mNextView = getChildAt(mNextIndex);
   mCurrentViewAtLeft = !mCurrentViewAtLeft;
  }
 } 

 /**
  * Current event is move to left,if current view move out of screen move the
  * current view to right and reBind the item change index.
  */
 private void moveToNext() {
  if (mCurrentView.getRight() < 0) {
   mCurrentView.layout(mPreView.getLeft() + getItemMargin(),
     getItemY(), mPreView.getLeft() + getItemMargin()
       + getItemWidth(), getItemY() + getItemHeight()); 

   if (mCurrentView.getTag() != null) {
    int listIndex = (Integer) mCurrentView.getTag();
    int index = (listIndex + maxItemCount) % mAdapter.getCount();
    mAdapter.bindView(mCurrentView, mAdapter.get(index));
    mCurrentView.setTag(index);
   } 

   mPreIndex = mCurrentIndex;
   mCurrentIndex = mNextIndex;
   mNextIndex++;
   if (mNextIndex > maxItemCount - 1) {
    mNextIndex = 0;
   }
   mCurrentView = getChildAt(mCurrentIndex);
   mPreView = getChildAt(mPreIndex);
   mNextView = getChildAt(mNextIndex);
   moveToNext();
  }
 } 

 /**
  * Current event is move to right,if current view move out of screen move
  * the current view to left and reBind the item change index.
  */
 private void moveToPre() {
  if (mCurrentView.getLeft() > getScreenWidth()) {
   mCurrentView.layout(mNextView.getLeft() - getItemMargin(),
     getItemY(), mNextView.getLeft() - getItemMargin()
       + getItemWidth(), getItemY() + getItemHeight()); 

   if (mCurrentView.getTag() != null) {
    int listIndex = (Integer) mCurrentView.getTag();
    int index = (listIndex - maxItemCount + mAdapter.getCount())
      % mAdapter.getCount();
    mAdapter.bindView(mCurrentView, mAdapter.get(index));
    mCurrentView.setTag(index);
   } 

   mNextIndex = mCurrentIndex;
   mCurrentIndex = mPreIndex;
   mPreIndex--;
   if (mPreIndex < 0) {
    mPreIndex = maxItemCount - 1;
   }
   mCurrentView = getChildAt(mCurrentIndex);
   mPreView = getChildAt(mPreIndex);
   mNextView = getChildAt(mNextIndex);
   moveToPre();
  }
 } 

 @Override
 public boolean onDown(MotionEvent e) {
  return true;
 } 

 @Override
 public void onShowPress(MotionEvent e) {
 } 

 @Override
 public boolean onSingleTapUp(MotionEvent e) {
  return false;
 } 

 @Override
 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
   float distanceY) {
  return false;
 } 

 @Override
 public void onLongPress(MotionEvent e) {
 } 

 public int getMaxItemCount() {
  return maxItemCount;
 } 

 public void setMaxItemCount(int maxItemCount) {
  this.maxItemCount = maxItemCount;
 } 

 public void setReLayout(boolean reLayout) {
  this.reLayout = reLayout;
 } 

 public void setCanScroll(boolean canScroll) {
  this.canScroll = canScroll;
 } 

 public int getItemX() {
  return itemX;
 } 

 public void setItemX(int itemX) {
  this.itemX = itemX;
 } 

 public int getItemY() {
  return itemY;
 } 

 public void setItemY(int itemY) {
  this.itemY = itemY;
 } 

 public int getItemWidth() {
  return itemWidth;
 } 

 public void setItemWidth(int itemWidth) {
  this.itemWidth = itemWidth;
 } 

 public int getItemHeight() {
  return itemHeight;
 } 

 public void setItemHeight(int itemHeight) {
  this.itemHeight = itemHeight;
 } 

 public int getItemMargin() {
  return (screenWidth - itemWidth * (maxItemCount - 1) - initItemX * 2)/(maxItemCount - 2) + itemWidth;
 } 

 public int getScreenWidth() {
  return screenWidth;
 } 

 public void setScreenWidth(int screenWidth) {
  this.screenWidth = screenWidth;
 } 

 public int getInitItemX() {
  return initItemX;
 } 

 public void setInitItemX(int initItemX) {
  this.initItemX = initItemX;
 } 

 /**
  * The interface for item click callback.
  */
 interface OnItemClickListener {
  public boolean onItemClick(int position);
 } 

} 

CycleScrollAdapter.java

package com.example.test; 

import java.util.List; 

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View; 

public abstract class CycleScrollAdapter<T> { 

 private List<T> list;
 private CycleScrollView<T> mCycleScrollView;
 Context mContext; 

 /**
  * Initial CycleScrollAdapter bind list to view.
  *
  * @param list
  *   The list data.
  * @param cycleScrollView
  *   The CycleScrollView.
  * @param context
  *   The Context.
  */
 public CycleScrollAdapter(List<T> list, CycleScrollView<T> cycleScrollView,
   Context context) {
  this.list = list;
  mContext = context;
  mCycleScrollView = cycleScrollView;
  mCycleScrollView.setAdapter(this);
  GetScreenWidthPixels();
  initView(list);
 } 

 /**
  * Get screen width pixels.
  */
 private void GetScreenWidthPixels() {
  DisplayMetrics dm = new DisplayMetrics();
  Activity a = (Activity) mContext;
  a.getWindowManager().getDefaultDisplay().getMetrics(dm);
  mCycleScrollView.setScreenWidth(dm.widthPixels);
 } 

 /**
  * Bind list to view.
  *
  * @param list
  *   The list data.
  */
 protected void initView(List<T> list) {
  if (list == null || list.size() == 0) {
   return;
  } 

  // Clear all view from ViewGroup at first.
  mCycleScrollView.removeAllViewsInLayout(); 

  // Loop list.
  for (int i = 0; i < list.size(); i++) {
   /**
    * If list size more than MaxItemCount break the loop, only create
    * view count is MaxItemCount.
    */
   if (i == mCycleScrollView.getMaxItemCount()) {
    break;
   } 

   /**
    * If list size less than MaxItemCount at the last loop reLayout
    * otherwise at the MaxItemCount index reLayout.
    */
   if (i == list.size() - 1
     || i == mCycleScrollView.getMaxItemCount() - 1) {
    mCycleScrollView.setItemX(mCycleScrollView.getInitItemX());
    mCycleScrollView.setReLayout(true);
   }
   add(list.get(i), i);
  } 

  /**
   * If list count more than MaxItemCount the view can scroll otherwise
   * can not scroll.
   */
  if (list.size() >= mCycleScrollView.getMaxItemCount()) {
   mCycleScrollView.setCanScroll(true);
  } else {
   mCycleScrollView.setCanScroll(false);
  } 

  /**
   * If list count more than MaxItemCount reBuild index.
   */
  mCycleScrollView.createIndex();
 } 

 /**
  * Get list size.
  *
  * @return The list size.
  */
 public int getCount() {
  return list.size();
 } 

 /**
  * Returns the element at the specified location in this
  *
  * @param index
  *   the index of the element to return.
  * @return the element at the specified location.
  */
 public T get(int index) {
  return list.get(index);
 } 

 /**
  * Adds the specified object at the end of this and refresh view.
  *
  * @param t
  *   the object to add.
  */
 public void addItem(T t) {
  list.add(t);
  initView(list);
 } 

 /**
  * Removes the first occurrence of the specified object from this and
  * refresh view.
  *
  * @param t
  *   the object to remove.
  */
 public void removeItem(T t) {
  list.remove(t);
  initView(list);
 } 

 /**
  * Add the specified view to the index.
  *
  * @param t
  *   The data to add.
  * @param index
  *   the index.
  */
 private void add(T t, int index) {
  View view = getView(t);
  ComputeItemSize(view);
  mCycleScrollView.addView(view);
  view.setTag(index);
 } 

 /**
  * If item size is null compute item size.
  *
  * @param view
  *   the item view.
  */
 private void ComputeItemSize(View view) {
  if (mCycleScrollView.getItemWidth() == 0
    || mCycleScrollView.getItemHeight() == 0) {
   int w = View.MeasureSpec.makeMeasureSpec(0,
     View.MeasureSpec.UNSPECIFIED);
   int h = View.MeasureSpec.makeMeasureSpec(0,
     View.MeasureSpec.UNSPECIFIED);
   view.measure(w, h);
   int height = view.getMeasuredHeight();
   int width = view.getMeasuredWidth();
   mCycleScrollView.setItemHeight(height);
   mCycleScrollView.setItemWidth(width);
  }
 } 

 /**
  * Get item view.
  *
  * @param t
  *   the data need bind to view.
  * @return the view.
  */
 public abstract View getView(T t); 

 /**
  * Bind the item to view.
  *
  * @param child
  *   the item view need bind.
  * @param t
  *   the item.
  */
 public abstract void bindView(View child, T t);
}

以上两个是核心类,下面是测试代码。

实现CycleScrollAdapter

AppCycleScrollAdapter.java绑定视图和应用数据

package com.example.test; 

import java.util.List; 

import android.content.Context;
import android.content.pm.PackageInfo;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView; 

public class AppCycleScrollAdapter extends CycleScrollAdapter<PackageInfo> { 

 public AppCycleScrollAdapter(List<PackageInfo> list,
   CycleScrollView<PackageInfo> cycleScrollView, Context context) {
  super(list, cycleScrollView, context);
 } 

 @Override
 protected void initView(List<PackageInfo> list) {
  super.initView(list);
 } 

 @Override
 public void bindView(View child, PackageInfo pi) {
  ImageView image = (ImageView) child.findViewById(R.id.item_image);
  TextView text = (TextView) child.findViewById(R.id.item_text);
  image.setImageDrawable(pi.applicationInfo.loadIcon(mContext
    .getPackageManager()));
  text.setText(pi.applicationInfo.loadLabel(mContext.getPackageManager()));
 } 

 @Override
 public View getView(PackageInfo pi) {
  View view = View.inflate(mContext, R.layout.view_item, null);
  // inflate APP icon view
  ImageView image = (ImageView) view.findViewById(R.id.item_image);
  // inflate APP name view
  TextView text = (TextView) view.findViewById(R.id.item_text);
  image.setImageDrawable(pi.applicationInfo.loadIcon(mContext
    .getPackageManager()));
  text.setText(pi.applicationInfo.loadLabel(mContext.getPackageManager()));
  return view;
 }
}

入口Activity

package com.example.test; 

import java.util.List; 

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.view.Menu; 

public class MainActivity extends Activity{ 

 private CycleScrollView<PackageInfo> mCycleScrollView;
 private AppCycleScrollAdapter mAdapter; 

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

  mCycleScrollView = ((CycleScrollView<PackageInfo>) this.findViewById(R.id.cycle_scroll_view)); 

  /**
   * Get APP list and sort by update time.
   */
  List<PackageInfo> list = this.getPackageManager()
    .getInstalledPackages(0); 

  mAdapter = new AppCycleScrollAdapter(list, mCycleScrollView, this); 

 } 

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 } 

}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 > 

 <ImageView
  android:id="@+id/item_image"
  android:layout_width="60dip"
  android:layout_height="60dip"
  android:layout_y="5dip"
  android:layout_x="10dip"
   /> 

 <TextView
  android:id="@+id/item_text"
  android:layout_width="80dip"
  android:layout_height="20dip"
  android:layout_y="65dip"
  android:layout_x="0dip"
  android:gravity="center_horizontal" /> 

</AbsoluteLayout>
[java] view plain copy
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent" > 

 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:layout_centerVertical="true"
  android:text="@string/hello_world"
  tools:context=".MainActivity" /> 

  <com.example.test.CycleScrollView
  android:id="@+id/cycle_scroll_view"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#B9000000"
  /> 

</RelativeLayout> 

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

(0)

相关推荐

  • Android使用RecyclerView实现水平滚动控件

    前言 相信大家都知道Android滚动控件的实现方式有很多, 使用RecyclerView也比较简单. 做了一个简单的年龄滚动控件, 让我们来看看RecyclerView的使用方式, 主要有以下几点: (1) 对齐控件中心位置. (2) 计算滚动距离. (3) 高亮中心视图. (4) 实时显示中心数据. (5) 停止时自动对齐. (6) 滚动时, 设置按钮状态开关. 效果 1. 框架 主要关注RecyclerView部分逻辑. /** * 初始化年龄滑动条 */ private void ini

  • Android开发实现自定义水平滚动的容器示例

    本文实例讲述了Android开发实现自定义水平滚动的容器.分享给大家供大家参考,具体如下: public class HorizontalScrollView extends ViewGroup { //手势 private GestureDetector mGestureDetector; private HorizontalScroller mScroller; private int curID; //快速滑动 private boolean isFlying; //--回调函数-----

  • Android使用Recyclerview实现图片水平自动循环滚动效果

    简介: 本篇博客主要介绍的是如何使用RecyclerView实现图片水平方向自动循环(跑马灯效果) 效果图:  思路: 1.准备m张图片 1.使用Recyclerview实现,返回无数个(实际Interge.MAXVALUE)item,第n个item显示第n%m张图片 3.使用recyclerview.scrollBy  每个一段时间水平滚动一段距离 4.通过layoutManager.findFirstVisibleItemPosition()获取当前显示的第一个View是第几个item,上面

  • 详解Android使GridView横向水平滚动的实现方式

    Android为我们提供了竖直方向的滚动控件GridView,但如果我们想让它水平滚动起来,就需要自己实现了. 以下使用的测试数据datas集合都为List<ResolveInfo>类型,用来存储手机中的所有App public static List<ResolveInfo> getAppData(Context context) { PackageManager packageManager = context.getPackageManager(); Intent mainI

  • android listview 水平滚动和垂直滚动的小例子

    网上有很多解决 android listview 水平和垂直滚动的代码,我没有按照他们说的做(以前没搜到 O(∩_∩)O~) 我采用的是添加HorizontalScrollViewJava代码 复制代码 代码如下: < ScrollView android:id="@+id/ScrollView01" android:layout_height="300px" android:layout_x="16px" android:layout_y

  • Android实现Activity水平和垂直滚动条的方法

    本文实例讲述了Android实现Activity水平和垂直滚动条的方法.分享给大家供大家参考,具体如下: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="

  • Android中实现多行、水平滚动的分页的Gridview实例源码

    功能要求: (1)比如每页显示2X2,总共2XN,每个item显示图片+文字(点击有链接). 如果单行水平滚动,可以用Horizontalscrollview实现. 如果是多行水平滚动,则结合Gridview(一般是垂直滚动的)和Horizontalscrollview实现. (2)水平滚动翻页,下面有显示当前页的icon. 1.实现自定义的HorizontalScrollView(HorizontalScrollView.java): 因为要翻页时需要传当前页给调用者,所以fling函数中自己

  • android水平循环滚动控件使用详解

    本文实例为大家分享了android水平循环滚动控件的具体代码,供大家参考,具体内容如下 CycleScrollView.java package com.example.test; import android.content.Context; import android.graphics.Rect; import android.os.Handler; import android.util.AttributeSet; import android.view.GestureDetector;

  • android九宫格锁屏控件使用详解

    本文实例为大家分享了android九宫格锁屏控件的具体代码,供大家参考,具体内容如下 代码: public class LockView extends View {     //半径     private int radius;     //中心小圆半径     private int smallRadius;     //一行个数     private int column;     //选中颜色     private int selectColor;     //未选中颜色    

  • Android UI组件AppWidget控件入门详解

    Widget引入 我们可以把Widget理解成放置在桌面上的小组件(挂件),有了Widget,我们可以很方便地直接在桌面上进行各种操作,例如播放音乐. 当我们长按桌面时,可以看到Widget选项,如下图所示: 点击上图中箭头处的widgets图标,会出现如下界面:(都是widget) 长按上图中的任意一个widget,就可以将其放到桌面上. Widget的使用 Widget的实现思路  (1)在AndroidManifest中声明AppWidget:  (2)在xml目录中定义AppWidget

  • Android消息个数提醒控件使用详解

    前言 在QQ中有消息个数提醒的控件,虽然现在没用到,但是以后可能会用到,所以就实现它,也不难. 实现 效果图如下: 先贴源码了: public class TipNumberView extends TextView { private Paint mBgPaint ; PaintFlagsDrawFilter pfd; public TipNumberView(Context context, AttributeSet attrs) { super(context, attrs); //初始化

  • Android自定义弹窗提醒控件使用详解

    Android中原生的Dialog弹窗提醒控件样式单一,有时候并不能满足我们的项目需求,而且一个工程里面有时候会在多处都用到弹窗提醒的功能,代码会出现大量的冗余,工作之余,就自己实现了这么一个弹窗提醒控件.自定义控件继承自我们的Dialog,样式自定义,弹窗中的文字可通过数组参数初始化,Item个数实现了动态添加,和数组长度一致.对话框底端可展示一个Item(如:确定)或两个Item(如:确定   取消),通过参数设置.废话不多说,直接上代码: 1.自定义对话框的背景样式,在res/values

  • Android仿京东淘宝自动无限循环轮播控件思路详解

    在App的开发中,很多的时候都需要实现类似京东淘宝一样的自动无限轮播的广告栏,所以就自己写了一个,下面是我自定义控件的思路和过程. 一.自定义控件属性 新建自定义控件SliderLayout继承于RelativeLayout,首先要考虑的就是自定义的控件需要扩展那些属性,把这些属性列出来.在这里是要实现类似于京东淘宝的无限轮播广告栏,那么首先想到的就是轮播的时长.轮播指示器的样式等等.我在这里列举了一些并且结合到了代码中. 1.扩展属性 (1)是否开启自动轮播的功能. (2)指示器的图形样式,一

  • Android 仿淘宝、京东商品详情页向上拖动查看图文详情控件DEMO详解

    一.淘宝商品详情页效果 我们的效果 二.实现思路 使用两个scrollView,两个scrollView 竖直排列,通过自定义viewGroup来控制两个scrollView的竖直排列,以及滑动事件的处理.如下图 三.具体实现 1.继承viewGroup自定义布局View 重写onMeasure()和onLayout方法,在onLayout方法中完成对两个子ScrollView的竖直排列布局,代码如下: 布局文件: <RelativeLayout xmlns:android="http:/

  • android之SeekBar控件用法详解

    MainActivity.java package com.example.mars_2400_seekbar; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.app.Activity; import android.os.Bundle; import a

  • android之RatingBar控件用法详解

    MainActivity.java package com.example.mars_2500_ratingbar; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.app.Activity; import android.os.Bundle; import

  • Android 带清除功能的输入框控件实例详解

    Android 带清除功能的输入框控件实例详解 今天,看到一个很好的自定义输入框控件,于是记录一下. 效果很好: 一,自定义一个类,名为ClearEditText package com.example.clearedittext; import android.content.Context; import android.graphics.drawable.Drawable; import android.text.Editable; import android.text.TextWatc

随机推荐