Android实现今日头条订阅频道效果
程序员文章站
2022-06-23 15:04:42
本文实例为大家分享了android仿今日头条订阅频道,供大家参考,具体内容如下
源码:android实现今日头条订阅频道
布局文件
<...
本文实例为大家分享了android仿今日头条订阅频道,供大家参考,具体内容如下
布局文件
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.a2_.mainactivity"> <textview android:background="@android:color/holo_blue_dark" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="已订阅频道" /> <com.example.a2_.mygridlayout android:id="@+id/gl1" android:columncount="4" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> </com.example.a2_.mygridlayout> <textview android:gravity="center_horizontal" android:background="@android:color/darker_gray" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="未订阅频道" /> <com.example.a2_.mygridlayout android:columncount="4" android:id="@+id/gl2" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> </com.example.a2_.mygridlayout> </linearlayout>
shape文件和选择器
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp"/> <stroke android:color="#000" android:width="1dp"/> </shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp"/> <stroke android:color="#ff0000" android:dashgap="1dp" android:dashwidth="3dp" android:width="1dp"/> </shape>
自定义布局
package com.example.a2_; import android.animation.layouttransition; import android.content.context; import android.graphics.color; import android.graphics.rect; import android.util.attributeset; import android.view.dragevent; import android.view.view; import android.widget.gridlayout; import android.widget.textview; import java.util.arraylist; import java.util.list; /** * created by administrator on 2017.06.08.0008. */ public class mygridlayout extends gridlayout implements view.ondraglistener { private onitemclicklistener listener; private list<rect> rects; private view dragitem = null; private boolean dragable; public mygridlayout(context context, attributeset attrs) { super(context, attrs); //添加动画 setlayouttransition(new layouttransition()); //舰艇拖拽事件 setondraglistener(this); } //根据传递进来的数据,动态地添加控件 public void setdata(list<string> list) { for (int i = 0; i < list.size(); i++) { additem(list.get(i)); } } public void additem(string s) { final textview textview = new textview(getcontext()); textview.settext(s); textview.settextcolor(color.black); textview.setpadding(15, 5, 15, 5); //设置背景 textview.setbackgroundresource(r.drawable.selector_item_bg); textview.settextsize(25); //将控件添加到页面中 addview(textview); layoutparams layoutparams = (layoutparams) textview.getlayoutparams(); //设置外边距 layoutparams.setmargins(5, 5, 5, 5); //监听textview点击事件 textview.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (listener != null) { listener.onitemclick(v); } } }); //监听控件的长按事件 textview.setonlongclicklistener(new onlongclicklistener() { @override public boolean onlongclick(view v) { if (!dragable) { return true; } //把当前长按的控件变红,并且有虚线 v.setbackgroundresource(r.drawable.selector_item_red_bg); //开始拖拽控件 v.startdrag(null, new dragshadowbuilder(v), null, 0); dragitem = v; //获取所有空间的矩形区域 getallrect(); return true; } }); } //获取所有的矩形区域 private void getallrect() { rects = new arraylist<>(); for (int i = 0; i < getchildcount(); i++) { view view = getchildat(i); //获取每个控件的坐标点,并存在集合中 rects.add(new rect(view.getleft(), view.gettop(), view.getright(), view.getbottom())); } } public void setonclicklistener(onitemclicklistener listener) { this.listener = listener; } @override public boolean ondrag(view v, dragevent event) { if (!dragable) { return true; } switch (event.getaction()) { case dragevent.action_drag_started: system.out.println("action_drag_started"); break; case dragevent.action_drag_entered: system.out.println("action_drag_entered"); break; case dragevent.action_drag_exited: system.out.println("action_drag_exited"); break; case dragevent.action_drag_location: system.out.println("action_drag_location"); //拖拽移动时,位置发生变化 //根据当前的位置,判断应该插入到哪个位置 int dragitemindex = finddragitem(event); if (dragitemindex != -1 && dragitem != null && getchildat(dragitemindex) != dragitem) { //先删除原来的控件 removeview(dragitem); //吧新的控件拖拽到新的位置 addview(dragitem, dragitemindex); } break; case dragevent.action_drop: system.out.println("action_drop"); break; case dragevent.action_drag_ended: system.out.println("action_drag_ended"); if (dragitem != null) { dragitem.setbackgroundresource(r.drawable.selector_item_bg); } break; } return true; } private int finddragitem(dragevent event) { if (rects == null) { return -1; } for (int i = 0; i < rects.size(); i++) { //如果鼠标当前的坐标包含在某个控件的坐标内部,那就说明,当前在该空间内部 if (rects.get(i).contains((int) event.getx(), (int) event.gety())) { return i; } } return -1; } public interface onitemclicklistener { void onitemclick(view v); } // 设置控件是否可以拖拽 public void setdragable(boolean dragable) { this.dragable = dragable; } }
核心代码
package com.example.a2_; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.linearlayout; import android.widget.textview; import java.util.arrays; import java.util.list; import butterknife.butterknife; import butterknife.injectview; public class mainactivity extends activity { @injectview(r.id.gl1) mygridlayout gl1; @injectview(r.id.gl2) mygridlayout gl2; @injectview(r.id.activity_main) linearlayout activitymain; //创建已订阅和为订阅的集合 private list<string> select = arrays.aslist("北京", "中国", "国际", "体育", "生活", "旅游", "科技", "军事", "时尚", "财经", "育儿", "汽车"); private list<string> unselect = arrays.aslist("娱乐", "服饰", "音乐", "视频", "段子", "搞笑", "科学", "房产", "名站"); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); butterknife.inject(this); //初始化数据 initdata(); } private void initdata() { gl1.setdata(select); gl2.setdata(unselect); gl1.setdragable(true); //设置监听 gl1.setonclicklistener(new mygridlayout.onitemclicklistener() { @override public void onitemclick(view v) { gl1.removeview(v); //设置中间人 string s = ((textview) v).gettext().tostring(); gl2.additem(s); } }); gl2.setonclicklistener(new mygridlayout.onitemclicklistener() { @override public void onitemclick(view v) { gl2.removeview(v); //设置中间人 string s = ((textview) v).gettext().tostring(); gl1.additem(s); } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: mysql设置不区分表名大小写的实例