Android實現(xiàn)仿網(wǎng)易今日頭條等自定義頻道listview 或者grideview等item上移到另一個view中
我這里只是簡單的用了兩個listview來實現(xiàn)的,先上效果圖。比較粗糙。預(yù)留了自定義的空間。
思路:
從上圖應(yīng)該可以看的出來。就是上下兩個listview。點擊下面的ltem。會動態(tài)的移動到上一個listview的最后。上面的listview 為listview1,下面的為listview2. 點擊listview2,獲取到view ,設(shè)置一個動畫,移動到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>(); /** 是否在移動,由于這邊是動畫結(jié)束后才進行的數(shù)據(jù)更替,設(shè)置這個限制為了避免操作太頻繁造成的數(shù)據(jù)錯亂。 */ 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) { //如果點擊的時候,之前動畫還沒結(jié)束,那么就讓點擊事件無效 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]; // 獲取終點的坐標(biāo) 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) { //如果點擊的時候,之前動畫還沒結(jié)束,那么就讓點擊事件無效 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]; // 獲取終點的坐標(biāo) 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的坐標(biāo) moveView.getLocationInWindow(initLocation); // 得到要移動的VIEW,并放入對應(yīng)的容器中 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。上面部分可以用這部分替換 /* // 創(chuàng)建移動動畫 TranslateAnimation moveAnimation = new TranslateAnimation(startLocation[0], endLocation[0], startLocation[1], endLocation[1]); moveAnimation.setDuration(300L);// 動畫時間 // 動畫配置 AnimationSet moveAnimationSet = new AnimationSet(true); moveAnimationSet.setFillAfter(false);// 動畫效果執(zhí)行完畢后,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; } } });*/ } /** * 創(chuàng)建移動的ITEM對應(yīng)的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的對應(yīng)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,放入對應(yīng)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; } /** 設(shè)置刪除的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(); } }
以上內(nèi)容是小編給大家介紹的Android實現(xiàn)仿網(wǎng)易今日頭條等自定義頻道listview 或者grideview等item上移到另一個view中的全部知識,希望對大家有所幫助!
- android自定義view仿今日頭條加載文字變色效果
- Android仿今日頭條頂部導(dǎo)航欄效果的實例代碼
- Android仿今日頭條多個fragment懶加載的實現(xiàn)
- Android使用RecyclerView實現(xiàn)今日頭條頻道管理功能
- Android 仿今日頭條評論時鍵盤自動彈出的效果(推薦)
- Android 仿今日頭條簡單的刷新效果實例代碼
- Android仿今日頭條APP實現(xiàn)下拉導(dǎo)航選擇菜單效果
- Android應(yīng)用中仿今日頭條App制作ViewPager指示器
- Android仿今日頭條滑動頁面導(dǎo)航效果
- Android實現(xiàn)今日頭條訂閱頻道效果
相關(guān)文章
Android ExpandableListView雙層嵌套實現(xiàn)三級樹形菜單
這篇文章主要介紹了Android ExpandableListView雙層嵌套實現(xiàn)三級樹形菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11Android 實現(xiàn)IOS 滾輪選擇控件的實例(源碼下載)
這篇文章主要介紹了 Android 實現(xiàn)IOS 滾輪選擇控件的實例(源碼下載)的相關(guān)資料,需要的朋友可以參考下2017-03-03Android使用 Retrofit 2.X 上傳多文件和多表單示例
本篇文章主要介紹了Android使用 Retrofit 2.X 上傳多文件和多表單示例,具有一定的參考價值,有興趣的小伙伴一起來了解一下2017-08-08Android編程重寫ViewGroup實現(xiàn)卡片布局的方法
這篇文章主要介紹了Android編程重寫ViewGroup實現(xiàn)卡片布局的方法,實例分析新建FlowLayout繼承ViewGroup類及設(shè)置布局文件實現(xiàn)卡片布局效果的相關(guān)技巧,需要的朋友可以參考下2016-02-02android為ListView每個Item上面的按鈕添加事件
本篇文章主要介紹了android為ListView每個Item上面的按鈕添加事件,有興趣的同學(xué)可以了解一下。2016-11-11