Android UI Design: Imitation iOS, imitation QQ implementation ListView Slide Show button

Source: Internet
Author: User
Tags gety

The usual first look:

This code is very common, the online Daniel has done several times, but in a learning attitude, to study the code written by others. There are several implementations of this discovery. One of the most popular is the use of scroller to achieve sliding , and then the implementation of the button there are three ways.
1. One is to use Java classes to inherit a viewgroup to dynamically generate ListViewItem. This flexibility is high, but the skill is also high.
2. One is that the entire ListViewItem is written in XML and then inflate in. This write the advantage is can be reused, and only need adapter rewrite view, but the button and content is fixed, not easy to implement QQ friends kind, sometimes is deleted, sometimes is pinned, sometimes mark what, a variety of button combination
3. For this I tried to write the basic ListViewItem and button XML separately, writing 2 layouts, and then composing them dynamically with a linerlayout. This allows you to write multiple sets of button layouts when you expand, and then dynamically judge the composition, which makes it easier to implement multiple kinds of button methods.
4. Complete the project in my GitHub:
Stamp Stamp: Https://github.com/younfor/DragListView
Below is the core source code:

 PackageCom.draglistview;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.util.TypedValue;ImportAndroid.view.MotionEvent;ImportAndroid.widget.ListView;/** * * @author https://github.com/younfor * * * * Public  class dragdellistview extends ListView {    //Whether you can drag    Private Booleanmoveable=false;//Whether all closed    Private BooleanClosed=true;Private floatMdownx,mdowny;Private intmtouchposition,oldposition=-1;PrivateDragdelitem Mtouchview,oldview;PrivateContext context; Public Dragdellistview(Context context) {Super(context);    Init (context); } Public Dragdellistview(context context, AttributeSet attrs,intDefstyle) {Super(Context, attrs, Defstyle);    Init (context); } Public Dragdellistview(context context, AttributeSet attrs) {Super(context, attrs);    Init (context); }Private void Init(Context context) { This. Context=context; }@Override     Public Boolean ontouchevent(Motionevent ev) {Switch(Ev.getaction ()) { CaseMotionevent.action_down://Get position of clicksMtouchposition = Pointtoposition ((int) Ev.getx (), (int) ev.gety ());//Get the click View (visible range inside)mtouchview= (Dragdelitem) Getchildat (Mtouchposition-getfirstvisibleposition ());//Get places to clickMdownx = Ev.getx (); Mdowny=ev.gety ();//Click on the same or all initial            if(Oldposition==mtouchposition | | closed) {//Can be movedmoveable=true; Mtouchview.mdownx = (int) Mdownx; }Else{moveable=false;if(oldview!=NULL)                {//Old homingOldview.smoothclosemenu ();            }} oldposition=mtouchposition; Oldview=mtouchview; Break; CaseMotionevent.action_move://To determine whether to slide            if(Math.Abs (Mdownx-ev.getx ()) < Math.Abs (Mdowny-ev.gety ()) * DP2PX (2)) { Break; }if(moveable) {intDis = (int) (Mtouchview.mdownx-ev.getx ());//This direction will change                if(Mtouchview.state==mtouchview.state_open) dis+=mtouchview.mmenuview.getwidth ();                Mtouchview.swipe (DIS);            Ev.setaction (Motionevent.action_cancel); } Break; CaseMOTIONEVENT.ACTION_UP:if(moveable) {if((Mtouchview.mdownx-ev.getx ()) > (MTouchView.mMenuView.getWidth ()/2)) {//OpenMtouchview.smoothopenmenu (); Closed=false; }Else{//CloseMtouchview.smoothclosemenu (); Closed=true;            } ev.setaction (Motionevent.action_cancel); } Break; }return Super. ontouchevent (EV); }//Convert DP to PX    Private int dp2px(intDP) {return(int) Typedvalue.applydimension (Typedvalue.complex_unit_dip, DP, GetContext (). Getresources (). Getdisplaymetrics (    )); }}

Package Com.draglistview;

Import Android.content.Context;
Import Android.support.v4.view.GestureDetectorCompat;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import Android.util.TypedValue;
Import Android.view.GestureDetector.OnGestureListener;
Import Android.view.GestureDetector.SimpleOnGestureListener;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.animation.Interpolator;
Import Android.widget.AbsListView;
Import Android.widget.FrameLayout;
Import Android.widget.LinearLayout;
Import Android.widget.Scroller;
/**
*
* @author Https://github.com/younfor
*
*/
public class Dragdelitem extends LinearLayout {

public static final int state_close = 0;public static final int state_open = 1;private View Mcontentview;public View Mmenu view;public int mdownx;public int state = state_close;public boolean isfling;private int mbasex;private Scroller SCROLL;PU    Blic dragdelitem (View contentview, view Menuview) {super (Contentview.getcontext ());    Scroll=new Scroller (GetContext ());    Mcontentview = Contentview;    Mmenuview = Menuview; Init ();} Private Dragdelitem (context context, AttributeSet Attrs) {Super (context, attrs);} Private Dragdelitem (Context context) {super (context);} private void Init () {Setlayoutparams (new Abslistview.layoutparams (Layoutparams.match_parent, LAYOUTPARAMS.W    Rap_content));    Layoutparams contentparams = new Layoutparams (layoutparams.wrap_content, layoutparams.wrap_content);    Mcontentview.setlayoutparams (Contentparams);    Mmenuview.setlayoutparams (New Layoutparams (Layoutparams.wrap_content, layoutparams.wrap_content)); AddView (Mcontentview); AddView (Mmenuview);}    public void swipe (int dis) {if (Dis > mmenuview.getwidth ()) {dis = Mmenuview.getwidth ();    } if (Dis < 0) {dis = 0;    } mcontentview.layout (-dis, Mcontentview.gettop (), mcontentview.getwidth ()-Dis, getmeasuredheight ()); Mmenuview.layout (mcontentview.getwidth ()-Dis, mmenuview.gettop (), mcontentview.getwidth () + Mmenuview.getwidt H ()-Dis, mmenuview.getbottom ());}            @Overridepublic void Computescroll () {if (state = = State_open) {if (Scroll.computescrolloffset ()) {            Swipe (Scroll.getcurrx ());        Postinvalidate ();            }} else {if (Scroll.computescrolloffset ()) {swipe (Mbasex-scroll.getcurrx ());        Postinvalidate ();    }}}public void Smoothclosemenu () {state = State_close;    Mbasex =-mcontentview.getleft ();    Scroll.startscroll (0, 0, mbasex, 0, 350); Postinvalidate ();} public void Smoothopenmenu (){state = State_open;    Scroll.startscroll (-mcontentview.getleft (), 0, Mmenuview.getwidth ()/2, 0, 350); Postinvalidate ();} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, Hei    GHTMEASURESPEC);            Mmenuview.measure (Measurespec.makemeasurespec (0, measurespec.unspecified), Measurespec.makemeasurespec (    Getmeasuredheight (), measurespec.exactly));            Mcontentview.measure (Measurespec.makemeasurespec (0, measurespec.unspecified), Measurespec.makemeasurespec ( Getmeasuredheight (), measurespec.exactly));} @Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {mcontentview.layout (0, 0, Getmeasuredwi    DTH (), Mcontentview.getmeasuredheight ()); Mmenuview.layout (Getmeasuredwidth (), 0, Getmeasuredwidth () + mmenuview.getmeasuredwidth (), Mcontentv Iew.getmeasuredheight ());}

}

 PackageCom.draglistview;ImportJava.util.List;Importcom.draglistview.*;Importandroid.app.Activity;ImportAndroid.content.pm.ApplicationInfo;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;/** * * @author https://github.com/younfor * * * * Public  class mainactivity extends Activity {    PrivateList<applicationinfo> mapplist;PrivateDragdellistview Mlistview;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (r.layout.activity_list); Mapplist = Getpackagemanager (). Getinstalledapplications (0);        Mlistview = (Dragdellistview) Findviewbyid (R.id.listview); Mlistview.setadapter (NewAppadapter (mapplist)); } class Appadapter extends Baseadapter {PrivateList<applicationinfo> mapplist; Public Appadapter(list<applicationinfo> applist)        {mapplist=applist; }@Override         Public int GetCount() {returnMapplist.size (); }@Override         PublicApplicationInfoGetItem(intPosition) {returnMapplist.get (position); }@Override         Public Long Getitemid(intPosition) {returnPosition }@Override         PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {Final intLoc=position; Viewholder holder=NULL; View menuview=NULL;if(Convertview = =NULL) {Convertview = View.inflate (Getapplicationcontext (), R.layout.swipecontent,NULL); Menuview = View.inflate (Getapplicationcontext (), R.layout.swipemenu,NULL);//composition content and menusConvertview =NewDragdelitem (Convertview,menuview); Holder=NewViewholder (Convertview); }Else{holder = (Viewholder) convertview.gettag ();                } ApplicationInfo item = GetItem (position);                Holder.iv_icon.setImageDrawable (Item.loadicon (Getpackagemanager ()));                Holder.tv_name.setText (Item.loadlabel (Getpackagemanager ())); Holder.tv_open.setOnClickListener (NewView.onclicklistener () {@Override                     Public void OnClick(View arg0) {Toast.maketext (mainactivity). This,"Open:"+loc, Toast.length_short). Show ();                }                }); Holder.tv_del.setOnClickListener (NewView.onclicklistener () {@Override                     Public void OnClick(View arg0) {                    }                });returnConvertview;            } class Viewholder {ImageView Iv_icon;            TextView Tv_name; TextView Tv_open,tv_del; Public Viewholder(View view)                {Iv_icon = (ImageView) View.findviewbyid (R.id.iv_icon);                Tv_name = (TextView) View.findviewbyid (r.id.tv_name);                Tv_open= (TextView) View.findviewbyid (R.id.tv_open);                Tv_del= (TextView) View.findviewbyid (R.id.tv_del); View.settag ( This); }        }    }}

Android UI Design: Imitation iOS, imitation QQ implementation ListView Slide Show button

Related Article

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.