频道管理

来源:互联网 发布:广州恒大淘宝还合作吗 编辑:程序博客网 时间:2024/04/28 10:35

要实现2个gridview之间的Item相互移动:

1、首先我们获取我们点击的位置、处于gridview哪个位置

2、获取位置后、我们就能拿到这个Item的View,我们获取item绘制缓存的Bitmap对象。

3、将Bitmap设置的一个Imageview上,然后将这个ImageView放到一个容器中去进行移动操作,这样可能有人有疑问,为什么不直接把item放到容器中去呢,是因为item已经有自己的父容器gridview,所以我们new一个Imageview来代替item

4、然后我们将imageview移动到另一个gridview的最后一个位置。

5、最后刷新2个gridview的视图、就能实现我们所见的效果。

实现代码

主程序代码:


  1. package com.test.drag;  
  2.   
  3. import android.graphics.Bitmap;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.support.v7.app.AppCompatActivity;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.view.animation.Animation;  
  10. import android.view.animation.AnimationSet;  
  11. import android.view.animation.TranslateAnimation;  
  12. import android.widget.AdapterView;  
  13. import android.widget.AdapterView.OnItemClickListener;  
  14. import android.widget.GridView;  
  15. import android.widget.ImageView;  
  16. import android.widget.LinearLayout;  
  17. import android.widget.TextView;  
  18.   
  19. import com.test.drag.view.MyGridView;  
  20.   
  21. import java.util.ArrayList;  
  22. import java.util.List;  
  23.   
  24. public class MainActivity extends AppCompatActivity implements OnItemClickListener {  
  25.     private MyGridView mUserGv, mOtherGv;  
  26.     private List<String> mUserList = new ArrayList<>();  
  27.     private List<String> mOtherList = new ArrayList<>();  
  28.     private OtherAdapter mUserAdapter, mOtherAdapter;  
  29.   
  30.     @Override  
  31.     protected void onCreate(Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.activity_main);  
  34.         initView();  
  35.     }  
  36.   
  37.     public void initView() {  
  38.         mUserGv = (MyGridView) findViewById(R.id.userGridView);  
  39.         mOtherGv = (MyGridView) findViewById(R.id.otherGridView);  
  40.         mUserList.add("推荐");  
  41.         mUserList.add("热点");  
  42.         mUserList.add("上海");  
  43.         mUserList.add("时尚");  
  44.         mUserList.add("科技");  
  45.         mUserList.add("体育");  
  46.         mUserList.add("军事");  
  47.         mUserList.add("财经");  
  48.         mUserList.add("网络");  
  49.         mOtherList.add("汽车");  
  50.         mOtherList.add("房产");  
  51.         mOtherList.add("社会");  
  52.         mOtherList.add("情感");  
  53.         mOtherList.add("女人");  
  54.         mOtherList.add("旅游");  
  55.         mOtherList.add("健康");  
  56.         mOtherList.add("美女");  
  57.         mOtherList.add("游戏");  
  58.         mOtherList.add("数码");  
  59.         mOtherList.add("娱乐");  
  60.         mOtherList.add("探索");  
  61.         mUserAdapter = new OtherAdapter(this, mUserList,true);  
  62.         mOtherAdapter = new OtherAdapter(this, mOtherList,false);  
  63.         mUserGv.setAdapter(mUserAdapter);  
  64.         mOtherGv.setAdapter(mOtherAdapter);  
  65.         mUserGv.setOnItemClickListener(this);  
  66.         mOtherGv.setOnItemClickListener(this);  
  67.     }  
  68.   
  69.     /**  
  70.      *获取点击的Item的对应View,  
  71.      *因为点击的Item已经有了自己归属的父容器MyGridView,所有我们要是有一个ImageView来代替Item移动  
  72.      * @param view  
  73.      * @return  
  74.      */  
  75.     private ImageView getView(View view) {  
  76.         view.destroyDrawingCache();  
  77.         view.setDrawingCacheEnabled(true);  
  78.         Bitmap cache = Bitmap.createBitmap(view.getDrawingCache());  
  79.         view.setDrawingCacheEnabled(false);  
  80.         ImageView iv = new ImageView(this);  
  81.         iv.setImageBitmap(cache);  
  82.         return iv;  
  83.     }  
  84.     /**  
  85.      * 获取移动的VIEW,放入对应ViewGroup布局容器  
  86.      * @param viewGroup  
  87.      * @param view  
  88.      * @param initLocation  
  89.      * @return  
  90.      */  
  91.     private View getMoveView(ViewGroup viewGroup, View view, int[] initLocation) {  
  92.         int x = initLocation[0];  
  93.         int y = initLocation[1];  
  94.         viewGroup.addView(view);  
  95.         LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);  
  96.         mLayoutParams.leftMargin = x;  
  97.         mLayoutParams.topMargin = y;  
  98.         view.setLayoutParams(mLayoutParams);  
  99.         return view;  
  100.     }  
  101.   
  102.     /**  
  103.      * 创建移动的ITEM对应的ViewGroup布局容器  
  104.      * 用于存放我们移动的View  
  105.      */  
  106.     private ViewGroup getMoveViewGroup() {  
  107.         //window中最顶层的view  
  108.         ViewGroup moveViewGroup = (ViewGroup) getWindow().getDecorView();  
  109.         LinearLayout moveLinearLayout = new LinearLayout(this);  
  110.         moveLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));  
  111.         moveViewGroup.addView(moveLinearLayout);  
  112.         return moveLinearLayout;  
  113.     }  
  114.     /**  
  115.      * 点击ITEM移动动画  
  116.      *  
  117.      * @param moveView  
  118.      * @param startLocation  
  119.      * @param endLocation  
  120.      * @param moveChannel  
  121.      * @param clickGridView  
  122.      */  
  123.     private void MoveAnim(View moveView, int[] startLocation, int[] endLocation, final String moveChannel,  
  124.                           final GridView clickGridView, final boolean isUser) {  
  125.         int[] initLocation = new int[2];  
  126.         //获取传递过来的VIEW的坐标  
  127.         moveView.getLocationInWindow(initLocation);  
  128.         //得到要移动的VIEW,并放入对应的容器中  
  129.         final ViewGroup moveViewGroup = getMoveViewGroup();  
  130.         final View mMoveView = getMoveView(moveViewGroup, moveView, initLocation);  
  131.         //创建移动动画  
  132.         TranslateAnimation moveAnimation = new TranslateAnimation(  
  133.                 startLocation[0], endLocation[0], startLocation[1],  
  134.                 endLocation[1]);  
  135.         moveAnimation.setDuration(300L);//动画时间  
  136.         //动画配置  
  137.         AnimationSet moveAnimationSet = new AnimationSet(true);  
  138.         moveAnimationSet.setFillAfter(false);//动画效果执行完毕后,View对象不保留在终止的位置  
  139.         moveAnimationSet.addAnimation(moveAnimation);  
  140.         mMoveView.startAnimation(moveAnimationSet);  
  141.         moveAnimationSet.setAnimationListener(new Animation.AnimationListener() {  
  142.   
  143.             @Override  
  144.             public void onAnimationStart(Animation animation) {  
  145.             }  
  146.   
  147.             @Override  
  148.             public void onAnimationRepeat(Animation animation) {  
  149.             }  
  150.   
  151.             @Override  
  152.             public void onAnimationEnd(Animation animation) {  
  153.                 moveViewGroup.removeView(mMoveView);  
  154.                 // 判断点击的是DragGrid还是OtherGridView  
  155.                 if (isUser) {  
  156.                     mOtherAdapter.setVisible(true);  
  157.                     mOtherAdapter.notifyDataSetChanged();  
  158.                     mUserAdapter.remove();  
  159.                 } else {  
  160.                     mUserAdapter.setVisible(true);  
  161.                     mUserAdapter.notifyDataSetChanged();  
  162.                     mOtherAdapter.remove();  
  163.                 }  
  164.             }  
  165.         });  
  166.     }  
  167.   
  168.     @Override  
  169.     public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {  
  170.         switch (parent.getId()) {  
  171.             case R.id.userGridView:  
  172.                 //position为 0,1 的不可以进行任何操作  
  173.                 if (position != 0 && position != 1) {  
  174.                     final ImageView moveImageView = getView(view);  
  175.                     if (moveImageView != null) {  
  176.                         TextView newTextView = (TextView) view.findViewById(R.id.text_item);  
  177.                         final int[] startLocation = new int[2];  
  178.                         newTextView.getLocationInWindow(startLocation);  
  179.                         final String channel = ((OtherAdapter) parent.getAdapter()).getItem(position);//获取点击的频道内容  
  180.                         mOtherAdapter.setVisible(false);  
  181.                         //添加到最后一个  
  182.                         mOtherAdapter.addItem(channel);  
  183.                         new Handler().postDelayed(new Runnable() {  
  184.                             public void run() {  
  185.                                 try {  
  186.                                     int[] endLocation = new int[2];  
  187.                                     //获取终点的坐标  
  188.                                     mOtherGv.getChildAt(mOtherGv.getLastVisiblePosition()).getLocationInWindow(endLocation);  
  189.                                     MoveAnim(moveImageView, startLocation, endLocation, channel, mUserGv, true);  
  190.                                     mUserAdapter.setRemove(position);  
  191.                                 } catch (Exception localException) {  
  192.                                 }  
  193.                             }  
  194.                         }, 50L);  
  195.                     }  
  196.                 }  
  197.                 break;  
  198.             case R.id.otherGridView:  
  199.                 final ImageView moveImageView = getView(view);  
  200.                 if (moveImageView != null) {  
  201.                     TextView newTextView = (TextView) view.findViewById(R.id.text_item);  
  202.                     final int[] startLocation = new int[2];  
  203.                     newTextView.getLocationInWindow(startLocation);  
  204.                     final String channel = ((OtherAdapter) parent.getAdapter()).getItem(position);  
  205.                     mUserAdapter.setVisible(false);  
  206.                     //添加到最后一个  
  207.                     mUserAdapter.addItem(channel);  
  208.                     new Handler().postDelayed(new Runnable() {  
  209.                         public void run() {  
  210.                             try {  
  211.                                 int[] endLocation = new int[2];  
  212.                                 //获取终点的坐标  
  213.                                 mUserGv.getChildAt(mUserGv.getLastVisiblePosition()).getLocationInWindow(endLocation);  
  214.                                 MoveAnim(moveImageView, startLocation, endLocation, channel, mOtherGv,false);  
  215.                                 mOtherAdapter.setRemove(position);  
  216.                             } catch (Exception localException) {  
  217.                             }  
  218.                         }  
  219.                     }, 50L);  
  220.                 }  
  221.                 break;  
  222.             default:  
  223.                 break;  
  224.         }  
  225.     }  
  226. }  

