In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to package horizontal scrolling View, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
First of all, the foundation is just an introduction:
1.Scroller
This class mainly supports view control sliding. In fact, this class is hidden by default in many slidable controls in android. And the class does not actually move the view, when its startScroll () method is actually called just to start the animation before the parent class calls the computeScroll () method, that is, the class is actually equivalent to a proxy, just to add some animation to the later view movement. So calling startScroll () alone without overriding the computeScroll () method will not see any effect. The two must be used together in order to have the animation effect when moving.
The Scroller.computeScrollOffset () method determines whether the mobile animation of scroller is complete. When you call the startScroll () method, this method always returns true, and this method returns false if you move the view in other ways, such as scrollTo () or scrollBy.
Now let's talk about the meaning of the four parameters of the startScroll (int startX, int startY, int dx, int dy, int duration) method:
StartX represents the x coordinate value of the current view
StartY represents the y coordinate value of the current view
Dx represents the distance that moves horizontally based on the x-coordinates of the current view
Dy represents the distance moved longitudinally based on the y coordinates of the current view
Duration indicates how long it takes for the view movement to finish, that is, the duration of the animation (in milliseconds)
2.ViewGroup
This is a special View, which inherits from Android.view.View, and its function is to load and manage the next layer of View objects or ViewGroup objects, that is, it is a container that holds other elements.
Let's analyze the methods we want to use in these five classes. First, let's take a look at the ViewGroup class. Because our custom control inherits from this class, we will override the five methods in this class as follows:
1.onLayout (boolean changed, int l, int t, int r, int b)
This method is called after the onMeasure () method is executed, and the function is that the parent class assigns the actual width and height to the child class on the screen. The four parameters inside indicate whether the layout has changed and the margin between the upper left and lower right of the layout.
2.onMeasure (int widthMeasureSpec, int heightMeasureSpec)
This method is called when the parent element of the control is about to place its child control. Then pass in two parameters-- widthMeasureSpec and heightMeasureSpec. They indicate the space available to the control and the metadata about the description of the space. A better way than returning a result is that you pass the height and width of the View to the setMeasuredDimension method. Before using the widthMeasureSpec and heightMeasureSpec parameters, the first thing to do is to use the static methods getMode and getSize of the MeasureSpec class to translate. An MeasureSpec contains a size and pattern.
There are three possible modes:
UNSPECIFIED: the parent layout does not impose any restrictions on the child layout, which can be of any size.
EXACTLY: the parent layout determines the exact size of the child layout. No matter how big the sublayout is, it must be limited to this limit. (EXACTLY mode when the layout is defined as a fixed pixel or fill_parent)
AT_MOST: the sub-layout can choose any size according to its own size. (AT_MOST mode when the layout is defined as wrap_content)
3.computeScroll ()
This method is mainly called when the parent class requires its subclasses to scroll. In this method, we can implement the scrolling operation of view, where scrolling is not the scrolling of the view but the scrolling of the layout. When the startScroll () method of scroller is called, the parent class calls this method to implement the scrolling view operation.
4.onTouchEvent (MotionEvent event)
Handles gesture events passed to view. Gesture event types include events such as ACTION_DOWN,ACTION_MOVE,ACTION_UP,ACTION_CANCEL. The default return value of onTouch in Layout is false, and the default return value of onTouch in View is true. When we click on the screen, we first call the ACTION_DOWN event. When the return value in onTouch is true, onTouch will continue to call the ACTION_UP event. If the return value in onTouch is false, then onTouch will only call ACTION_DOWN instead of ACTION_UP.
5.onInterceptTouchEvent (MotionEvent ev)
Used to intercept gesture events, this method is called first for each gesture event. The default return value of onInterceptTouchEvent in Layout is false, so that the touch event is passed to the View control.
6.Invalidate () and PostInvalidate (), both of which have the same effect, call the UI thread to redraw the interface, that is, to refresh the interface. So why two methods? this is because android is a multithreaded application, and everyone should know that interface controls cannot be operated directly in non-UI threads, so the second method helps you to refresh the interface in child threads. The first method is to refresh the interface in the UI thread.
Both 7.getX () and getRawX () get the coordinates of the current point on the screen, getX () gets the coordinates of the current point relative to the upper-left corner of the current view, and getRawX () gets the coordinates of the current point relative to the upper-left corner of the mobile phone screen.
Code that encapsulates View
Package com.xc.view;import java.util.ArrayList;import java.util.List;import com.xc.view.XCSlideListView.XCSlideView;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.View.OnClickListener;import android.widget.BaseAdapter;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {private XCSlideListView list; private List items / * Called when the activity is first created. * / @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); initData (); initViews (); initActions ();} private void initData () {items = new ArrayList (); for (int I = 0; I
< 20; i++) { items.add("SlideView" + i); } } private void initViews() { setContentView(R.layout.main); list = (XCSlideListView) findViewById(R.id.list); list.setAdapter(new MyAdapter2(this, items)); } private void initActions() { } public class MyAdapter2 extends BaseAdapter { private Context context; private List items; private LayoutInflater inflater; public MyAdapter2(Context context, List items) { this.context = context; this.items = items; this.inflater = LayoutInflater.from(context); } @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final String item = items.get(position); MyAdapterHolder holder; XCSlideListView.XCSlideView slideView = (XCSlideView) convertView; if (slideView == null) { slideView = list.new XCSlideView(context); slideView.addShowView(View.inflate(context, R.layout.slideview_show, null)); slideView.addPopupView(View.inflate(context, R.layout.slideview_popup, null), 120); holder = new MyAdapterHolder(); holder.setText((TextView) slideView.findViewById(R.id.text)); holder.setDelete((TextView) slideView.findViewById(R.id.delete)); slideView.setTag(holder); } else { holder = (MyAdapterHolder) slideView.getTag(); } holder.getText().setText(item); holder.getDelete().setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, "delete!-->"+ position, Toast.LENGTH_LONG) .show ();}}); return slideView;} package com.xc.view;import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.util.TypedValue;import android.view.MotionEvent;import android.view.View;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.Scroller Public class XCSlideListView extends ListView {private final String TAG = "XCSlideListView"; private XCSlideView currentView; private Context context; public XCSlideListView (Context context) {this (context, null);} public XCSlideListView (Context context, AttributeSet attrs) {super (context, attrs); this.context = context;} @ Override public boolean onTouchEvent (MotionEvent ev) {int x = (int) ev.getX () Int y = (int) ev.getY (); int position = pointToPosition (x, y); if (position! = INVALID_POSITION & & ev.getAction () = = MotionEvent.ACTION_DOWN) {int firstVisiblePosition = getFirstVisiblePosition (); XCSlideView slideView = (XCSlideView) getChildAt (position-firstVisiblePosition) If (currentView! = null & & currentView! = slideView) {Log.e (TAG, "currentView! = slideView"); currentView.shrink (true);} currentView = slideView;} if (currentView! = null) {currentView.onRequireTouchEvent (ev);} return super.onTouchEvent (ev) } public class XCSlideView extends LinearLayout {private Scroller mScroller; private int slideWidth = 0; private int mLastX = 0; private int mLastY = 0; private boolean isRight = false; private static final int TAN = 2; public XCSlideView (Context context) {this (context, null);} public XCSlideView (Context context, AttributeSet attrs) {super (context, attrs) SetOrientation (LinearLayout.HORIZONTAL); mScroller = new Scroller (context);} public void addShowView (View view) {addView (view, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);} public void addPopupView (View view, int width) {slideWidth = Math.round (TypedValue.applyDimension (TypedValue.COMPLEX_UNIT_DIP, width, getResources (). GetDisplayMetrics () AddView (view, slideWidth, LayoutParams.FILL_PARENT);} @ Override public Object getTag () {shrink (false); return super.getTag ();} public void onRequireTouchEvent (MotionEvent event) {if (slideWidth = = 0) return; int x = (int) event.getX () Int y = (int) event.getY (); int scrollX = getScrollX (); switch (event.getAction ()) {case MotionEvent.ACTION_DOWN: {if (! mScroller.isFinished ()) {mScroller.abortAnimation ();} break } case MotionEvent.ACTION_MOVE: {int deltaX = x-mLastX; int deltaY = y-mLastY; if (deltaX > 0) {isRight = true;} else {isRight = false;} if (Math.abs (deltaX)
< Math.abs(deltaY) * TAN) { break; } int newScrollX = scrollX - deltaX; if (deltaX != 0) { if (newScrollX < 0) { newScrollX = 0; } else if (newScrollX >SlideWidth) {newScrollX = slideWidth;} this.scrollTo (newScrollX, 0);} break;} case MotionEvent.ACTION_UP: {int newScrollX = 0; if (scrollX-(isRight?) SlideWidth * 0.75: slideWidth * 0.25) > 0) {newScrollX = slideWidth;} this.smoothScrollTo (newScrollX, 0); break;} default: break;} mLastX = x; mLastY = y } public void shrink (boolean haveAnim) {if (getScrollX ()! = 0) {if (haveAnim) {this.smoothScrollTo (0,0);} else {this.scrollTo (0,0) } / / scroll slowly to the specified location private void smoothScrollTo (int destX, int destY) {int scrollX = getScrollX (); int delta = destX-scrollX / / Last parameter: duration of the animation Math.abs: absolute value mScroller.startScroll (scrollX, 0, delta, 0, Math.abs (delta) * 3); invalidate ();} @ Override public void computeScroll () {Log.e ("computeScroll", "x" + mScroller.getCurrX () + "--YLV -" + mScroller.getCurrY ()) / / is to determine whether the mobile animation of scroller is complete / / this method always returns true when you call the startScroll () method, and this method returns false if you move the view in other ways, such as scrollTo () or / / scrollBy. If (mScroller.computeScrollOffset ()) {scrollTo (mScroller.getCurrX (), mScroller.getCurrY ()); postInvalidate ();}
Effect picture:
The above content is how to package horizontal scrolling View, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.