Easy realization of Android imitation Taobao area selection function _android

Source: Internet
Author: User
Tags commit

Taobao client recently, when editing the address of a region to select the function. Look at the effect of the above feel pretty cool, when scrolling, is the last one from below fly up next to the previous one. Just make a play for yourself.

Said the effect may not be intuitive, the following two pictures to see the effect

Taobao Area selection Effect

One more piece of your own effect.

GIF may not be very good, we use the Android phone to open Taobao look

Implementation analysis

The display is very simple, ListView is OK. For the animation effect, just need to getview in the time to get to show the view, through the property animation to modify Translationy OK. Since the region selection is an interface, fragment's Addtobackstack knowledge is also used here.

1, used to show the fragment

Use a fragment to accept the Parentcode parameter to get all the child regions of the parent area and then display it. This is done with fragment because, with activity, such continuous clicks are the same kind of interface that is not appropriate.

public class Areafragment extends Fragment implements Adapterview.onitemclicklistener {private static final String ARG
 _param1 = "Parentcode";
 @Bind (R.id.refresh_list_view) ListView Mrefreshlistview;

 @Bind (R.id.loadingbar) ProgressBar Mloadingbar;

 Private String mparam1;//parentcode parameter okhttpclient mokhttpclient = new Okhttpclient ();

