您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Android的ListView怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Android的ListView怎么使用”吧!
在Android開發(fā) 中,經(jīng)常會要用到水平水平ListView(HorizontalListView),但是,Android 官方并沒有提供這樣一個控件, 所以在這里我給大家分享一下我在項(xiàng)目中用到的一個水平水平ListView,非常好用, 使用過程 中與 ListView 是一樣的, 實(shí)例化組件, 設(shè)置數(shù)據(jù),設(shè)置Adaper.
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是怎么實(shí)現(xiàn)的,
public class ListView extends AbsListView
public abstract class AbsListView extends AdapterView<ListAdapter> implements TextWatcher, ViewTreeObserver.OnGlobalLayoutListener, Filter.FilterListener, ViewTreeObserver.OnTouchModeChangeListener, RemoteViewsAdapter.RemoteAdapterConnectionCallback {
大家 對比去看看官方文檔實(shí)現(xiàn) ListView的類就知道了.這里我就不再多說了
大家 注意 HorizontalListView只是 一個抽象類, 所以我們不能直接用它,需要用一個類來實(shí)現(xiàn)里面的方法
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); } } ..... .... ....
當(dāng)然了 , 這個類很復(fù)雜,就不把代碼貼出來了, 在下面我們把這個demo代碼上傳的, 希望大家可以去下載,研究,然后把代碼整合到自己的項(xiàng)目中使用.
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的點(diǎn)擊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是一樣的用法
我再把a(bǔ)ctivity_main.xml文件代碼貼出來
<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>
到此,相信大家對“Android的ListView怎么使用”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。