适配器代码:
[html] view plain copy
  1. package com.test.drag;  
  2.   
  3. import android.content.Context;  
  4. import android.view.LayoutInflater;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.BaseAdapter;  
  8. import android.widget.TextView;  
  9.   
  10. import java.util.List;  
  11.   
  12. /**  
  13.  * Created by fuweiwei on 2016/1/8.  
  14.  */  
  15. public class OtherAdapter extends BaseAdapter {  
  16.   
  17.     private Context context;  
  18.     public List<String> channelList;  
  19.     private TextView item_text;  
  20.     /** 是否可见 在移动动画完毕之前不可见,动画完毕后可见*/  
  21.     boolean isVisible = true;  
  22.     /** 要删除的position */  
  23.     public int remove_position = -1;  
  24.     /** 是否是用户频道 */  
  25.     private boolean isUser = false;  
  26.   
  27.     public OtherAdapter(Context context, List<String> channelList ,boolean isUser) {  
  28.         this.context = context;  
  29.         this.channelList = channelList;  
  30.         this.isUser = isUser;  
  31.     }  
  32.   
  33.     @Override  
  34.     public int getCount() {  
  35.         return channelList == null ? 0 : channelList.size();  
  36.     }  
  37.   
  38.     @Override  
  39.     public String getItem(int position) {  
  40.         if (channelList != null && channelList.size() != 0) {  
  41.             return channelList.get(position);  
  42.         }  
  43.         return null;  
  44.     }  
  45.   
  46.     @Override  
  47.     public long getItemId(int position) {  
  48.         return position;  
  49.     }  
  50.   
  51.     @Override  
  52.     public View getView(int position, View convertView, ViewGroup parent) {  
  53.         View view = LayoutInflater.from(context).inflate(R.layout.adapter_mygridview_item, null);  
  54.         item_text = (TextView) view.findViewById(R.id.text_item);  
  55.         String channel = getItem(position);  
  56.         item_text.setText(channel);  
  57.         if(isUser){  
  58.             if ((position == 0) || (position == 1)){  
  59.                 item_text.setEnabled(false);  
  60.             }  
  61.         }  
  62.         if (!isVisible && (position == -1 + channelList.size())){  
  63.             item_text.setText("");  
  64.             item_text.setSelected(true);  
  65.             item_text.setEnabled(true);  
  66.         }  
  67.         if(remove_position == position){  
  68.             item_text.setText("");  
  69.         }  
  70.         return view;  
  71.     }  
  72.   
  73.     /** 获取频道列表 */  
  74.     public List<String> getChannnelLst() {  
  75.         return channelList;  
  76.     }  
  77.   
  78.     /** 添加频道列表 */  
  79.     public void addItem(String channel) {  
  80.         channelList.add(channel);  
  81.         notifyDataSetChanged();  
  82.     }  
  83.   
  84.     /** 设置删除的position */  
  85.     public void setRemove(int position) {  
  86.         remove_position = position;  
  87.         notifyDataSetChanged();  
  88.         // notifyDataSetChanged();  
  89.     }  
  90.   
  91.     /** 删除频道列表 */  
  92.     public void remove() {  
  93.         channelList.remove(remove_position);  
  94.         remove_position = -1;  
  95.         notifyDataSetChanged();  
  96.     }  
  97.     /** 设置频道列表 */  
  98.     public void setListDate(List<String> list) {  
  99.         channelList = list;  
  100.     }  
  101.   
  102.     /** 获取是否可见 */  
  103.     public boolean isVisible() {  
  104.         return isVisible;  
  105.     }  
  106.   
  107.     /** 设置是否可见 */  
  108.     public void setVisible(boolean visible) {  
  109.         isVisible = visible;  
  110.     }  
  111.   
  112. }  


