Android實(shí)現(xiàn)今日頭條訂閱頻道效果
本文實(shí)例為大家分享了Android仿今日頭條訂閱頻道,供大家參考,具體內(nèi)容如下
源碼:Android實(shí)現(xiàn)今日頭條訂閱頻道
布局文件
<?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);
//添加動(dòng)畫
setLayoutTransition(new LayoutTransition());
//艦艇拖拽事件
setOnDragListener(this);
}
//根據(jù)傳遞進(jìn)來(lái)的數(shù)據(jù),動(dòng)態(tài)地添加控件
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);
//設(shè)置背景
textView.setBackgroundResource(R.drawable.selector_item_bg);
textView.setTextSize(25);
//將控件添加到頁(yè)面中
addView(textView);
LayoutParams layoutParams = (LayoutParams) textView.getLayoutParams();
//設(shè)置外邊距
layoutParams.setMargins(5, 5, 5, 5);
//監(jiān)聽textview點(diǎn)擊事件
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onItemClick(v);
}
}
});
//監(jiān)聽控件的長(zhǎng)按事件
textView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (!dragable) {
return true;
}
//把當(dāng)前長(zhǎng)按的控件變紅,并且有虛線
v.setBackgroundResource(R.drawable.selector_item_red_bg);
//開始拖拽控件
v.startDrag(null, new DragShadowBuilder(v), null, 0);
DragItem = v;
//獲取所有空間的矩形區(qū)域
getAllRect();
return true;
}
});
}
//獲取所有的矩形區(qū)域
private void getAllRect() {
rects = new ArrayList<>();
for (int i = 0; i < getChildCount(); i++) {
View view = getChildAt(i);
//獲取每個(gè)控件的坐標(biāo)點(diǎn),并存在集合中
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");
//拖拽移動(dòng)時(shí),位置發(fā)生變化
//根據(jù)當(dāng)前的位置,判斷應(yīng)該插入到哪個(gè)位置
int dragItemIndex = findDragItem(event);
if (dragItemIndex != -1 && DragItem != null && getChildAt(dragItemIndex) != DragItem) {
//先刪除原來(lái)的控件
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++) {
//如果鼠標(biāo)當(dāng)前的坐標(biāo)包含在某個(gè)控件的坐標(biāo)內(nèi)部,那就說(shuō)明,當(dāng)前在該空間內(nèi)部
if (rects.get(i).contains((int) event.getX(), (int) event.getY())) {
return i;
}
}
return -1;
}
public interface OnItemClickListener {
void onItemClick(View v);
}
// 設(shè)置控件是否可以拖拽
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;
//創(chuàng)建已訂閱和為訂閱的集合
private List<String> select = Arrays.asList("北京", "中國(guó)", "國(guó)際", "體育", "生活", "旅游", "科技", "軍事", "時(shí)尚", "財(cái)經(jīng)", "育兒", "汽車");
private List<String> unselect = Arrays.asList("娛樂(lè)", "服飾", "音樂(lè)", "視頻", "段子", "搞笑", "科學(xué)", "房產(chǎn)", "名站");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
//初始化數(shù)據(jù)
initData();
}
private void initData() {
gl1.setData(select);
gl2.setData(unselect);
gl1.setDragable(true);
//設(shè)置監(jiān)聽
gl1.setOnClickListener(new MyGridLayout.OnItemClickListener() {
@Override
public void onItemClick(View v) {
gl1.removeView(v);
//設(shè)置中間人
String s = ((TextView) v).getText().toString();
gl2.addItem(s);
}
});
gl2.setOnClickListener(new MyGridLayout.OnItemClickListener() {
@Override
public void onItemClick(View v) {
gl2.removeView(v);
//設(shè)置中間人
String s = ((TextView) v).getText().toString();
gl1.addItem(s);
}
});
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android自定義view仿今日頭條加載文字變色效果
- Android仿今日頭條頂部導(dǎo)航欄效果的實(shí)例代碼
- Android仿今日頭條多個(gè)fragment懶加載的實(shí)現(xiàn)
- Android使用RecyclerView實(shí)現(xiàn)今日頭條頻道管理功能
- Android studio導(dǎo)入項(xiàng)目的方法詳解(簡(jiǎn)單快速)
- Android 仿今日頭條簡(jiǎn)單的刷新效果實(shí)例代碼
- Android仿今日頭條APP實(shí)現(xiàn)下拉導(dǎo)航選擇菜單效果
- Android應(yīng)用中仿今日頭條App制作ViewPager指示器
- Android實(shí)現(xiàn)仿網(wǎng)易今日頭條等自定義頻道listview 或者grideview等item上移到另一個(gè)view中
- Android仿今日頭條滑動(dòng)頁(yè)面導(dǎo)航效果
相關(guān)文章
Android數(shù)據(jù)持久化之File機(jī)制分析
這篇文章主要介紹了Android數(shù)據(jù)持久化之File機(jī)制,較為詳細(xì)的分析了File機(jī)制的原理及Android使用File實(shí)現(xiàn)數(shù)據(jù)持久化的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
Android App數(shù)據(jù)格式Json解析方法和常見(jiàn)問(wèn)題
JSON數(shù)據(jù)格式,在Android中被廣泛運(yùn)用于客戶端和網(wǎng)絡(luò)(或者說(shuō)服務(wù)器)通信,非常有必要系統(tǒng)的了解學(xué)習(xí)。恰逢本人最近對(duì)json做了一個(gè)簡(jiǎn)單的學(xué)習(xí),特此總結(jié)一下,以饗各位2014-03-03
Android 網(wǎng)絡(luò)請(qǐng)求框架解析之okhttp與okio
HTTP是現(xiàn)代應(yīng)用常用的一種交換數(shù)據(jù)和媒體的網(wǎng)絡(luò)方式,高效地使用HTTP能讓資源加載更快,節(jié)省帶寬,OkHttp是一個(gè)高效的HTTP客戶端,下面這篇文章主要給大家介紹了關(guān)于OkHttp如何用于安卓網(wǎng)絡(luò)請(qǐng)求,需要的朋友可以參考下2021-10-10
Android中實(shí)現(xiàn)滑動(dòng)的七種方式總結(jié)
這篇文章主要介紹了Android中實(shí)現(xiàn)滑動(dòng)的七種方式總結(jié),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
Android自定義View實(shí)現(xiàn)價(jià)格區(qū)間選擇控件
這篇文章主要為大家詳細(xì)介紹了Android如何利用自定義View實(shí)現(xiàn)價(jià)格區(qū)間選擇控件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-11-11
Android 實(shí)現(xiàn)旋轉(zhuǎn)木馬的音樂(lè)效果
大家一定在百度音樂(lè)上在線聽過(guò)歌,有沒(méi)有注意到那個(gè)旋轉(zhuǎn)的唱片,本篇文章主要介紹在Android上如何實(shí)現(xiàn)這樣的功能,有需要的小伙伴可以參考下2016-07-07
Android?Studio中使用SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)登錄和注冊(cè)功能
SQLite是一款輕型的數(shù)據(jù)庫(kù),是遵守ACID的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),它包含在一個(gè)相對(duì)小的C庫(kù)中,下面這篇文章主要給大家介紹了關(guān)于Android?Studio中使用SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)登錄和注冊(cè)功能的相關(guān)資料,需要的朋友可以參考下2024-06-06
Android仿活動(dòng)時(shí)分秒倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了Android仿活動(dòng)時(shí)分秒倒計(jì)時(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02

