Event mechanism (classic cases + otaku benefits) and Mechanism classic cases

Source: Internet
Author: User

Event mechanism (classic cases + otaku benefits) and Mechanism classic cases

------------------ As above ----------------

The effect is: the screen can be dragged up or down independently in the left and right, on the premise that at the bottom of the half of the height, drag up in the upper part of the screen, is the overall picture drag up

Activity_main.xml

<com.atguigu.pinterestlistview.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/mll"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <ListView        android:id="@+id/lv2"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="wrap_content"        android:scrollbars="none" />    <ListView        android:id="@+id/lv1"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="wrap_content"        android:scrollbars="none" />    <ListView        android:id="@+id/lv3"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="wrap_content"        android:scrollbars="none" /></com.atguigu.pinterestlistview.MyLinearLayout>

Lv_item.xml

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/iv"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:adjustViewBounds="true"    android:src="@drawable/default1" />

MainActivity. java

Package com. atguigu. pinterestlistview; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. listView; public class MainActivity extends Activity {/** defines three listview */private ListView lv1; private ListView lv2; private ListView lv3; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain listviewlv1 = (ListView) findViewById (R. id. lv1); lv2 = (ListView) findViewById (R. id. lv2); lv3 = (ListView) findViewById (R. id. lv3); // set the adapter try {lv1.setAdapter (new MyAdapter1 (); lv2.setAdapter (new MyAdapter1 (); lv3.setAdapter (new MyAdapter1 ();} catch (Exception e) {e. printStackTrace () ;}/// prepare the required image resource private int ids [] = new int [] {R. drawable. default1, R. drawable. girl1, R. drawable. girl2, R. drawable. girl3, R. drawable. girl4}; class MyAdapter1 extends BaseAdapter {@ Overridepublic int getCount () {return 3000 ;}@ Overridepublic Object getItem (int position) {return position ;}@ Overridepublic long getItemId (int position) {return 0 ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {ImageView iv = (ImageView) View. inflate (getApplicationContext (), R. layout. lv_item, null); // 0 <= Math. random () * 5 <5int resId = (int) (Math. random () * 5); iv. setImageResource (ids [resId]); return iv ;}}}

MyLinearLayout. java

Package com. atguigu. pinterestlistview; import android. content. context; import android. util. attributeSet; import android. view. motionEvent; import android. view. view; import android. widget. linearLayout; public class MyLinearLayout extends LinearLayout {/*** Step 1: Execute the constructor */public MyLinearLayout (Context context, AttributeSet attrs) {super (context, attrs );} /*** onInterceptTouchEvent (); intercept touch Event 1. if true is returned, the onTouchEvent (); * 2 of the current View is triggered. if false is returned, the event will be sent to the child */@ Overridepublic boolean onInterceptTouchEvent (MotionEvent ev) {// cause the ontouch event to return true;} @ Overridepublic boolean onTouchEvent (MotionEvent event) {// The screen's 1/3 int width = getWidth ()/getChildCount (); int height = getHeight (); int count = getChildCount (); // obtain the x coordinate value float eventX = event. getX (); // slide the left-side listViewif (eventX <width) {// set the valid range event. setLocation (width/2, event. getY (); // distribute the event to listviewgetChildAt (0 ). dispatchTouchEvent (event); // returns true, indicating that the list Processing event returns true;} else if (eventX> width & eventX <2 * width) {// slide the listView in the middle. // obtain the y value float eventY = event in the middle listview. getY (); // if it is on top of screen 1/2 (upper part of screen) if (eventY 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.