在開發systemUi過程當中,要求像小米的開關排序進行拖拽添加,對控制中心的每一個按鈕添加更新;通過不停的探索,最終實現出結果; 下面咱們來看實現; 咱們先看values下的xml的實現: colors.xmljava
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<color name="itemColorNormal">#FFFFFF</color>
<color name="itemColorPressed">#F5F5F5</color>
</resources>
複製代碼
strings.xmlandroid
<resources>
....
<string name="app_name">MyDragListView</string>
<string name="drag_tag_text">上方%1$d個開關會出如今通知欄中</string>
</resources>
複製代碼
styles.xmlbash
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
<!-- Customize your theme here. -->
<!--<item name="colorPrimary">@color/colorPrimary</item>-->
<!--<item name="colorPrimaryDark">@color/colorPrimaryDark</item>-->
<!--<item name="colorAccent">@color/colorAccent</item>-->
</style>
</resources>
複製代碼
在工程目錄下建立一個widget包,建立java文件; LDragItemClass.java:app
package application.com.drag.widget;
public class LDragItemClass {
public String name;
}
複製代碼
LDragListView.javaide
package application.com.drag.widget;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import application.com.drag.R;
public class LDragAdapter extends BaseAdapter {
private Context mContext;
private int finalAdded = 0;
private List<LDragItemClass> mAddedItems;
private List<LDragItemClass> mNotAddedItems;
public static final int TYPE_TAG = 0;
public static final int TYPE_ITEM = 1;
private String tag = "";
public LDragAdapter(Context context, ArrayList<LDragItemClass> added, ArrayList<LDragItemClass> notAdded) {
this.mContext = context;
mAddedItems = added;
mNotAddedItems = notAdded;
finalAdded = mAddedItems.size();
tag = String.format(context.getResources().getString(R.string.drag_tag_text), finalAdded);
}
@Override
public int getCount() {
return 1+finalAdded+mNotAddedItems.size();
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if(position==finalAdded){
return TYPE_TAG;
}else {
return TYPE_ITEM;
}
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder=null;
if (convertView==null){
if (getItemViewType(position)==TYPE_ITEM){
convertView = LayoutInflater.from(mContext).inflate(R.layout.drag_list_item, null);
viewHolder = new ViewHolder(convertView);
}else {
convertView = LayoutInflater.from(mContext).inflate(R.layout.drag_list_item_tag, null);
viewHolder = new ViewHolder(convertView);
}
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
LDragItemClass itemObj = getLDragItem(position);
if(getItemViewType(position)==TYPE_ITEM){
viewHolder.nameView.setText(itemObj.name);
}else {
viewHolder.nameView.setText(tag);
viewHolder.nameView.setEnabled(false);
}
return convertView;
}
class ViewHolder{
TextView nameView;
ImageView iconView;
public ViewHolder(View view){
nameView = (TextView)view.findViewById(R.id.drag_list_item_text);
view.setTag(this);
}
}
private LDragItemClass getLDragItem(int position){
if (position==finalAdded){
return null;
}
else if(position<finalAdded){
//add
return mAddedItems.get(position);
}else{
//not add
return mNotAddedItems.get(position-finalAdded-1);
}
}
public boolean isTag(int pos){
if( getItemViewType(pos)==TYPE_TAG){
return true;
}
return false;
}
public boolean canDrag(int pos, View dragView, int x, int y){
if( getItemViewType(pos)==TYPE_TAG){
return false;
}
else {
if (dragView == null) {
return false;
}
View dragger = dragView.findViewById(R.id.drag_list_item_drag);
if (dragger == null || dragger.getVisibility() != View.VISIBLE) {
return false;
}
float tx = x - getViewX(dragView);
float ty = y - getViewY(dragView);
Rect mFrame = new Rect();
dragger.getHitRect(mFrame);
if (mFrame.contains((int) tx, (int) ty)) { // 當點擊拖拽圖標纔可進行拖拽
return true;
}
return false;
}
}
public boolean exchange(int scrPos, int desPos){
boolean success = false;
int count = mAddedItems.size();
if(scrPos==count || desPos==count){
return false;
}
if(scrPos!=desPos){
if(scrPos<count){
LDragItemClass srcObj = mAddedItems.get(scrPos);
if (desPos<count){
//change in add rang
LDragItemClass desObj = mAddedItems.get(desPos);
mAddedItems.set(scrPos, desObj);
mAddedItems.set(desPos, srcObj);
}else {
//change from add to notAdd
desPos = desPos-mAddedItems.size()-1;
LDragItemClass desObj = mNotAddedItems.get(desPos);
mAddedItems.set(scrPos, desObj);
mNotAddedItems.set(desPos, srcObj);
}
}else {
scrPos = scrPos-mAddedItems.size()-1;
LDragItemClass srcObj = mNotAddedItems.get(scrPos);
if (desPos<count){
//change from noTAdd to add
LDragItemClass desObj = mAddedItems.get(desPos);
mAddedItems.set(desPos, srcObj);
mNotAddedItems.set(scrPos, desObj);
}else {
//change in notAdd rang
desPos = desPos-mAddedItems.size()-1;
LDragItemClass desObj = mNotAddedItems.get(desPos);
mNotAddedItems.set(scrPos, desObj);
mNotAddedItems.set(desPos, srcObj);
}
}
success = true;
}
if (success) {
notifyDataSetChanged();
}
return success;
}
public float getViewX(View view) {
if (Build.VERSION.SDK_INT >= 11) {
return view.getX();
} else {
return view.getLeft() + view.getTranslationX();
}
}
public float getViewY(View view) {
if (Build.VERSION.SDK_INT >= 11) {
return view.getY();
} else {
return view.getTop() + view.getTranslationY();
}
}
}
複製代碼
LDragAdapter.javaui
package application.com.drag.widget;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import application.com.drag.R;
public class LDragAdapter extends BaseAdapter {
private Context mContext;
private int finalAdded = 0;
private List<LDragItemClass> mAddedItems;
private List<LDragItemClass> mNotAddedItems;
public static final int TYPE_TAG = 0;
public static final int TYPE_ITEM = 1;
private String tag = "";
public LDragAdapter(Context context, ArrayList<LDragItemClass> added, ArrayList<LDragItemClass> notAdded) {
this.mContext = context;
mAddedItems = added;
mNotAddedItems = notAdded;
finalAdded = mAddedItems.size();
tag = String.format(context.getResources().getString(R.string.drag_tag_text), finalAdded);
}
@Override
public int getCount() {
return 1+finalAdded+mNotAddedItems.size();
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if(position==finalAdded){
return TYPE_TAG;
}else {
return TYPE_ITEM;
}
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder=null;
if (convertView==null){
if (getItemViewType(position)==TYPE_ITEM){
convertView = LayoutInflater.from(mContext).inflate(R.layout.drag_list_item, null);
viewHolder = new ViewHolder(convertView);
}else {
convertView = LayoutInflater.from(mContext).inflate(R.layout.drag_list_item_tag, null);
viewHolder = new ViewHolder(convertView);
}
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
LDragItemClass itemObj = getLDragItem(position);
if(getItemViewType(position)==TYPE_ITEM){
viewHolder.nameView.setText(itemObj.name);
}else {
viewHolder.nameView.setText(tag);
viewHolder.nameView.setEnabled(false);
}
return convertView;
}
class ViewHolder{
TextView nameView;
ImageView iconView;
public ViewHolder(View view){
nameView = (TextView)view.findViewById(R.id.drag_list_item_text);
view.setTag(this);
}
}
private LDragItemClass getLDragItem(int position){
if (position==finalAdded){
return null;
}
else if(position<finalAdded){
//add
return mAddedItems.get(position);
}else{
//not add
return mNotAddedItems.get(position-finalAdded-1);
}
}
public boolean isTag(int pos){
if( getItemViewType(pos)==TYPE_TAG){
return true;
}
return false;
}
public boolean canDrag(int pos, View dragView, int x, int y){
if( getItemViewType(pos)==TYPE_TAG){
return false;
}
else {
if (dragView == null) {
return false;
}
View dragger = dragView.findViewById(R.id.drag_list_item_drag);
if (dragger == null || dragger.getVisibility() != View.VISIBLE) {
return false;
}
float tx = x - getViewX(dragView);
float ty = y - getViewY(dragView);
Rect mFrame = new Rect();
dragger.getHitRect(mFrame);
if (mFrame.contains((int) tx, (int) ty)) { // 當點擊拖拽圖標纔可進行拖拽
return true;
}
return false;
}
}
public boolean exchange(int scrPos, int desPos){
boolean success = false;
int count = mAddedItems.size();
if(scrPos==count || desPos==count){
return false;
}
if(scrPos!=desPos){
if(scrPos<count){
LDragItemClass srcObj = mAddedItems.get(scrPos);
if (desPos<count){
//change in add rang
LDragItemClass desObj = mAddedItems.get(desPos);
mAddedItems.set(scrPos, desObj);
mAddedItems.set(desPos, srcObj);
}else {
//change from add to notAdd
desPos = desPos-mAddedItems.size()-1;
LDragItemClass desObj = mNotAddedItems.get(desPos);
mAddedItems.set(scrPos, desObj);
mNotAddedItems.set(desPos, srcObj);
}
}else {
scrPos = scrPos-mAddedItems.size()-1;
LDragItemClass srcObj = mNotAddedItems.get(scrPos);
if (desPos<count){
//change from noTAdd to add
LDragItemClass desObj = mAddedItems.get(desPos);
mAddedItems.set(desPos, srcObj);
mNotAddedItems.set(scrPos, desObj);
}else {
//change in notAdd rang
desPos = desPos-mAddedItems.size()-1;
LDragItemClass desObj = mNotAddedItems.get(desPos);
mNotAddedItems.set(scrPos, desObj);
mNotAddedItems.set(desPos, srcObj);
}
}
success = true;
}
if (success) {
notifyDataSetChanged();
}
return success;
}
public float getViewX(View view) {
if (Build.VERSION.SDK_INT >= 11) {
return view.getX();
} else {
return view.getLeft() + view.getTranslationX();
}
}
public float getViewY(View view) {
if (Build.VERSION.SDK_INT >= 11) {
return view.getY();
} else {
return view.getTop() + view.getTranslationY();
}
}
}
複製代碼
上面三個文件,咱們的自定義listView算是寫完了,下面咱們來看具體實現this
activity_main.xmlspa
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<application.com.drag.widget.LDragListView
android:id="@+id/id_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@drawable/list_item_divider"
android:dividerHeight="1px"
android:listSelector="@drawable/drag_list_selector"
>
</application.com.drag.widget.LDragListView>
</RelativeLayout>
複製代碼
咱們在來看定義怎麼定義itme和tag; drag_list_item.xml.net
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#FFFFFF"
android:layout_height="wrap_content">
<TextView
android:id="@+id/drag_list_item_text"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:paddingLeft="32dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
/>
<ImageView android:id="@+id/drag_list_item_drag"
android:src="@mipmap/dragicon"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:paddingRight="16dip"
android:layout_height="50dp" />
</RelativeLayout>
複製代碼
drag_list_item_tag.xmlcode
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00555555"
android:padding="5dip"
android:paddingLeft="10dip">
<!--文本框的ID保持不變-->
<TextView
android:id="@+id/drag_list_item_text"
android:layout_width="match_parent"
android:layout_height="20dip"
android:textColor="@android:color/darker_gray"
android:gravity="center"
android:textSize="11sp"
android:text="@string/drag_tag_text"/>
<!--去除來右邊拖拽圖像,分組標籤是不能隨意拖動的-->
</FrameLayout>
複製代碼
drawable包下定義: drag_list_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 這個是按下去的時候item顯示背景色 -->;
<item android:drawable="@color/itemColorPressed" android:state_pressed="true"></item>
<!-- 這個是默認的item背景色,設爲了透明 -->
<item android:drawable="@android:color/transparent" android:state_pressed="false"></item>
</selector>
複製代碼
list_item_divider.xml
<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="30dp"
android:insetRight="0dp"
android:drawable="@color/gray">
</inset>
複製代碼
咱們來看最終的MainActivity的實現;
package application.com.drag;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import java.util.ArrayList;
import application.com.drag.widget.LDragAdapter;
import application.com.drag.widget.LDragItemClass;
import application.com.drag.widget.LDragListView;
public class MainActivity extends Activity {
private String data[] = new String[]{"WLAN","截屏","數據","GPS","靜音","飛行模式","藍牙","方向鎖屏","自動亮度","手電筒","省電模式","快傳","振動","屏蔽按鍵","勿擾模式","鎖屏","同步","護眼模式"};
private LDragAdapter adapter = null;
private LDragListView listView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化樣本數據
initData();
}
void initData(){
ArrayList<LDragItemClass> addlist = new ArrayList<LDragItemClass>();
ArrayList<LDragItemClass> notAddlist = new ArrayList<LDragItemClass>();
for(int i=0; i<data.length; i++){
LDragItemClass item = new LDragItemClass();
item.name = data[i];
if(i>6){
notAddlist.add(item);
}else {
addlist.add(item);
}
}
adapter = new LDragAdapter(this, addlist, notAddlist);
listView = (LDragListView) findViewById(R.id.id_list);
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("", "pos="+position);
listView.startDragOnLong();
return false;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("", "pos="+position);
}
});
}
}
複製代碼
以上就算完成了,咱們來張圖片: