在Android開發 中,常常會要用到水平水平ListView(HorizontalListView),可是,Android 官方並無提供這樣一個控件, 因此在這裏我給你們分享一下我在項目中用到的一個水平水平ListView,很是好用, 使用過程 中與 ListView 是同樣的, 實例化組件, 設置數據,設置Adaper.java
package com.example.horizontallistview.hl.widget; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.AdapterView; import android.widget.ListAdapter; public abstract class HorizontalListView extends AdapterView<ListAdapter> { public HorizontalListView(Context context) { super(context); } public HorizontalListView(Context context, AttributeSet attrs) { super(context, attrs); } public HorizontalListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public abstract int getScreenPositionForView(View view); /** * Interface definition for a callback to be invoked when an item in this * view has been clicked and held. */ public interface OnItemDragListener { /** * Callback method to be invoked when an item in this view has been * dragged outside the vertical tolerance area. * * Implementers can call getItemAtPosition(position) if they need to * access the data associated with the selected item. * * @param parent * The AbsListView where the click happened * @param view * The view within the AbsListView that was clicked * @param position * The position of the view in the list * @param id * The row id of the item that was clicked * * @return true if the callback consumed the long click, false otherwise */ boolean onItemStartDrag(AdapterView<?> parent, View view, int position, long id); } public interface OnLayoutChangeListener { void onLayoutChange(boolean changed, int left, int top, int right, int bottom); } public interface OnScrollFinishedListener { /** * Callback method to be invoked when the scroll has completed. * * @param currentX * The current scroll position of the view */ void onScrollFinished(int currentX); } }
爲何要繼承AdapterView, 你們能夠去看看Android提供的ListView是怎麼實現的,android
public class ListView extends AbsListView
public abstract class AbsListView extends AdapterView<ListAdapter> implements TextWatcher, ViewTreeObserver.OnGlobalLayoutListener, Filter.FilterListener, ViewTreeObserver.OnTouchModeChangeListener, RemoteViewsAdapter.RemoteAdapterConnectionCallback {
你們 對比去看看官方文檔實現 ListView的類就知道了.這裏我就再也不多說了canvas
你們 注意 HorizontalListView只是 一個抽象類, 因此咱們不能直接用它,須要用一個類來實現裏面的方法app
public class HorizontalVariableListView extends HorizontalListView implements OnGestureListener, FlingRunnableView { public enum SelectionMode { Single, Multiple }; public interface OnItemClickedListener { /** * Callback method to be invoked when an item in this AdapterView has * been clicked. * <p> * Implementers can call getItemAtPosition(position) if they need to * access the data associated with the selected item. * * @param parent * The AdapterView where the click happened. * @param view * The view within the AdapterView that was clicked (this * will be a view provided by the adapter) * @param position * The position of the view in the adapter. * @param id * The row id of the item that was clicked. * @return if the implementation return false, then the selection will * not be updated */ boolean onItemClick(AdapterView<?> parent, View view, int position, long id); } public static final int OVER_SCROLL_ALWAYS = 0; public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1; public static final int OVER_SCROLL_NEVER = 2; protected static final String LOG_TAG = "horizontal-variable-list"; private static final float WIDTH_THRESHOLD = 1.1f; protected int mAlignMode = Gravity.CENTER; protected SparseBooleanArray mSelectedPositions = new SparseBooleanArray(); protected int mHeight = 0; protected int mPaddingTop = 0; protected int mPaddingBottom = 0; protected ListAdapter mAdapter; private int mAdapterItemCount; private int mLeftViewIndex = -1; private int mRightViewIndex = 0; private GestureDetector mGesture; private List<Queue<View>> mRecycleBin; private List<Integer> mChildWidths = new ArrayList<Integer>(); private List<Integer> mChildHeights = new ArrayList<Integer>(); private boolean mDataChanged = false; private IFlingRunnable mFlingRunnable; private boolean mForceLayout; private int mDragTolerance = 0; private boolean mDragScrollEnabled; protected EdgeGlow mEdgeGlowLeft, mEdgeGlowRight; private int mOverScrollMode = OVER_SCROLL_NEVER; private Matrix mEdgeMatrix = new Matrix(); private ScrollNotifier mScrollNotifier; private SelectionMode mChoiceMode = SelectionMode.Single; private OnItemSelectedListener mOnItemSelected; private OnItemClickedListener mOnItemClicked; private OnItemDragListener mItemDragListener; private OnScrollChangedListener mScrollListener; private OnScrollFinishedListener mScrollFinishedListener; private OnLayoutChangeListener mLayoutChangeListener; public void setOnItemDragListener(OnItemDragListener listener) { mItemDragListener = listener; } public void setOnScrollListener(OnScrollChangedListener listener) { mScrollListener = listener; } public void setOnLayoutChangeListener(OnLayoutChangeListener listener) { mLayoutChangeListener = listener; } public void setOnScrollFinishedListener(OnScrollFinishedListener listener) { mScrollFinishedListener = listener; } public OnItemDragListener getOnItemDragListener() { return mItemDragListener; } /** * Controls how selection is managed within the list.<br /> * Single or multiple selections are supported * * @see SelectionMode * @param mode * the selection mode */ public void setSelectionMode(SelectionMode mode) { mChoiceMode = mode; } /** * Returns the current selection mode * * @see SelectionMode * @return */ public SelectionMode getChoiceMode() { return mChoiceMode; } /** * Instantiates a new horizontial fixed list view. * * @param context * the context * @param attrs * the attrs */ public HorizontalVariableListView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public HorizontalVariableListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(); } private synchronized void initView() { if (Build.VERSION.SDK_INT > 8) { try { mFlingRunnable = (IFlingRunnable) ReflectionUtils .newInstance( "com.iotdc.android.app.shopping.HorizontalVariableListView.widget.Fling9Runnable", new Class<?>[] { FlingRunnableView.class, int.class }, this, mAnimationDuration); } catch (ReflectionException e) { mFlingRunnable = new Fling8Runnable(this, mAnimationDuration); } } else { mFlingRunnable = new Fling8Runnable(this, mAnimationDuration); } mLeftViewIndex = -1; mRightViewIndex = 0; mMaxX = Integer.MAX_VALUE; mMinX = 0; mRightEdge = 0; mLeftEdge = 0; mGesture = new GestureDetector(getContext(), mGestureListener); mGesture.setIsLongpressEnabled(false); setFocusable(true); setFocusableInTouchMode(true); ViewConfiguration configuration = ViewConfiguration.get(getContext()); mTouchSlop = configuration.getScaledTouchSlop(); mDragTolerance = mTouchSlop; mOverscrollDistance = 10; mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); mMinimumVelocity = configuration.getScaledMinimumFlingVelocity(); } public void setOverScrollMode(int mode) { mOverScrollMode = mode; if (mode != OVER_SCROLL_NEVER) { if (mEdgeGlowLeft == null) { Drawable glow = getContext().getResources().getDrawable( R.drawable.overscroll_glow); Drawable edge = getContext().getResources().getDrawable( R.drawable.overscroll_edge); mEdgeGlowLeft = new EdgeGlow(edge, glow); mEdgeGlowRight = new EdgeGlow(edge, glow); mEdgeGlowLeft.setColorFilter(0xFF33b5e5, Mode.MULTIPLY); } } else { mEdgeGlowLeft = mEdgeGlowRight = null; } } public void setEdgeHeight(int value) { mEdgesHeight = value; } public void setEdgeGravityY(int value) { mEdgesGravityY = value; } @Override public void trackMotionScroll(int newX) { scrollTo(newX, 0); mCurrentX = getScrollX(); removeNonVisibleItems(mCurrentX); fillList(mCurrentX); invalidate(); } @Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); if (getChildCount() > 0) { drawEdges(canvas); } } ..... .... ....
固然了 , 這個類很複雜,就不把代碼貼出來了, 在下面咱們把這個demo代碼上傳的, 但願你們能夠去下載,研究,而後把代碼整合到本身的項目中使用.ide
package com.example.horizontallistview; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.Toast; import com.example.horizontallistview.hl.widget.HorizontalVariableListView; import com.example.horizontallistview.hl.widget.HorizontalVariableListView.OnItemClickedListener; public class MainActivity extends Activity { private HorizontalVariableListView lv; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (HorizontalVariableListView) findViewById(R.id.lv_horizontal); String[] items = new String[20]; adapter = new MyAdapter(this, items); lv.setAdapter(adapter); lv.setSelectionMode(HorizontalVariableListView.SelectionMode.Single); lv.setOnItemClickedListener(new OnItemClickedListener() { @Override public boolean onItemClick(AdapterView<?> parent, View view, int position, long id) { // 水平ListView的點擊item事件 Toast.makeText(MainActivity.this, position + "", 1).show(); return false; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
在Activity中就是這麼使用這個控件就好了, 是否是很簡單,與Listview是同樣的用法ui
我再把activity_main.xml文件代碼貼出來this
<LinearLayout 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" android:orientation="vertical" > <com.example.horizontallistview.hl.widget.HorizontalVariableListView android:id="@+id/lv_horizontal" android:layout_width="match_parent" android:layout_height="120dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:gravity="bottom|center_vertical" android:paddingBottom="0dp" android:paddingTop="0dp" /> </LinearLayout>
下載demo鏈接:點擊下載Android水平水平ListViewspa