我这里只是简单的用了两个ListvIEw来实现的,先上效果图。比较粗糙。预留了自定义的空间。
思路:
从上图应该可以看的出来。就是上下两个ListvIEw。点击下面的ltem。会动态的移动到上一个ListvIEw的最后。上面的ListvIEw 为ListvIEw1,下面的为ListvIEw2. 点击ListvIEw2,获取到vIEw ,设置一个动画,移动到ListvIEw1,ListvIEw2中删除被点的item。ListvIEw1中新增一个。
上代码:
Mainactivity.java 部分
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 {// ListVIEw1private ListVIEw mLv1 = null;// ListVIEw2private ListVIEw mLv2 = null;// List1的adapterprivate LsAdapter1 mAdapter1 = null;// List2的adapterprivate LsAdapter2 mAdapter2 = null;// 支持的刷卡头String[] arrSupportShua = { "星期一","星期二","星期三","星期四","星期五","星期六","星期天"};List<String> mList1 = new ArrayList<String>();List<String> mList2 = new ArrayList<String>();/** 是否在移动,由于这边是动画结束后才进行的数据更替,设置这个限制为了避免 *** 作太频繁造成的数据错乱。 */boolean isMove = false;@OverrIDeprotected 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);}private 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() {@OverrIDepublic voID onItemClick(AdapterVIEw<?> arg0,VIEw vIEw,final int location,long arg3) {//如果点击的时候,之前动画还没结束,那么就让点击事件无效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];// 获取终点的坐标mLv2.getChildAt(mLv2.getLastVisibleposition()).getLocationInWindow(endLocation);MoveAnim(img,startLocation,endLocation,mShua,1);mAdapter1.setRemove(location);} catch (Exception localException) {}}},50L);}});mLv2.setonItemClickListener(new OnItemClickListener() {@OverrIDepublic voID onItemClick(AdapterVIEw<?> arg0,long arg3) {//如果点击的时候,之前动画还没结束,那么就让点击事件无效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 = mList2.get(location);mAdapter1.setVisible(false);mAdapter1.addItem(mShua);new Handler().postDelayed(new Runnable() {public voID run() {try {int[] endLocation = new int[2];// 获取终点的坐标mLv1.getChildAt(mLv1.getLastVisibleposition()).getLocationInWindow(endLocation);MoveAnim(img,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];// 获取传递过来的VIEW的坐标moveVIEw.getLocationInWindow(initLocation);// 得到要移动的VIEW,并放入对应的容器中final VIEwGroup moveVIEwGroup = getMoveVIEwGroup();final VIEw mMoveVIEw = getMoveVIEw(moveVIEwGroup,moveVIEw,initLocation);//使用ObjectAnimator动画ObjectAnimator mAnimator = ObjectAnimator.offloat(mMoveVIEw,"translationY",startLocation[1],endLocation[1]);mAnimator.setDuration(300);mAnimator.start();isMove = true;mAnimator.addListener(new AnimatorListener() {@OverrIDepublic voID onAnimationStart(Animator animation) {isMove = true;}@OverrIDepublic voID onAnimationRepeat(Animator animation) {}@OverrIDepublic 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;}}@OverrIDepublic voID onAnimationCancel(Animator animation) {}});//使用TranslateAnimation。上面部分可以用这部分替换/* // 创建移动动画TranslateAnimation moveAnimation = new TranslateAnimation(startLocation[0],endLocation[0],endLocation[1]);moveAnimation.setDuration(300L);// 动画时间// 动画配置AnimationSet moveAnimationSet = new AnimationSet(true);moveAnimationSet.setFillAfter(false);// 动画效果执行完毕后,VIEw对象不保留在终止的位置moveAnimationSet.addAnimation(moveAnimation);mMoveVIEw.startAnimation(moveAnimationSet);moveAnimationSet.setAnimationListener(new AnimationListener() {@OverrIDepublic voID onAnimationStart(Animation animation) {isMove = true;}@OverrIDepublic voID onAnimationRepeat(Animation animation) {}@OverrIDepublic voID onAnimationEnd(Animation animation) {moveVIEwGroup.removeVIEw(mMoveVIEw);// instanceof 方法判断2边实例是不是一样,判断点击的是DragGrID还是OtherGrIDVIEwif(code==1){mAdapter2.setVisible(true);mAdapter2.notifyDataSetChanged();mAdapter1.remove();isMove = false;}else{mAdapter1.setVisible(true);mAdapter1.notifyDataSetChanged();mAdapter2.remove();isMove = false;}}});*/}/*** 创建移动的ITEM对应的VIEwGroup布局容器*/private VIEwGroup getMoveVIEwGroup() {VIEwGroup moveVIEwGroup = (VIEwGroup) getwindow().getDecorVIEw();linearLayout movelinearLayout = new linearLayout(this);movelinearLayout.setLayoutParams(new linearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));moveVIEwGroup.addVIEw(movelinearLayout);return movelinearLayout;}/*** 获取点击的Item的对应VIEw,* * @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;}/*** 获取移动的VIEW,放入对应VIEwGroup布局容器* * @param vIEwGroup* @param vIEw* @param initLocation* @return*/private VIEw getMoveVIEw(VIEwGroup vIEwGroup,int[] initLocation) {int x = initLocation[0];int y = initLocation[1];vIEwGroup.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;}}
两个adapter部分。两个差不都。传一个
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 LayoutInflater mInflater = null;private boolean isVisible = true;/** 要删除的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);}@OverrIDepublic int getCount() {if (mList != null)return mList.size();return 0;}@OverrIDepublic Object getItem(int position) {if (mList != null)return mList.get(position);return null;}@OverrIDepublic long getItemID(int position) {return position;}@OverrIDepublic VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {VIEw vIEw = mInflater.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;}/** 设置删除的position */public voID setRemove(int position) {remove_position = position;notifyDataSetChanged();}/** 删除频道列表 */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();}}
以上内容是小编给大家介绍的AndroID实现仿网易今日头条等自定义频道ListvIEw 或者grIDevIEw等item上移到另一个vIEw中的全部知识,希望对大家有所帮助!
总结以上是内存溢出为你收集整理的Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中全部内容,希望文章能够帮你解决Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)