本文实例为大家分享了Android仿今日头条订阅频道,供大家参考,具体内容如下
源码:Android实现今日头条订阅频道
布局文件
<&#63;xml version="1.0" encoding="utf-8"&#63;>
shape文件和选择器
<&#63;xml version="1.0" encoding="utf-8"&#63;>
<&#63;xml version="1.0" encoding="utf-8"&#63;>
自定义布局
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 Listrects; 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 list) { for (int i = 0; i (); for (int i = 0; i
核心代码
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 Listselect = Arrays.asList("北京", "中国", "国际", "体育", "生活", "旅游", "科技", "军事", "时尚", "财经", "育儿", "汽车"); private List 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); } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。