频道管理(ChannelManager)之简易版

这篇博客展示了如何在Android中实现简单的频道管理,包括我的频道和其它频道之间的数据交换,但不包含拖拽移动功能。通过自定义MyGridView控件、适配器以及其他相关代码,实现在两个GridView间点击移动频道条目的效果。
[html]  view plain  copy
  1. <span style="font-size:14px;">此篇幅是关于频道管理的简单效果展示,主要实现了我的频道和其他频道的点击相互数据交换效果,里面没有涉及拖拽移动效果。  
  2.   
  3.   
  4.   
  5. 1.activity_main.xml  的布局 :  
  6.   
  7. <?xml version="1.0" encoding="utf-8"?>  
  8. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  9.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  10.     xmlns:tools="http://schemas.android.com/tools"  
  11.     android:layout_width="match_parent"  
  12.     android:layout_height="match_parent"  
  13.     tools:context="likaihu.com.baway.yuekao_a.MainActivity">  
  14.   
  15.   
  16.     <ScrollView  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="fill_parent"  
  19.         >  
  20.   
  21.         <LinearLayout  
  22.             android:id="@+id/subscribe_main_layout"  
  23.             android:layout_width="fill_parent"  
  24.             android:layout_height="wrap_content"  
  25.             android:orientation="vertical"  
  26.             android:paddingBottom="14.0dip" >  
  27.   
  28.             <LinearLayout  
  29.                 android:layout_width="fill_parent"  
  30.                 android:layout_height="wrap_content"  
  31.                 android:layout_marginLeft="10.0dip"  
  32.                 android:layout_marginTop="14.0dip"  
  33.                 android:gravity="bottom"  
  34.                 android:orientation="horizontal" >  
  35.   
  36.   
  37.                 <TextView  
  38.                     android:id="@+id/my_category_tip_text"  
  39.                     android:layout_width="wrap_content"  
  40.                     android:layout_height="wrap_content"  
  41.                     android:textSize="13sp"  
  42.                     android:text="我的频道"  
  43.                     />  
  44.             </LinearLayout>  
  45.   
  46.             <View  
  47.                 android:id="@+id/seperate_line"  
  48.                 android:layout_width="match_parent"  
  49.                 android:layout_marginTop="10dp"  
  50.                 android:layout_height="0.5dp"  
  51.                 android:layout_marginBottom="14.0dip"  
  52.                 android:background="#ABADAC"  
  53.              />  
  54.   
  55.             <likaihu.com.MyGridView  
  56.                 android:id="@+id/userGridView"  
  57.                 android:layout_width="fill_parent"  
  58.                 android:layout_height="wrap_content"  
  59.                 android:layout_marginLeft="14dip"  
  60.                 android:layout_marginRight="14dip"  
  61.                 android:gravity="center"  
  62.                 android:horizontalSpacing="14dip"  
  63.                 android:numColumns="4"  
  64.                 android:scrollbars="vertical"  
  65.                 android:stretchMode="columnWidth"  
  66.                 android:verticalSpacing="14.0px" />  
  67.   
  68.   
  69.   
  70.             <View  
  71.                 android:id="@+id/seperate_line2"  
  72.                 android:layout_marginTop="10dp"  
  73.                 android:layout_width="match_parent"  
  74.                 android:layout_height="0.5dp"  
  75.                 android:background="#ABADAC"  
  76.                 />  
  77.   
  78.             <TextView  
  79.                 android:id="@+id/more_category_text"  
  80.                 android:layout_marginBottom="14.0dip"  
  81.                 android:layout_width="wrap_content"  
  82.                 android:layout_marginTop="10dp"  
  83.                 android:layout_height="wrap_content"  
  84.                 android:textSize="13sp"  
  85.                 android:layout_marginLeft="10.0dip"  
  86.                 android:text="更多频道" />  
  87.   
  88.             <likaihu.com.MyGridView  
  89.                 android:id="@+id/otherGridView"  
  90.                 android:layout_width="fill_parent"  
  91.                 android:layout_height="wrap_content"  
  92.                 android:layout_marginLeft="14dip"  
  93.                 android:layout_marginRight="14dip"  
  94.                 android:gravity="center"  
  95.                 android:horizontalSpacing="14dip"  
  96.                 android:numColumns="4"  
  97.                 android:scrollbars="vertical"  
  98.                 android:stretchMode="columnWidth"  
  99.                 android:verticalSpacing="14.0px" />  
  100.         </LinearLayout>  
  101.     </ScrollView>  
  102.   
  103. </RelativeLayout>  
  104.   
  105.   
  106. 2.适配器条目布局 adapter_mygridview_item.xml:  
  107.   
  108. <?xml version="1.0" encoding="utf-8"?>  
  109. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  110.     android:orientation="vertical" android:layout_width="match_parent"  
  111.     android:layout_height="match_parent">  
  112.   
  113.     <TextView  
  114.         android:background="#ADD3B2"  
  115.         android:layout_marginTop="10dp"  
  116.         android:layout_width="wrap_content"  
  117.         android:layout_height="wrap_content"  
  118.         android:layout_gravity="center"  
  119.         android:textSize="20sp"  
  120.         android:text="新闻"  
  121.         android:id="@+id/text_item"  
  122.         android:paddingLeft="5dp"  
  123.         />  
  124.   
  125. </LinearLayout>  
  126.   
  127.   
  128. 3.自定义的MyGridView控件 初始化代码如下:  
  129.         
  130.     import android.content.Context;    
  131.     import android.util.AttributeSet;    
  132.     import android.widget.GridView;    
  133.   
  134.   
  135.     public class MyGridView extends GridView {    
  136.         public MyGridView(Context paramContext, AttributeSet paramAttributeSet) {    
  137.             super(paramContext, paramAttributeSet);    
  138.         }    
  139.         
  140.         @Override    
  141.         public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    
  142.             int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,    
  143.                     MeasureSpec.AT_MOST);    
  144.             super.onMeasure(widthMeasureSpec, expandSpec);    
  145.         }    
  146.     }    
  147.   
  148.   
  149. 4.适配器的代码如下  
  150.         
  151.     import android.content.Context;    
  152.     import android.view.LayoutInflater;    
  153.     import android.view.View;    
  154.     import android.view.ViewGroup;    
  155.     import android.widget.BaseAdapter;    
  156.     import android.widget.TextView;    
  157.         
  158.     import java.util.List;    
  159.         
  160.   
  161.     public class OtherAdapter extends BaseAdapter {    
  162.         
  163.         private Context context;    
  164.         public List<String> channelList;    
  165.         private TextView item_text;    
  166.         /** 是否可见 在移动动画完毕之前不可见,动画完毕后可见*/    
  167.         boolean isVisible = true;    
  168.         /** 要删除的position */    
  169.         public int remove_position = -1;    
  170.         /** 是否是用户频道 */    
  171.         private boolean isUser = false;    
  172.         
  173.         public OtherAdapter(Context context, List<String> channelList ,boolean isUser) {    
  174.             this.context = context;    
  175.             this.channelList = channelList;    
  176.             this.isUser = isUser;    
  177.         }    
  178.         
  179.         @Override    
  180.         public int getCount() {    
  181.             return channelList == null ? 0 : channelList.size();    
  182.         }    
  183.         
  184.         @Override    
  185.         public String getItem(int position) {    
  186.             if (channelList != null && channelList.size() != 0) {    
  187.                 return channelList.get(position);    
  188.             }    
  189.             return null;    
  190.         }    
  191.         
  192.         @Override    
  193.         public long getItemId(int position) {    
  194.             return position;    
  195.         }    
  196.         
  197.         @Override    
  198.         public View getView(int position, View convertView, ViewGroup parent) {    
  199.             View view = LayoutInflater.from(context).inflate(R.layout.adapter_mygridview_item, null);    
  200.             item_text = (TextView) view.findViewById(R.id.text_item);    
  201.             String channel = getItem(position);    
  202.             item_text.setText(channel);    
  203.             if(isUser){    
  204.                 if ((position == 0) || (position == 1)){    
  205.                     item_text.setEnabled(false);    
  206.                 }    
  207.             }    
  208.             if (!isVisible && (position == -1 + channelList.size())){    
  209.                 item_text.setText("");    
  210.                 item_text.setSelected(true);    
  211.                 item_text.setEnabled(true);    
  212.             }    
  213.             if(remove_position == position){    
  214.                 item_text.setText("");    
  215.             }    
  216.             return view;    
  217.         }    
  218.         
  219.         /** 获取频道列表 */    
  220.         public List<String> getChannnelLst() {    
  221.             return channelList;    
  222.         }    
  223.         
  224.         /** 添加频道列表 */    
  225.         public void addItem(String channel) {    
  226.             channelList.add(channel);    
  227.             notifyDataSetChanged();    
  228.         }    
  229.         
  230.         /** 设置删除的position */    
  231.         public void setRemove(int position) {    
  232.             remove_position = position;    
  233.             notifyDataSetChanged();    
  234.             // notifyDataSetChanged();    
  235.         }    
  236.         
  237.         /** 删除频道列表 */    
  238.         public void remove() {    
  239.             channelList.remove(remove_position);    
  240.             remove_position = -1;    
  241.             notifyDataSetChanged();    
  242.         }    
  243.         /** 设置频道列表 */    
  244.         public void setListDate(List<String> list) {    
  245.             channelList = list;    
  246.         }    
  247.         
  248.         /** 获取是否可见 */    
  249.         public boolean isVisible() {    
  250.             return isVisible;    
  251.         }    
  252.         
  253.         /** 设置是否可见 */    
  254.         public void setVisible(boolean visible) {    
  255.             isVisible = visible;    
  256.         }    
  257.         
  258.     }    
  259.   
  260.   
  261.   
  262. 5.MainActivity 界面的代码如下:  
  263.   
  264. import android.support.v7.app.AlertDialog;  
  265. import android.support.v7.app.AppCompatActivity;  
  266. import android.os.Bundle;  
  267.   
  268. import android.graphics.Bitmap;  
  269. import android.os.Bundle;  
  270. import android.os.Handler;  
  271. import android.support.v7.app.AppCompatActivity;  
  272. import android.view.LayoutInflater;  
  273. import android.view.View;  
  274. import android.view.ViewGroup;  
  275. import android.view.animation.Animation;  
  276. import android.view.animation.AnimationSet;  
  277. import android.view.animation.TranslateAnimation;  
  278. import android.widget.AdapterView;  
  279. import android.widget.AdapterView.OnItemClickListener;  
  280. import android.widget.GridView;  
  281. import android.widget.ImageView;  
  282. import android.widget.LinearLayout;  
  283. import android.widget.TextView;  
  284.   
  285.   
  286.   
  287. import java.util.ArrayList;  
  288. import java.util.List;  
  289. public class MainActivity extends AppCompatActivity implements OnItemClickListener, AdapterView.OnItemLongClickListener {  
  290.   
  291.     private MyGridView mUserGv, mOtherGv;  
  292.     private List<String> mUserList = new ArrayList<>();  
  293.     private List<String> mOtherList = new ArrayList<>();  
  294.     private OtherAdapter mUserAdapter, mOtherAdapter;  
  295.   
  296.     @Override  
  297.     protected void onCreate(Bundle savedInstanceState) {  
  298.         super.onCreate(savedInstanceState);  
  299.         setContentView(R.layout.activity_main);  
  300.   
  301.   
  302.         initView();  
  303.   
  304.     }  
  305.   
  306.     public void initView() {  
  307.         mUserGv = (MyGridView) findViewById(R.id.userGridView);  
  308.         mOtherGv = (MyGridView) findViewById(R.id.otherGridView);  
  309.         mUserList.add("推荐");  
  310.         mUserList.add("热点");  
  311.         mUserList.add("上海");  
  312.         mUserList.add("时尚");  
  313.         mUserList.add("科技");  
  314.         mUserList.add("体育");  
  315.         mUserList.add("军事");  
  316.         mUserList.add("财经");  
  317.         mUserList.add("网络");  
  318.         mOtherList.add("汽车");  
  319.         mOtherList.add("房产");  
  320.         mOtherList.add("社会");  
  321.         mOtherList.add("情感");  
  322.         mOtherList.add("女人");  
  323.         mOtherList.add("旅游");  
  324.         mOtherList.add("健康");  
  325.         mOtherList.add("美女");  
  326.         mOtherList.add("游戏");  
  327.         mOtherList.add("数码");  
  328.         mOtherList.add("娱乐");  
  329.         mOtherList.add("探索");  
  330.         mUserAdapter = new OtherAdapter(this, mUserList,true);  
  331.         mOtherAdapter = new OtherAdapter(this, mOtherList,false);  
  332.         mUserGv.setAdapter(mUserAdapter);  
  333.         mOtherGv.setAdapter(mOtherAdapter);  
  334.         mUserGv.setOnItemClickListener(this);  
  335.         mOtherGv.setOnItemClickListener(this);  
  336.   
  337.         mUserGv.setOnItemLongClickListener(this);  
  338.     }  
  339.   
  340.     /**  
  341.      *获取点击的Item的对应View,  
  342.      *因为点击的Item已经有了自己归属的父容器MyGridView,所有我们要是有一个ImageView来代替Item移动  
  343.      * @param view  
  344.      * @return  
  345.      */  
  346.     private ImageView getView(View view) {  
  347.         view.destroyDrawingCache();  
  348.         view.setDrawingCacheEnabled(true);  
  349.         Bitmap cache = Bitmap.createBitmap(view.getDrawingCache());  
  350.         view.setDrawingCacheEnabled(false);  
  351.         ImageView iv = new ImageView(this);  
  352.         iv.setImageBitmap(cache);  
  353.         return iv;  
  354.     }  
  355.     /**  
  356.      * 获取移动的VIEW,放入对应ViewGroup布局容器  
  357.      * @param viewGroup  
  358.      * @param view  
  359.      * @param initLocation  
  360.      * @return  
  361.      */  
  362.     private View getMoveView(ViewGroup viewGroup, View view, int[] initLocation) {  
  363.         int x = initLocation[0];  
  364.         int y = initLocation[1];  
  365.         viewGroup.addView(view);  
  366.         LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);  
  367.         mLayoutParams.leftMargin = x;  
  368.         mLayoutParams.topMargin = y;  
  369.         view.setLayoutParams(mLayoutParams);  
  370.         return view;  
  371.     }  
  372.   
  373.     /**  
  374.      * 创建移动的ITEM对应的ViewGroup布局容器  
  375.      * 用于存放我们移动的View  
  376.      */  
  377.     private ViewGroup getMoveViewGroup() {  
  378.         //window中最顶层的view  
  379.         ViewGroup moveViewGroup = (ViewGroup) getWindow().getDecorView();  
  380.         LinearLayout moveLinearLayout = new LinearLayout(this);  
  381.         moveLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));  
  382.         moveViewGroup.addView(moveLinearLayout);  
  383.         return moveLinearLayout;  
  384.     }  
  385.     /**  
  386.      * 点击ITEM移动动画  
  387.      *  
  388.      * @param moveView  
  389.      * @param startLocation  
  390.      * @param endLocation  
  391.      * @param moveChannel  
  392.      * @param clickGridView  
  393.      */  
  394.     private void MoveAnim(View moveView, int[] startLocation, int[] endLocation, final String moveChannel,  
  395.                           final GridView clickGridView, final boolean isUser) {  
  396.         int[] initLocation = new int[2];  
  397.         //获取传递过来的VIEW的坐标  
  398.         moveView.getLocationInWindow(initLocation);  
  399.         //得到要移动的VIEW,并放入对应的容器中  
  400.         final ViewGroup moveViewGroup = getMoveViewGroup();  
  401.         final View mMoveView = getMoveView(moveViewGroup, moveView, initLocation);  
  402.         //创建移动动画  
  403.         TranslateAnimation moveAnimation = new TranslateAnimation(  
  404.                 startLocation[0], endLocation[0], startLocation[1],  
  405.                 endLocation[1]);  
  406.         moveAnimation.setDuration(10);//动画时间  
  407.         //动画配置  
  408.         AnimationSet moveAnimationSet = new AnimationSet(true);  
  409.         moveAnimationSet.setFillAfter(false);//动画效果执行完毕后,View对象不保留在终止的位置  
  410.         moveAnimationSet.addAnimation(moveAnimation);  
  411.         mMoveView.startAnimation(moveAnimationSet);  
  412.         moveAnimationSet.setAnimationListener(new Animation.AnimationListener() {  
  413.   
  414.             @Override  
  415.             public void onAnimationStart(Animation animation) {  
  416.             }  
  417.   
  418.             @Override  
  419.             public void onAnimationRepeat(Animation animation) {  
  420.             }  
  421.   
  422.             @Override  
  423.             public void onAnimationEnd(Animation animation) {  
  424.                 moveViewGroup.removeView(mMoveView);  
  425.                 // 判断点击的是DragGrid还是OtherGridView  
  426.                 if (isUser) {  
  427.                     mOtherAdapter.setVisible(true);  
  428.                     mOtherAdapter.notifyDataSetChanged();  
  429.                     mUserAdapter.remove();  
  430.                 } else {  
  431.                     mUserAdapter.setVisible(true);  
  432.                     mUserAdapter.notifyDataSetChanged();  
  433.                     mOtherAdapter.remove();  
  434.                 }  
  435.             }  
  436.         });  
  437.     }  
  438.   
  439.     @Override  
  440.     public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {  
  441.         switch (parent.getId()) {  
  442.             case R.id.userGridView:  
  443.                 //position为 0,1 的不可以进行任何操作  
  444.                 if (position != 0 && position != 1) {  
  445.                     final ImageView moveImageView = getView(view);  
  446.                     if (moveImageView != null) {  
  447.                         TextView newTextView = (TextView) view.findViewById(R.id.text_item);  
  448.                         final int[] startLocation = new int[2];  
  449.                         newTextView.getLocationInWindow(startLocation);  
  450.                         final String channel = ((OtherAdapter) parent.getAdapter()).getItem(position);//获取点击的频道内容  
  451.                         mOtherAdapter.setVisible(false);  
  452.                         //添加到最后一个  
  453.                         mOtherAdapter.addItem(channel);  
  454.                         new Handler().postDelayed(new Runnable() {  
  455.                             public void run() {  
  456.                                 try {  
  457.                                     int[] endLocation = new int[2];  
  458.                                     //获取终点的坐标  
  459.                                     mOtherGv.getChildAt(mOtherGv.getLastVisiblePosition()).getLocationInWindow(endLocation);  
  460.                                     MoveAnim(moveImageView, startLocation, endLocation, channel, mUserGv, true);  
  461.                                     mUserAdapter.setRemove(position);  
  462.                                 } catch (Exception localException) {  
  463.                                 }  
  464.                             }  
  465.                         }, 50L);  
  466.                     }  
  467.                 }  
  468.                 break;  
  469.             case R.id.otherGridView:  
  470.                 final ImageView moveImageView = getView(view);  
  471.                 if (moveImageView != null) {  
  472.                     TextView newTextView = (TextView) view.findViewById(R.id.text_item);  
  473.                     final int[] startLocation = new int[2];  
  474.                     newTextView.getLocationInWindow(startLocation);  
  475.                     final String channel = ((OtherAdapter) parent.getAdapter()).getItem(position);  
  476.                     mUserAdapter.setVisible(false);  
  477.                     //添加到最后一个  
  478.                     mUserAdapter.addItem(channel);  
  479.                     new Handler().postDelayed(new Runnable() {  
  480.                         public void run() {  
  481.                             try {  
  482.                                 int[] endLocation = new int[2];  
  483.                                 //获取终点的坐标  
  484.                                 mUserGv.getChildAt(mUserGv.getLastVisiblePosition()).getLocationInWindow(endLocation);  
  485.                                 MoveAnim(moveImageView, startLocation, endLocation, channel, mOtherGv,false);  
  486.                                 mOtherAdapter.setRemove(position);  
  487.                             } catch (Exception localException) {  
  488.                             }  
  489.                         }  
  490.                     }, 50L);  
  491.                 }  
  492.                 break;  
  493.             default:  
  494.                 break;  
  495.         }  
  496.     }  
  497.   
  498.     @Override  
  499.     public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {  
  500.   
  501.         //创建alertDialog  
  502.         AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  503.    //创建一个自定义View  
  504.         LayoutInflater inflater = LayoutInflater.from(this);  
  505.         //View view = inflater.inflate(R.layout.xxx, null);  
  506.         builder.setView(view);  
  507.         //创建一个AlertDialog对象  
  508.         AlertDialog dialog = builder.create();  
  509.         dialog.show();  
  510.         return false;  
  511.   
  512.     }  
  513. }</span>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值