Androidのlistview Sliding list items (click events and slide events coexist)-Bvin

Source: Internet
Author: User
Tags gety


Return Script Blackstone

Here exactly in the project there is such a BT demand, such as the ListView item can respond to click events can also respond to item's left and right sliding events, two events can be independent of each other.

I heard that the iphone list option has such a function of BT, the Android version of the mobile phone QQ and QQ Address book also have a similar effect, in the online all kinds of early-looking programs have tried, or can only slide can not click or can only click can not slide, and the operation is not sensitive, The code on the web is handled in Itemview's Ontouch method, judging the pixel difference between down and up. In fact, this operation is quite inconvenient, down-up this can only be counted as a drag event instead of sliding events, so you will associate the difference between scroll and fling.

You can look at my previous approach, using the Ontouch method of processing, it is difficult to separate sliding events and click events, even if you can swipe is very sensitive, operation 10 times a rare success.

classSwipelistenerImplementsview.ontouchlistener{

Viewholder Holder;
Houselist_item Item;
intStartX = 0;
intEndX = 0;

PublicSwipelistener (Viewholder holder, Houselist_item Item) {
Super();
This. Holder = Holder;
This. item = Item;
}



@Override
PublicBooleanOnTouch (View V, motionevent event) {
//TODO auto-generated Method Stub

if(event.getaction () = = Motionevent.action_down) {
StartX = (int) Event.getx ();
Debuger.log_e ("StartX", "+startx");
returntrue;
}Elseif(event.getaction () = = motionevent.action_up) {
EndX = (int) Event.getx ();
Debuger.log_e ("EndX", "+endx");
if(Startx-endx > 120) {
Debuger.log_e ("Trigger", "left stroke");
Holder.llMenu.setVisibility (view.visible);
returnfalse;
}Elseif(Endx-startx >120) {
Debuger.log_e ("Trigger", "right stroke");
Holder.llMenu.setVisibility (View.gone);
return true;
}Else{
Toast.maketext (CTX, "Click on item", +). Show ();
returntrue;
}


}
returntrue;
}

}

The code has comments that everyone can understand, like the above to let the sliding events and click events independently. At first it was silly to use the Onitemclick event with the ListView, which is really a bunker.

After a half-day effort to explore, today finally very smooth to achieve this effect. Here's the new code:

This is the adapter code, some omitted, mainly gesturedetector such a gesture listener.

