In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to use Android's ListView". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Android's ListView.
In Android development, horizontal ListView (HorizontalListView) is often used, but Android officially does not provide such a control, so here I would like to share with you a horizontal ListView used in the project, which is very easy to use, the same as ListView in the process of use, instantiating components, setting data, setting 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 {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);}}
Why inherit AdapterView? you can take a look at how the ListView provided by Android is implemented.
Public class ListView extends AbsListViewpublic abstract class AbsListView extends AdapterView implements TextWatcher, ViewTreeObserver.OnGlobalLayoutListener, Filter.FilterListener, ViewTreeObserver.OnTouchModeChangeListener, RemoteViewsAdapter.RemoteAdapterConnectionCallback {
Let's take a look at the classes that implement ListView in the official documentation. I won't say any more here.
Notice that HorizontalListView is just an abstract class, so we can't use it directly, we need a class to implement the methods in it.
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. *
* 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 mRecycleBin; private List mChildWidths = new ArrayList (); private List mChildHeights = new ArrayList (); 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. * 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);}}. .... ....
Of course, this class is very complex, so we won't post the code. We upload the demo code below. I hope you can download, study, and integrate the code into your own project.
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) {/ / horizontal ListView click item event 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;}}
Just use this control in Activity, isn't it very simple? it's the same as Listview.
I'll post the activity_main.xml file code again.
At this point, I believe you have a deeper understanding of "how to use Android's ListView". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.