Android realizes a custom channel ListView or Grideview, such as NetEase's today's headline, moves to another view _android

Source: Internet
Author: User

I am here simply with two ListView to achieve, first on the effect of the picture. More coarse. A custom space is reserved.

Ideas:

From the above figure should be able to see out. is the top and bottom two ListView. Click on the ltem below. will move dynamically to the last ListView. The listview above is ListView1, and the following is Listview2. Click Listview2, get to view, set up an animation, move to ListView1, listview2 delete the item in point. A new one is added to the ListView1.

Code on:

Mainactivity.java part

Package com.example.testlistanimator;
Import java.util.ArrayList;
Import java.util.List;
Import Android.animation.Animator;
Import Android.animation.Animator.AnimatorListener;
Import Android.animation.ObjectAnimator;
Import Android.animation.ValueAnimator;
Import Android.annotation.SuppressLint;
Import Android.annotation.TargetApi;
Import android.app.Activity;
Import Android.graphics.Bitmap;
Import Android.os.Build;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.ViewGroup.LayoutParams;
Import android.view.animation.Animation;
Import Android.view.animation.Animation.AnimationListener;
Import Android.view.animation.AnimationSet;
Import android.view.animation.TranslateAnimation;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import Android.widget.ListView;
Import Android.widget.TextView; @SuppresSlint ("Newapi") @TargetApi (build.version_codes. Honeycomb) public class Mainactivity extends activity {//ListView1 private ListView MLV1 = null;//LISTVIEW2 Private Li
Stview mLv2 = null;
List1 's adapter private LsAdapter1 mAdapter1 = null;
List2 's adapter private LsAdapter2 mAdapter2 = null;
Supported swipe head string[] Arrsupportshua = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
list<string> mList1 = new arraylist<string> ();
list<string> mList2 = new arraylist<string> (); /** is moving, because this is the end of the animation after the data replacement, set this limit in order to avoid the operation of too often caused by data confusion.
* * Boolean ismove = false; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Initview ();
InitData ();
Initlistener (); private void Initview () {mLv1 = (ListView) Findviewbyid (r.id.list1); mLv2 = (ListView) Findviewbyid (R.ID.LIST2);} priv ate void MakeList () {for (String Shua:arrsupportshua) {mlist2.add (Shua);}} private void InitData () {makelist ();
MAdapter1 = new LsAdapter1 (mainactivity.this, mList1);
MAdapter2 = new LsAdapter2 (mainactivity.this, mList2);
Mlv1.setadapter (MAdapter1);
Mlv2.setadapter (MADAPTER2); private void Initlistener () {Mlv1.setonitemclicklistener (new Onitemclicklistener () {@Override public void Onitemclick (adapterview<?> arg0, view view, final int location, long arg3)
{//If clicked, the previous animation is not finished, then let the Click event Invalid if (ismove) {return;} final ImageView img = getview (view);
TextView MTV = (TextView) View.findviewbyid (R.ID.ITEM_TV);
Final int[] startlocation = new int[2];
Mtv.getlocationinwindow (startlocation);
Final String Mshua = mlist1.get (location);
Madapter2.setvisible (FALSE);
Madapter2.additem (Mshua); New Handler (). postdelayed (New Runnable () {public void run () {try {int[] endlocation = new int[2];//Get the coordinates of the endpoint Mlv2.getch
Ildat (Mlv2.getlastvisibleposition ()). Getlocationinwindow (endlocation);
Moveanim (IMG, startlocation, Endlocation, Mshua, 1);
Madapter1.setremove (location); catch (Exception localexception) {
}}, 50L);
}
}); Mlv2.setonitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (Adapterview<?> arg0, View view, final int location, long Arg3 {//If clicked, the previous animation is not finished, then let the Click event Invalid if (ismove) {return;} final ImageView img = Getv
Iew (view);
TextView MTV = (TextView) View.findviewbyid (R.ID.ITEM_TV);
Final int[] startlocation = new int[2];
Mtv.getlocationinwindow (startlocation);
Final String Mshua = mlist2.get (location);
Madapter1.setvisible (FALSE);
Madapter1.additem (Mshua); New Handler (). postdelayed (New Runnable () {public void run () {try {int[] endlocation = new int[2];//Get the coordinates of the endpoint Mlv1.getch
Ildat (Mlv1.getlastvisibleposition ()). Getlocationinwindow (endlocation);
Moveanim (IMG, startlocation, Endlocation, Mshua, 2);
Madapter2.setremove (location);
catch (Exception localexception) {}}, 50L);
}
}); } private void Moveanim (ImageView moveview, int[] startlocation, int[] endlocation, String mshua, final int code) {int[]
Initlocation = new Int[2]; Get the V passed overIew coordinate Moveview.getlocationinwindow (initlocation);
Get the view to move and put in the corresponding container final viewgroup Moveviewgroup = Getmoveviewgroup ();
Final View Mmoveview = Getmoveview (Moveviewgroup, Moveview, initlocation); Use objectanimator animation objectanimator manimator = Objectanimator.offloat (Mmoveview, "Translationy", startLocation[1),
ENDLOCATION[1]);
Manimator.setduration (300);
Manimator.start ();
Ismove = true; Manimator.addlistener (New Animatorlistener () {@Override public void Onanimationstart (animator animation) {Ismove = True
;  @Override public void Onanimationrepeat (animator animation) {} @Override public void Onanimationend (animator animation)
{Moveviewgroup.removeview (Mmoveview); if (code==1) {madapter2.setvisible (true); madapter2.notifydatasetchanged ();
Madapter1.remove ();
Ismove = false;
}else{madapter1.setvisible (True); madapter1.notifydatasetchanged (); Madapter2.remove (); ismove = false;}}
@Override public void Onanimationcancel (animator animation) {}}); Use Translateanimation. The upper part can use thisPartial replacement///create mobile animation translateanimation moveanimation = new Translateanimation (startlocation[0], endlocation[0],
STARTLOCATION[1], endlocation[1]);
Moveanimation.setduration (300L);//animation time//animation configuration Animationset Moveanimationset = new Animationset (true);
Moveanimationset.setfillafter (false);//The View object does not remain in the terminated position moveanimationset.addanimation (moveanimation) after the animation effect has finished executing;
Mmoveview.startanimation (Moveanimationset); Moveanimationset.setanimationlistener (New Animationlistener () {@Override public void Onanimationstart (Animation Animation) {Ismove = true;} @Override public void Onanimationrepeat (animation animation) {} @Override public void Onani Mationend (Animation Animation) {Moveviewgroup.removeview (Mmoveview);//instanceof method to determine whether 2-side instances are the same,
Determine if the click is Draggrid or Othergridview if (code==1) {madapter2.setvisible (true); madapter2.notifydatasetchanged ();
Madapter1.remove ();
Ismove = false;
}else{madapter1.setvisible (True); madapter1.notifydatasetchanged (); Madapter2.remove (); ismove = false;}}
});*/
}
/*** Create moving item corresponding to the ViewGroup layout container/Private ViewGroup Getmoveviewgroup () {ViewGroup Moveviewgroup = (viewgroup) GetWindow (). g
Etdecorview ();
LinearLayout movelinearlayout = new LinearLayout (this); Movelinearlayout Setlayoutparams (New Linearlayout.layoutparams (Layoutparams.match_parent, LayoutParams.MATCH_
PARENT));
Moveviewgroup.addview (movelinearlayout);
return movelinearlayout; 
/** * Get the corresponding view of the clicked Item, * * @param view * @return/Private ImageView GetView (view view) {View.destroydrawingcache ();
View.setdrawingcacheenabled (TRUE);
Bitmap cache = Bitmap.createbitmap (View.getdrawingcache ());
View.setdrawingcacheenabled (FALSE);
ImageView IV = new ImageView (this);
Iv.setimagebitmap (cache);
return IV; /** * Get moved view, put in corresponding ViewGroup layout container * * @param viewgroup * @param view * @param initlocation * @return/Private View GE Tmoveview (ViewGroup viewgroup, view view, int[] initlocation) {int x = initlocation[0]; int y = initlocation[1]; Viewgrou
P.addview (view); Linearlayout.layoutparams MLAYOUTPArams = new Linearlayout.layoutparams (layoutparams.wrap_content, layoutparams.wrap_content);
Mlayoutparams.leftmargin = x;
Mlayoutparams.topmargin = y;
View.setlayoutparams (Mlayoutparams);
return view; }
}

Two adapter sections. Two Chabudu. Pass a

Package com.example.testlistanimator;
Import java.util.List;
Import Android.content.Context;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.TextView; public class LsAdapter1 extends Baseadapter {private context mcontext; private list<string> mlist; private Layoutin
Flater minflater = null;
Private Boolean isVisible = true;
/** to delete the position/public int remove_position =-1; Private int[] bg = {R.DRAWABLE.A1,R.DRAWABLE.A2,R.DRAWABLE.A3,R.DRAWABLE.A4,R.DRAWABLE.A5,R.DRAWABLE.A6,
R.DRAWABLE.A7}; Public LsAdapter1 (Context Mcontext, list<string> mlist) {this.mcontext = Mcontext; this.mlist = mlist; minflater =
Layoutinflater.from (Mcontext); @Override public int GetCount () {if (mlist!= null) return Mlist.size (), return 0;} @Override the public Object getitem (in T position) {if (mlist!= null) return mlist.get (position); return null;} @Override public long getitemid (int positION) {return position} @Override public View getview (int position, View Convertview, ViewGroup parent) {View view = MI
Nflater.inflate (R.layout.list_item, NULL);
TextView TV = (TextView) View.findviewbyid (R.ID.ITEM_TV);
Tv.setbackgroundresource (Bg[position]);
Tv.settext (Mlist.get (position));
if (!isvisible && (Position = = 1 + mlist.size ())) {Tv.settext ("");}
if (remove_position = = position) {Tv.settext ("");}
return view; public void AddItem (String Mshua) {mlist.add (Mshua); notifydatasetchanged ();} public void setvisible (Boolean isVisible {this.isvisible = isVisible}/** Set Delete position/public void setremove (int position) {remove_position = position; n
Otifydatasetchanged (); 
/** Delete Channel list */public void Remove () {//System.out.println ("list1=" +mlist.size () + "remove_position =" +remove_position); if (remove_position>=0| |
Remove_position<mlist.size ()) Mlist.remove (remove_position);
Remove_position =-1;
Notifydatasetchanged (); }
}

The above content is a small series to introduce the Android to the realization of such as NetEase today's headlines, such as Custom channel ListView or Grideview and other items moved to another view of all knowledge, I hope to help!

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.