Android DynamicGrid:拖曳交换位置
Android DynamicGrid:拖曳交换位置
Android DynamicGrid是一个第三方开源项目,DynamicGrid在github上的项目主页是:https://github.com/askerov/DynamicGrid
它实现在一个网格布局内,拖曳任意子view实现动态的交换位置,这很类似手机的桌面,手机桌面的图标,均可自由拖曳实现摆放位置的交换,如动图所示:
DynamicGrid使用起来简单,需要注意:
(1)最好使用完全的BaseDynamicGridAdapter构造BaseDynamicGridAdapter构造函数,否则可能无法正常工作。
(2)通常以长按模式触发拖曳交换位置操作,所以一般在onItemLongClick里面startEditMode开始编辑。
(3)按返回键取消拖曳操作,代码比较固定,重写onBackPressed:
@Override public void onBackPressed() { //此处将取消拖曳编辑模式,取消方式为按返回键。 if (gridView.isEditMode()) { gridView.stopEditMode(); } else { super.onBackPressed(); } }
给出一个完整的代码例子:
package zhangphil.app; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.TextView; import android.widget.Toast; import org.askerov.dynamicgrid.BaseDynamicGridAdapter; import org.askerov.dynamicgrid.DynamicGridView; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity { private DynamicGridView gridView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = (DynamicGridView) findViewById(R.id.dynamicgrid); ArrayList lists = new ArrayList(); for (int i = 0; i < 100; i++) { lists.add("项目-" + i); } //最好以完全方式构造,否则可能无法正常工作 MyAdapter adapter = new MyAdapter(this, lists, 3); gridView.setAdapter(adapter); gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { //长按触发拖曳交换位置操作。 gridView.startEditMode(position); return true; } }); gridView.setOnDragListener(new DynamicGridView.OnDragListener() { @Override public void onDragStarted(int position) { //Toast.makeText(getApplication(), "drag started at position " + position,Toast.LENGTH_SHORT).show(); } @Override public void onDragPositionsChanged(int oldPosition, int newPosition) { Toast.makeText(getApplication(), String.format("from %d to %d", oldPosition, newPosition), Toast.LENGTH_SHORT).show(); } }); } @Override public void onBackPressed() { //此处将取消拖曳编辑模式,取消方式为按返回键。 if (gridView.isEditMode()) { gridView.stopEditMode(); } else { super.onBackPressed(); } } private class MyAdapter extends BaseDynamicGridAdapter { @Override public View getView(int i, View view, ViewGroup viewGroup) { View v = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, null); TextView text = (TextView) v.findViewById(android.R.id.text1); text.setText("项目-" + i); v.setPadding(10, 10, 10, 10); return v; } public MyAdapter(Context context, List<?> items, int columnCount) { super(context, items, columnCount); } } }
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <org.askerov.dynamicgrid.DynamicGridView android:id="@+id/dynamicgrid" android:layout_height="wrap_content" android:layout_width="match_parent" android:numColumns="3"/> </LinearLayout>