文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2022-06-06 08:55

关注

我这里只是简单的用了两个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。上面部分可以用这部分替换

}

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;
}

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;
}

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;

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;
}

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自定义view仿今日头条加载文字变色效果Android仿今日头条顶部导航栏效果的实例代码Android仿今日头条多个fragment懒加载的实现Android使用RecyclerView实现今日头条频道管理功能Android 仿今日头条评论时键盘自动弹出的效果(推荐)Android 仿今日头条简单的刷新效果实例代码Android仿今日头条APP实现下拉导航选择菜单效果Android应用中仿今日头条App制作ViewPager指示器Android仿今日头条滑动页面导航效果Android实现今日头条订阅频道效果


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