android——仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

转载请注明出处:

www.cnblogs.com/shoneworn

我这里只是简单的用了两个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 {

    // ListView1
    private ListView mLv1 = null;
    // ListView2
    private ListView mLv2 = null;
    // list1的adapter
    private LsAdapter1 mAdapter1 = null;
    // list2的adapter
    private LsAdapter2 mAdapter2 = null;
    // 支持的刷卡头
    String[] arrSupportShua = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六","星期天"};

    List<String> mList1 = new ArrayList<String>();
    List<String> mList2 = new ArrayList<String>();
    /** 是否在移动,由于这边是动画结束后才进行的数据更替,设置这个限制为了避免操作太频繁造成的数据错乱。 */
    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);
    }

    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() {

            @Override
            public 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() {

            @Override
            public 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 = 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, 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];
        // 获取传递过来的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() {

            @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) {

            }
        });

     //使用TranslateAnimation。上面部分可以用这部分替换
        /*    // 创建移动动画
        TranslateAnimation moveAnimation = new TranslateAnimation(startLocation[0], endLocation[0], startLocation[1],
                endLocation[1]);
        moveAnimation.setDuration(300L);// 动画时间
        // 动画配置
        AnimationSet moveAnimationSet = new AnimationSet(true);
        moveAnimationSet.setFillAfter(false);// 动画效果执行完毕后,View对象不保留在终止的位置
        moveAnimationSet.addAnimation(moveAnimation);
        mMoveView.startAnimation(moveAnimationSet);
        moveAnimationSet.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                isMove = true;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                moveViewGroup.removeView(mMoveView);
                // instanceof 方法判断2边实例是不是一样,判断点击的是DragGrid还是OtherGridView
                if(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, View view, 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);
    }

    @Override
    public int getCount() {
        if (mList != null)
            return mList.size();
        return 0;
    }

    @Override
    public Object getItem(int 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 = 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();
    }

}

源码下载地址:

http://files.cnblogs.com/files/shoneworn/TestListAnimator.rar

推荐使用一个视频在线转gif的网站:

http://ezgif.com/video-to-gif

时间: 2024-10-08 16:58:43

android——仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中的相关文章

android 仿网易新闻客户端源码都有

原文:android 仿网易新闻客户端源码都有 android 仿网易新闻服务端源码 源代码下载地址: http://www.zuidaima.com/share/1550463560944640.htm http://www.zuidaima.com/share/1550463561206784.htm android 仿网易新闻 客户端和服务端 源码都有 ,有些功能还未实现,因为文件有点大,所以分为2次上传  java源代码截图:

Android仿网易客户端实现抽屉式拖拉菜单界面

前面有写过一篇文章使用DrawerLayout实现Android仿网易客户端抽屉模式,昨天从群里看一哥们问抽屉式拖拉,从主界面的下方出现,而使用DrawerLayout实现的是覆盖掉主界面,今天我们就来实现一下主界面下方脱出菜单界面,先上图,方便观看 啊哦,图片好大,开始今天的实现 1.继承HorizontalScrollView,实现自定义控件 package com.sdufe.thea.guo.view; import com.nineoldandroids.view.ViewHelper

ym——Android仿网易新闻导航栏PagerSlidingTabStrip源码分析

转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 前言 最近工作比较忙,所以现在才更新博文,对不住大家了~!言归正传,我们来说说这个PagerSlidingTabStrip,它是配合ViewPager使用的导航栏,网易新闻就是用的这个导航,我们仔细观察这个导航栏不仅他是跟着ViewPager滑动而滑动,而且指示器还会随着标题的长度而动态的变化长度,还可以改变多种样式哦~! · 下载地址: Github:https://github.

Android 仿网易新闻v3.5:上下滑动的引导页

版权声明:本文为博主原创文章,未经博主允许不得转载. 最近看了下网易新闻月初发布的3.5版,发现两个比较明显的改动: 1.引导页的修改,变为上下滑动. 2.增加了聚合阅读,里面的动画效果也是蛮有创意的.于是又禁不住模仿一下 这次先看这个上下滑动的引导页效果图: 这种效果具体怎么做呢? 首先直接上github,直接看有没有相关的开源项目,果不其然,被我找到了: https://github.com/JakeWharton/Android-DirectionalViewPager JakeWhart

android 仿网易新闻首页框架

   首页布局: 1 SliddingMenu  +  ViewPagerIndicator 2 JSON 解析   FastJson 3 网络请求  Volley 4 sqlite 数据库简单封装,主要处理数据库版本升级问题 5 微信.微博 API 简单封装 6 代码混淆 ...... github: https://github.com/lesliebeijing/MyAndroidFramework.git

[Android] Android 实现类似 今日头条 视频播放列表

演示实例如下: Talk is cheap. Show me the code 话不多说,代码在这里下载! https://github.com/wukong1688/Android_BaseVideo 如果觉得有帮助,欢迎在 Github 为我 star! 本博客地址: wukong1688 本文原文地址:https://www.cnblogs.com/wukong1688/p/10739635.html 转载请著名出处!谢谢~~ 原文地址:https://www.cnblogs.com/wu

iOS仿今日头条滑动导航

之前写了篇博客网易首页导航封装类.网易首页导航封装类优化,今天在前两个的基础上仿下今日头条. 1.网易首页导航封装类中主要解决了上面导航的ScrollView和下面的页面的ScrollView联动的问题,以及上面导航栏的便宜量. 2.网易首页导航封装类优化中主要解决iOS7以上滑动返回功能中UIScreenEdgePanGestureRecognizer与ScrollView的滑动的手势冲突问题. 今天仿今日头条滑动导航和网易首页导航封装类优化相似,这个也是解决手势冲突,UIPanGesture

android 1.6 launcher研究之自定义ViewGroup (转 2011.06.03(二)——— android 1.6 launcher研究之自定义ViewGroup )

2011.06.03(2)——— android 1.6 launcher研究之自定义ViewGroup2011.06.03(2)——— android 1.6 launcher研究之自定义ViewGroup 1.用xml来作为ViewGroup里面的View参考:http://www.eoeandroid.com/thread-30888-1-1.html MyViewGroup.java package com.lp; import android.content.Context; impo

Android日期时间选择器实现以及自定义大小

本文主要讲两个内容:1.如何将DatePicker和TimePicker放在一个dialog里面:2.改变他们的宽度: 问题1:其实现思路就是自定义一个Dialog,然后往里面同时放入DatePicker和TimePicker,直接贴代码: date_time_picker.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://s