 Private Onfragmentinteractionlistener Mlistener; Private Areaadapter adapter;//Area adapter Public areafragment () {}/** * Use this factory the to create a new ins
  Tance of * This fragment using the provided parameters.
  * * @param param1 Parameter 1.
  * @return A new instance of fragment Areafragment.
  */public static areafragment newinstance (String param1) {areafragment fragment = new Areafragment ();
  Bundle args = new Bundle ();
  Args.putstring (arg_param1, param1);
  Fragment.setarguments (args);
 return fragment;
  @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); IF (getarguments ()!= null) {//Get the code for the parent area to query the sub area mParam1 = Getarguments (). getString (ARG_PARAM1); @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate
  {//Inflate the layout for this fragment view view = Inflater.inflate (R.layout.fragment_area, container, false);
  Butterknife.bind (this, view);

  Mrefreshlistview.setonitemclicklistener (this);
  Formencodingbuilder builder = new Formencodingbuilder ();

  Builder.add (ARG_PARAM1,MPARAM1); Requesting the area via Parentcode, if Parentcode does not exist, is the first level final request = new Request.builder (). URL ("http://123.184.16.19:80
  08/area/list "). Post (Builder.build ()). build (); Mokhttpclient.newcall (Request). Enqueue (New Callback () {@Override public void onfailure (Request request, IOException e) {} @Override public void Onresponse (Response Response) throws IOException {final String res = Response
    . Body (). String (); if (res!=null) {GsonGson = new Gson ();
     Jsonresult Jsonresult = Gson.fromjson (res, jsonresult.class);

      if (jsonresult.issuccess ()) {List List = (list) jsonresult.getresult ();
      List NewList = new ArrayList ();
      Iterator iterator = List.iterator ();
       while (Iterator.hasnext ()) {map map = (map) iterator.next ();
       Areainfo areainfo = Gson.fromjson (Gson.tojson (map), areainfo.class);
      Newlist.add (Areainfo);
      } adapter = new Areaadapter (GetContext (), newlist); Getactivity (). Runonuithread (New Runnable () {@Override public void run () {//Get the data for display Mrefres
       Hlistview.setadapter (adapter);
     }
      });

  }
    }
   }
  });
 return view;
  @Override public void Onattach (context) {Super.onattach);
  If (context instanceof Onfragmentinteractionlistener) {Mlistener = (onfragmentinteractionlistener) context; else {throw new RuntimeException (context.tostring () + must implement OnfragmeNtinteractionlistener ");
  @Override public void Ondetach () {Super.ondetach ();
 Mlistener = null;
  @Override public void Ondestroyview () {Super.ondestroyview ();
 Butterknife.unbind (this); @Override public void Onitemclick (adapterview<?> parent, view view, int position, long ID) {//Click to process Region click
  Events, which were uniformly entrusted to the activity processing areainfo Areainfo = (areainfo) parent.getadapter (). GetItem (position);
  if (areainfo==null) return;
  if (mlistener!=null) {mlistener.onfragmentinteraction (areainfo); The callback interface used to interact with the activity is public interface Onfragmentinteractionlistener {void onfragmentinteraction (Areainfo areai
 NFO);

 }

We used a fragment to accept the Parentcode, to request the next level of the region, after the success of the show. and provides a onfragmentinteractionlistener to interact with the activity when Onitemclick.

Next look at adapter, the beginning we mentioned to realize the effect of Taobao we just have to get the view to be displayed, set animation on it.

2, processing the display effect adapter

Class Areaadapter extends Baseadapter {private list list;

  private int lastposition;
  Public Areaadapter (Context context, list<areainfo> list) {this.list = list;
  @Override public int GetCount () {return list.size ();
  @Override public Object getitem (int position) {return list.get (position);
  @Override public long getitemid (int position) {return 0;
   @Override public View getview (int position, View Convertview, ViewGroup parent) {Viewholder viewholder = null; if (convertview==null) {Convertview = Layoutinflater.from (GetContext ()). Inflate (R.layout.area_list_item,parent,fal
    SE);
    Viewholder = new Viewholder (); Viewholder.textview = (TextView) Convertview.findviewbyid (Android.
    R.ID.TEXT1);
   Convertview.settag (Viewholder);
   } Viewholder = (Viewholder) convertview.gettag ();
   Areainfo item = (areainfo) list.get (position);
   ViewHolder.textView.setText (Item.getareaname ()); if (LASTPOSITION&LT;POSITION&Amp;&lastposition!=0) {objectanimator.offloat (Convertview, "Translationy", Convertview.getheight () *2,0).

   Setduration. Start ();
   } lastposition = position;
  return convertview;
  Class viewholder{TextView TextView;

 }
 }

A very common adapter writing, only in the GetView to get to show the view, through
Objectanimator.offloat (Convertview, "Translationy", Convertview.getheight () *2,0). Setduration. Start () The animation was set for Veiw,

There is also a variable position to distinguish the animation only when scrolling up. But I think the effect of position difference is also good, we can try.

In fact, this has achieved the effect, and then incidentally mention the activity of the framgnet in the treatment of Onitemclick.

3. Interactive processing of activity and fragment

public class Areaselectactivity extends appcompatactivity implements areafragment.onfragmentinteractionlistener{
 Private Fragment onefragment;


 Private Fragment twofragment;
 Private Map map = new HashMap ();
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.activity_area_select);
  Butterknife.bind (this);
  New first level area, parentcode parameter is null onefragment = Areafragment.newinstance ("");
  Fragmentmanager Fragmentmanager = Getsupportfragmentmanager ();
 Fragmentmanager.begintransaction (). replace (r.id.content,onefragment). commit (); @Override public boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {case Android.
    R.id.home:fragmentmanager Fragmentmanager = Getsupportfragmentmanager ();
    if (Fragmentmanager.getbackstackentrycount () >0) {fragmentmanager.popbackstack ();
    }else{finish ();
  } break;
 return true; /** * Handles interaction, hide before a fragment, andCall Addtobackstack so fragment can click Back to display the previous fragment * if it is a third-tier region then direct return to the region select data for the last activity * @param areainfo the area to be clicked * * @O
  Verride public void Onfragmentinteraction (Areainfo areainfo) {if (areainfo==null) {return;
  } fragmenttransaction Transaction = Getsupportfragmentmanager (). BeginTransaction ();
  int level = Areainfo.getlevel ();
    Switch (level) {case 1:map.put ("Provid", Areainfo.getid ());
    Map.put ("Provname", Areainfo.getareaname ());
     if (Areainfo.isleaf ()) {Intent Intent = new Intent ();
     Intent.putextra ("AddressInfo", (Serializable) map);
     Setresult (result_ok,intent);
    Finish ();
     }else{transaction.hide (onefragment); Transaction.add (R.id.content,twofragment=areafragment.newinstance (Areainfo.getareacode () + "")). AddToBackStack (
    NULL). commit ();
   } break;
    Case 2:map.put ("Cityid", Areainfo.getid ());
    Map.put ("CityName", Areainfo.getareaname ());
     if (Areainfo.isleaf ()) {Intent Intent = new Intent (); Intent.putExtra ("AddressInfo", (Serializable) map);
     Setresult (result_ok,intent);
    Finish ();
     }else {transaction.hide (twofragment); Transaction.add (R.id.content, Areafragment.newinstance (Areainfo.getareacode () + "")). Addtobackstack (NULL). Commit (
    );
   } break;
    Case 3:map.put ("Districtid", Areainfo.getid ());
    Map.put ("Districtname", Areainfo.getareaname ());
    Intent Intent = new Intent ();
    Intent.putextra ("AddressInfo", (Serializable) map);
    Setresult (result_ok,intent);
    Finish ();
  Break

 }

 }
}

This imitation Taobao region to achieve the choice!

Conclusion

You can write your own test interface, you can also directly invoke my written interface: http://123.184.16.19:8008/area/list

Source to provide you with reference: Android imitation Taobao region selection

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.