android 开源项目(二)可以左右拖动删除、调换位置的的listview的 item的实现(drag-sort-listview)
本次分享的是国外大牛们关于listView的一些牛逼方法的使用,外国的大牛真是牛掰,菜鸟无限佩服...
这个工程可以左右拖动删除、调换位置的的listview的 item的实现,这个方法在iphone手机上应用还是挺广泛的,android中也有这样实现的,在android的开源项目中,那些牛逼的外国大牛们也分享了他们的开源项目,下面呢,就是小菜鸟学习在git代码上借鉴那些外国大牛的源码,由于水平有限,在git上下载下来的源码无法导入Eclipse中直接运行,那么怎么解决这个问题呢,下面是菜鸟我的解决方法,大神们可以略过,如果有那个大神可以抽出空闲时间,教教小弟怎样直接运行从Git下下载的工程,小弟感激不尽,这样呢,小弟就不用用这样笨拙的方法解决这个问题了,这个工程源码的下载地址: https://github.com/bauerca/drag-sort-listview
实现的效果如下左右拖动删除、
上下拖动调换位置
部分代码实现的如下:
package com.nyist.net; import java.util.ArrayList; import java.util.Arrays; import android.app.Activity; import android.app.ListActivity; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; public class Drag_Sort_ListViewDemoActivity extends ListActivity { //private ArrayAdapter<ActivityInfo> adapter; private MyAdapter adapter; private ArrayList<ActivityInfo> mActivities = null; private String[] mActTitles; private String[] mActDescs; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.launcher); try { PackageInfo pi = getPackageManager().getPackageInfo( "com.nyist.net", PackageManager.GET_ACTIVITIES); mActivities = new ArrayList<ActivityInfo>(Arrays.asList(pi.activities)); String ourName = getClass().getName(); for (int i = 0; i < mActivities.size(); ++i) { if (ourName.equals(mActivities.get(i).name)) { mActivities.remove(i); break; } } } catch (PackageManager.NameNotFoundException e) { // Do nothing. Adapter will be empty. } mActTitles = getResources().getStringArray(R.array.activity_titles); mActDescs = getResources().getStringArray(R.array.activity_descs); //adapter = new ArrayAdapter<ActivityInfo>(this, // R.layout.launcher_item, R.id.text, mActivities); adapter = new MyAdapter(); setListAdapter(adapter); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { Intent intent = new Intent(); intent.setClassName(this, mActivities.get(position).name); startActivity(intent); } private class MyAdapter extends ArrayAdapter<ActivityInfo> { MyAdapter() { super(Drag_Sort_ListViewDemoActivity.this, R.layout.launcher_item, R.id.activity_title, mActivities); } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = super.getView(position, convertView, parent); TextView title = (TextView) v.findViewById(R.id.activity_title); TextView desc = (TextView) v.findViewById(R.id.activity_desc); title.setText(mActTitles[position]); desc.setText(mActDescs[position]); return v; } } }
想要源码的可以留个邮箱