Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Comparative Verification Analysis of Android Touch screen textview and listview

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces Android touch screen textview and listview comparative verification analysis, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Touchscreen actions have become the norm for mainstream mobile phones, but have you ever thought about how to develop gesture operations for touchscreen phones? This article will introduce to you the textview and listview comparison verification of Android touch screen mobile phone development.

View can receive touch screen events through onTouchEvent

We can set event listeners through View.setOnTouchListener ()

Or override onTouchEvent () to intercept these events

The gesture can be determined by judging the trajectory and speed of the touch point in the interception function.

The Android system provides GestureDetector to facilitate gesture judgment, that is, every time touchevent calls GestureDetector.onTouchEvent () as a parameter in the interceptor function, and when a gesture is recognized, it will notify the caller.

In order to notify the caller, GestureDetector requires that an object that implements the OnGestureListener interface be passed in during construction, through which various gesture notifications can be received.

First, experiment with textview:

Place two Textview with one ViewFlipper and switch as the fingers move left and right on the screen.

Because textview itself does not handle touchevent, but will continue to upload it, layout_width and layout_height have no effect on textview layout.

If the touch event occurs on textview, it will continue to be uploaded to viewflipper;. If it happens on viewflipper, it will be handled directly.

So we should intercept the touchEvent that occurs on the ViewFlipper and deal with it.

The following code is used to complete the interface layout and event interception function settings in onCreate:

Java code

/ / create a ViewFlipper mVf = new ViewFlipper (this); / / ViewFlipper open long-click support. If you don't open it and you can't get a long click, then gesture judgment can't be done. MVf.setLongClickable (true); / / intercept the touch event of ViewFlipper and use GestureDetector.onTouchEvent to handle mVf.setOnTouchListener (new OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {return mVfDetector.onTouchEvent (event);}}) / / add a textview. Textview cannot setLongClickable (true), / / if set, touch events that occur on textview cannot be passed to viewflipper, / / cannot be processed by gesture recognition objects. TextView tv = new TextView (this); tv.setText ("TextView 1"); tv.setBackgroundColor (0xffffffff); / / set a white background to make it easy to see the textview area mVf.addView (tv,new LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); / / add another textview TextView tv2 = new TextView (this) Tv2.setText ("TextView 2"); tv2.setBackgroundColor (0xffffffff); mVf.addView (tv2,new LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); / / display setContentView (mVf) with viewFlipper as the main view of Activity

The GestureDetector object, which is a class member object, is used in the listener and is created with the following code:

Java code

Private GestureDetector mVfDetector = new GestureDetector (new OnGestureListener () {/ / fingers moving less than this value on the screen will not be considered as gestures private static final int SWIPE_MIN_DISTANCE = 120; / / fingers moving less than this value on the screen will not be considered gestures private static final int SWIPE_THRESHOLD_VELOCITY = 200 / / gesture recognition function. When this function is called back by the system, it means that the system thinks that a gesture event has occurred. / / We can make a further decision. @ Override public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {/ / if the first coordinate point is greater than the second coordinate point, the sliding distance is to the left and the sliding speed is an additional judgment, which can be modified according to the actual situation. If (e1.getX ()-e2.getX () > SWIPE_MIN_DISTANCE & & Math.abs (velocityX) > SWIPE_THRESHOLD_VELOCITY) {/ / left Log.i ("GestureDemo", "ViewFlipper left"); mVf.showNext (); return true } else if (e2.getX ()-e1.getX () > SWIPE_MIN_DISTANCE & & Math.abs (velocityX) > SWIPE_THRESHOLD_VELOCITY) {/ / right Log.i ("GestureDemo", "ViewFlipper right"); mVf.showPrevious (); return true } return false;}.});}

Let's change the addition of the second textview in the previous article to add Listview, as follows:

Java code