至于之前说的很赞的交互体验是怎么实现的:

1、在点击gridview1的item时(也就是Imageview移动动画开始时),我们将所点击的内容置为空,

2、在gridview1点击的同时我们在gridview2的最后面加一个我们刚点击的item,但内容显示也是空

3、当imageview移动动画结束后,我们将gridview1所点击的item移除、重新刷新界面,而gridview2我们只要设置最后一个内容显示、刷新视图。

这样就能实现今天头条一样超赞的交互体验。

其它代码:

[html] view plain copy
  1. package com.test.drag.view;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.widget.GridView;  
  6.   
  7. /**  
  8.  * Created by fuweiwei on 2016/1/8.  
  9.  */  
  10. public class MyGridView extends GridView {  
  11.     public MyGridView(Context paramContext, AttributeSet paramAttributeSet) {  
  12.         super(paramContext, paramAttributeSet);  
  13.     }  
  14.   
  15.     @Override  
  16.     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  17.         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,  
  18.                 MeasureSpec.AT_MOST);  
  19.         super.onMeasure(widthMeasureSpec, expandSpec);  
  20.     }  
  21. }  

布局:
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" tools:context=".MainActivity">  
  5.     <ScrollView  
  6.         android:layout_width="fill_parent"  
  7.         android:layout_height="fill_parent"  
  8.         >  
  9.   
  10.         <LinearLayout  
  11.             android:id="@+id/subscribe_main_layout"  
  12.             android:layout_width="fill_parent"  
  13.             android:layout_height="wrap_content"  
  14.             android:orientation="vertical"  
  15.             android:paddingBottom="14.0dip" >  
  16.   
  17.             <LinearLayout  
  18.                 android:layout_width="fill_parent"  
  19.                 android:layout_height="wrap_content"  
  20.                 android:layout_marginLeft="10.0dip"  
  21.                 android:layout_marginTop="14.0dip"  
  22.                 android:gravity="bottom"  
  23.                 android:orientation="horizontal" >  
  24.   
  25.   
  26.                 <TextView  
  27.                     android:id="@+id/my_category_tip_text"  
  28.                     android:layout_width="wrap_content"  
  29.                     android:layout_height="wrap_content"  
  30.                     android:textSize="13sp"  
  31.                     android:text="我的频道"  
  32.                     />  
  33.             </LinearLayout>  
  34.   
  35.             <View  
  36.                 android:id="@+id/seperate_line"  
  37.                 android:layout_width="match_parent"  
  38.                 android:layout_marginTop="10dp"  
  39.                 android:layout_height="0.5dp"  
  40.                 android:layout_marginBottom="14.0dip"  
  41.                 android:background="@color/subscribe_item_drag_stroke" />  
  42.   
  43.             <com.test.drag.view.MyGridView  
  44.                 android:id="@+id/userGridView"  
  45.                 android:layout_width="fill_parent"  
  46.                 android:layout_height="wrap_content"  
  47.                 android:layout_marginLeft="14dip"  
  48.                 android:layout_marginRight="14dip"  
  49.                 android:gravity="center"  
  50.                 android:horizontalSpacing="14dip"  
  51.                 android:listSelector="@android:color/transparent"  
  52.                 android:numColumns="4"  
  53.                 android:scrollbars="vertical"  
  54.                 android:stretchMode="columnWidth"  
  55.                 android:verticalSpacing="14.0px" />  
  56.   
  57.             <View  
  58.                 android:id="@+id/seperate_line2"  
  59.                 android:layout_marginTop="10dp"  
  60.                 android:layout_width="match_parent"  
  61.                 android:layout_height="0.5dp"  
  62.                 android:background="@color/subscribe_item_drag_stroke"/>  
  63.   
  64.             <TextView  
  65.                 android:id="@+id/more_category_text"  
  66.                 android:layout_marginBottom="14.0dip"  
  67.                 android:layout_width="wrap_content"  
  68.                 android:layout_marginTop="10dp"  
  69.                 android:layout_height="wrap_content"  
  70.                 android:textSize="13sp"  
  71.                 android:layout_marginLeft="10.0dip"  
  72.                 android:text="更多频道" />  
  73.   
  74.             <com.test.drag.view.MyGridView  
  75.                 android:id="@+id/otherGridView"  
  76.                 android:layout_width="fill_parent"  
  77.                 android:layout_height="wrap_content"  
  78.                 android:layout_marginLeft="14dip"  
  79.                 android:layout_marginRight="14dip"  
  80.                 android:gravity="center"  
  81.                 android:horizontalSpacing="14dip"  
  82.                 android:listSelector="@android:color/transparent"  
  83.                 android:numColumns="4"  
  84.                 android:scrollbars="vertical"  
  85.                 android:stretchMode="columnWidth"  
  86.                 android:verticalSpacing="14.0px" />  
  87.         </LinearLayout>  
  88.     </ScrollView>  
  89.   
  90. </RelativeLayout> 
原创粉丝点击