Android实现今日头条订阅频道效果
作者:常利兵 时间:2021-10-29 23:30:00
本文实例为大家分享了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);
}
});
}
}
来源:https://blog.csdn.net/qq_32890771/article/details/72935087
标签:Android,今日头条
0
投稿
猜你喜欢
Java 函数式编程梳理
2023-01-17 04:16:34
java并发JUC工具包AtomicInteger原子整型语法基础
2023-10-05 14:16:47
基于C#实现磁性吸附窗体
2022-09-04 07:04:51
Java多线程ThreadAPI详细介绍
2023-04-14 12:19:18
Android进阶从字节码插桩技术了解美团热修复实例详解
2022-05-27 18:06:07
SpringMVC数据响应详细介绍
2023-09-24 04:12:41
Android SQLite数据库版本升级的管理实现
2023-11-09 23:58:44
c#预处理指令分析
2021-07-23 10:35:00
解析HikariCP一百行代码轻松掌握多线程
2023-03-21 19:11:00
Spring AOP如何整合redis(注解方式)实现缓存统一管理详解
2023-11-19 06:09:27
C++利用ImGUI绘制D3D外部菜单
2023-11-18 12:35:00
mybatis-plus的批量新增/批量更新以及问题
2022-10-28 04:44:16
Android 使用压缩纹理的方案
2023-09-26 12:25:23
Android实现腾讯新闻的新闻类别导航效果
2023-07-29 04:17:46
java泛型详解
2023-06-07 23:54:47
Java包装类的概述与应用
2022-03-14 07:50:43
docker 的java编译环境构建详细介绍
2023-02-10 04:08:30
Android OpenGL仿自如APP裸眼3D效果详解
2023-12-23 21:37:05
Spring Boot 自动配置的实现
2023-07-21 18:18:55
Android Glide图片加载(加载监听、加载动画)
2022-08-27 08:31:24