ListView lv = new ListView (this); lv.setBackgroundColor (0xff808080); final String [] items = {"one", "two", "three"}; lv.setAdapter (new ArrayAdapter (this, android.R.layout.simple_list_item_1, items)); mVf.addView (lv,new LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))

Execute ap, slide the screen to switch to the second screen, and you can see that the second screen has been replaced by a ListView, and the entire screen has not been filled. If you swipe left in the bottom non-ListView area, you can still switch to the * * screen, but sliding in the Listview area has no effect, because the touch event is handled by Listview, and the ViewFlipper cannot make gesture judgment if it cannot receive the touch event.

We also add a touch event listener to ListView with the following code:

Java code

Lv.setOnTouchListener (new OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {return mVfDetector.onTouchEvent (event);}})

At this point, you can also switch screens by swiping on the ListView.

Let's add the following code to Listview to handle the click event:

Java code

Lv.setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView > arg0, View arg1, int arg2, long arg3) {new AlertDialog.Builder (MainActivity.this) .setMessage (items [arg2]) .create () .show ();}})

When you click on the entry for Listview, a pop-up window shows which item you clicked, and the code at this time can be found in attachment 1.

Up to this point, it seems that the left and right sliding operation supported by Listview is complete, but there are still two problems:

First of all, when sliding, Listview sometimes has entries highlighted, which is not a serious problem.

Third, the ContextMenu will be activated every time it slips, and we can modify the code that creates the ListView through code verification:

Java code

/ make Listview longer final String [] items = {"one", "two", "three", "four", "five", "six", "sevent", "eight", "nine"}; registerForContextMenu (lv)

In addition, the code for Activity has been added:

Java code

@ Override public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {menu.add ("Menu 1"); menu.add ("Menu 2"); menu.add ("Menu 3"); super.onCreateContextMenu (menu, v, menuInfo);}

When we grow on an item in ListView, the menu pops up on time, but when we swipe, the menu still pops up even if the screen is switched to the screen.

In order to solve the above problems, I have tried the following methods:

1. True is always returned in the touch event listener function of ListView, and all events are eaten. As a result, the ListView cannot be clicked accordingly and the ListView cannot slide up and down.

two。 Sending a MotionEvent.ACTION_CANCEL event to ListView in the fling function of GestureDetector always has a null pointer exception. It is suspected that it is because the touch event has not been handled by ListView and its internal member state is abnormal, so I inherit and implement a ListView from ListView and call super.onTouchEvent in onTouchEvent, but the MotionEvent.ACTION_CANCEL event is still a null pointer exception and fails again.

The onDown function of 3.GestureDetector returns true and eats the down event. There is no highlight when clicking, and contextmenu is not trigger when switching, but the contextmenu cannot be popped up even if the button is pressed for a long time.

In order to pop up contextMenu, call ListView.showContextMenuForChild () in the onLongPress function of GestureDetector to pop up the menu.

Then GestureDetector is not common with ViewFlipper.

So I re-inherited from ListView to implement a class that binds itself to a GestureDetector:

Java code

@ Override public boolean onDown (MotionEvent e) {return true; / / eat Down event} @ Override public void onLongPress (MotionEvent e) {System.out.println ("Listview long press"); int position = pointToPosition ((int) e.getX (), (int) e.getY ()) If (position! = ListView.INVALID_POSITION) {View child = getChildAt (position-getFirstVisiblePosition ()); if (child! = null) GestureListView.this.showContextMenuForChild (child);}}

In addition, in order to show which item is activated during contextmenu, add a function to Activity:

Java code

@ Override public boolean onContextItemSelected (MenuItem item) {AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo (); System.out.println ("View" + info.position + "context menu activited."); return super.onContextItemSelected (item);}

You can see the printout from LogCat.

At this time, the ListView can respond to gestures, clicks, pop-up menus, the basic functions have been met, and then fine-tune the ondown function, which can be highlighted when clicked.

On the Android touch screen textview and listview comparative verification analysis is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report