您好,登錄后才能下訂單哦!
這篇文章主要介紹了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); } }); } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Android如何實現今日頭條訂閱頻道效果”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。