PublicclassHouselist_adapterextendsbaseadapter{

PrivateGesturedetector detector;
PrivateListPrivateContext CTX =NULL;
PrivateLayoutinflater Inflater =NULL;
Flinglisteber Listener;

PublicHouselist_adapter (Context ctx,listSuper();
This. list = list;
This. CTX = CTX;
Inflater = (layoutinflater) ctx.getsystemservice (Context.layout_inflater_service);
Listener =NewFlinglisteber ();
detector =NewGesturedetector (listener);
}

@Override
PublicView GetView (intarg0, View arg1, ViewGroup arg2) {
//TODO auto-generated Method Stub
Viewholder holder =NULL;
if(arg1==NULL){
Arg1 = Inflater.inflate (R.layout.house_item_layout,NULL);
Holder =NewViewholder ();
Holder.llitem = (linearlayout) Arg1.findviewbyid (R.id.llitem);
Holder.tvtitle = (TextView) Arg1.findviewbyid (r.id.tvtitle);
Holder.tvbuildearea = (TextView) Arg1.findviewbyid (R.id.tvarea);
Holder.tvprice = (TextView) Arg1.findviewbyid (R.id.tvprice);
Holder.tvpriceunit = (TextView) Arg1.findviewbyid (r.id.tvpriceunit);
Holder.tvroom = (TextView) Arg1.findviewbyid (r.id.tvroom);
Holder.llflag = (linearlayout) Arg1.findviewbyid (R.id.llflag);
Holder.ivflag1 = (ImageView) Arg1.findviewbyid (R.ID.IVFLAG1);
Holder.ivflag2 = (ImageView) Arg1.findviewbyid (R.ID.IVFLAG2);
Holder.ivflag3 = (ImageView) Arg1.findviewbyid (R.ID.IVFLAG3);
Holder.ivflag4 = (ImageView) Arg1.findviewbyid (R.ID.IVFLAG4);
Holder.llmenu = (linearlayout) Arg1.findviewbyid (R.id.house_ltem_menu);
Holder.ivcall = (Button) Arg1.findviewbyid (R.id.ivcall);
Holder.ivdetails = (Button) Arg1.findviewbyid (R.id.ivcall);
Holder.ivmap = (Button) Arg1.findviewbyid (R.ID.IVMAP);
Holder.ivsend = (Button) Arg1.findviewbyid (r.id.ivsend);
Arg1.settag (holder);

}Else{
Holder = (viewholder) arg1.gettag ();
}
FinalHouselist_item Item = List.get (arg0);
Listener.setitem (item);
//Holder.llItem.setOnTouchListener (New Swipelistener (Holder,item));
Holder.llItem.setOnTouchListener (NewView.ontouchlistener () {

@Override
Public BooleanOnTouch (View V, motionevent event) {
//TODO auto-generated Method Stub
returnDetector.ontouchevent (event);
}
});

}

classFlinglisteberImplementsgesturedetector.ongesturelistener{

Houselist_item Item;
Viewholder Holder;

PublicHouselist_item GetItem () {
returnItem
}

PublicvoidSetItem (Houselist_item Item) {
This. item = Item;
}



PublicViewholder Getholder () {
returnHolder
}

PublicvoidSetholder (Viewholder holder) {
This. Holder = Holder;
}

@Override
PublicBooleanOndown (Motionevent e) {
//TODO auto-generated Method Stub
returnfalse;
}

@Override
PublicBooleanOnfling (motionevent E1, motionevent E2,floatVelocityx,
floatVELOCITYY) {
//TODO auto-generated Method Stub
if(E2.getx ()-e1.getx () >20) {
Toast.maketext (CTX, "left slide" +item.areaname, +). Show ();

}Else if(E1.getx ()-e2.getx () >20) {
Toast.maketext (CTX, "right slide" +item.areaname, +). Show ();
}

returnfalse;
}

@Override
PublicvoidOnlongpress (Motionevent e) {
//TODO auto-generated Method Stub

}

@Override
PublicBooleanOnscroll (motionevent E1, motionevent E2,
floatDistancex,floatDistancey) {
//TODO auto-generated Method Stub
returnfalse;
}

@Override
Public voidOnshowpress (Motionevent e) {
//TODO auto-generated Method Stub

}

@Override
PublicBooleanOnsingletapup (Motionevent e) {
//TODO auto-generated Method Stub
Toast.maketext (CTX, "Click on item", +). Show ();
return false;
}

}



}

This lets the item's sliding event to onfling to deal with, click the event to give Onsingletapup so that can let two events independent of each other, but so that the implementation of the discovery will still have a very smooth sliding, And then I thought that it must be the top and bottom of the ListView is in conflict with the left and right sliding events of item, so it is very sensitive to scrollview the previous definition of a touch event, never miss.

//sliding distance and coordinates
PrivatefloatXdistance, Ydistance, XLAST, Ylast;
@Override
PublicBooleanOnintercepttouchevent (motionevent ev) {
//TODO auto-generated Method Stub
Switch(Ev.getaction ()) {
CaseMotionevent.action_down:
Xdistance = Ydistance = 0f;
XLAST = Ev.getx ();
Ylast = Ev.gety ();
Break;
CaseMotionevent.action_move:
FinalfloatCurX = Ev.getx ();
FinalfloatCurY = Ev.gety ();

Xdistance + = Math.Abs (curx-xlast);
Ydistance + = Math.Abs (cury-ylast);
XLAST = CurX;
Ylast = CurY;

if(Xdistance > Ydistance) {
return false;
}
}

returnSuper. onintercepttouchevent (EV);
}

Copy this code into a ListView extension class to overwrite the line.


Original link:/link.php?url=http://www.cnblogs.com/bvin/p/3153191.html, reprint please specify.




More articles:
    • With Project PHP Eighth Lecture-index.dwt file analysis in Ecshop

    • "Servlet3.0 new Features" section No. 01 _servlet annotations and asynchronous support

    • Five tricks to get your interviewer attracted to you

    • "Servlet3.0 new Features" section No. 02 _servlet dynamic Registration and using annotations to write listeners and filters

    • [C++stdlib Basics] about string manipulation--c++ standard library header file

    • Status bar notifications for Android (Notification)

    • How to change the logo when Android boots up

    • Resolution on no managedconnections available within configured blocking timeout exception-midnight outrageous

    • Through the source of the front end of the Backbone one

    • MyEclipse Common plugin Sharing





Baidu Statistics

Recommended for you


Androidのlistview Sliding list items (click events and slide events coexist)-Bvin

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.