Android realizes today's headline subscription channel effect
- 2021-11-01 04:45:43
- OfStack
In this article, we share the Android subscription channel imitating today's headlines for your reference. The specific contents are as follows
Source code: Android realizes today's headline subscription channel
Layout file
<?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=" Subscribed channel " />
<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=" Unsubscribed channel " />
<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 Files and Selectors
<?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>
Custom layout
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);
// Add Animation
setLayoutTransition(new LayoutTransition());
// Ship towing incident
setOnDragListener(this);
}
// According to the data passed in, , Adding controls dynamically
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);
// Setting Background
textView.setBackgroundResource(R.drawable.selector_item_bg);
textView.setTextSize(25);
// Add controls to the page
addView(textView);
LayoutParams layoutParams = (LayoutParams) textView.getLayoutParams();
// Set outer margins
layoutParams.setMargins(5, 5, 5, 5);
// Eavesdropping textview Click event
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onItemClick(v);
}
}
});
// Listening for long press events of controls
textView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (!dragable) {
return true;
}
// Turns the currently long-pressed control red , And there is a dotted line
v.setBackgroundResource(R.drawable.selector_item_red_bg);
// Start dragging and dropping controls
v.startDrag(null, new DragShadowBuilder(v), null, 0);
DragItem = v;
// Gets the rectangular region of all spaces
getAllRect();
return true;
}
});
}
// Get all rectangular areas
private void getAllRect() {
rects = new ArrayList<>();
for (int i = 0; i < getChildCount(); i++) {
View view = getChildAt(i);
// Gets the coordinate points of each control , And exists in the collection
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");
// When dragging and moving, , Change of position
// Depending on the current location , Determine where to insert
int dragItemIndex = findDragItem(event);
if (dragItemIndex != -1 && DragItem != null && getChildAt(dragItemIndex) != DragItem) {
// Delete the original control first
removeView(DragItem);
// Drag and drop new controls to new locations
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 the current coordinates of the mouse are contained within the coordinates of a control , That means that , Is currently inside the space
if (rects.get(i).contains((int) event.getX(), (int) event.getY())) {
return i;
}
}
return -1;
}
public interface OnItemClickListener {
void onItemClick(View v);
}
// Sets whether the control can be dragged
public void setDragable(boolean dragable) {
this.dragable = dragable;
}
}
Core code
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;
// Creating Subscribed and Subscribed Collections
private List<String> select = Arrays.asList(" Beijing ", " China ", " International ", " Sports ", " Life ", " Tourism ", " Science and technology ", " Military ", " Fashion ", " Finance and Economics ", " Parenting ", " Automobile ");
private List<String> unselect = Arrays.asList(" Entertainment ", " Clothing ", " Music ", " Video ", " Jokes ", " Funny ", " Science ", " Real estate ", " Famous station ");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
// Initialization data
initData();
}
private void initData() {
gl1.setData(select);
gl2.setData(unselect);
gl1.setDragable(true);
// Set up listening
gl1.setOnClickListener(new MyGridLayout.OnItemClickListener() {
@Override
public void onItemClick(View v) {
gl1.removeView(v);
// Set up a middleman
String s = ((TextView) v).getText().toString();
gl2.addItem(s);
}
});
gl2.setOnClickListener(new MyGridLayout.OnItemClickListener() {
@Override
public void onItemClick(View v) {
gl2.removeView(v);
// Set up a middleman
String s = ((TextView) v).getText().toString();
gl1.addItem(s);
}
});
}
}