需求分析
一个界面内两个ListView
我关注的栏目列表 上面的要长按后可拖动排序 点击减号后列表项消失 下面列表增加一行 同时存储相应字符串到本地作为标记
未关注栏目列表 普通ListView 点击加号后 该列表项消失 上面列表增加一行
UI如下
上代码
MainActivity
public class MainActivity extends Activity { SharedPreferences preferences; SharedPreferences.Editor editor; //关注列表相关 private ArrayList<Column> followList = new ArrayList<Column>(); private DragSortListView followListView; private FollowAdapter mFollowAdapter; //未关注列表相关 private ArrayList<Column> notFollowList = new ArrayList<Column>(); private ListView notFollowListView; private NotFollowAdapter mNotFollowAdapter; //增加一个临时变量 保存所有的值 因为在后面列表为空时存在问题 private ArrayList<Column> allList = new ArrayList<Column>();; // 监听器在手机拖动停下的时候触发 private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() { @Override public void drop(int from, int to) {// from to 分别表示 被拖动控件原位置 和目标位置 if (from != to) { Column column = (Column) mFollowAdapter.getItem(from);// 得到listview的适配器 mFollowAdapter.remove(from);// 在适配器中”原位置“的数据。 mFollowAdapter.insert(column, to);// 在目标位置中插入被拖动的控件。 } } }; // 删除监听器,点击左边差号就触发。删除item操作。 private DragSortListView.RemoveListener onRemove = new DragSortListView.RemoveListener() { @Override public void remove(int which) { //未关注列表增加一行 if (followList.size() != 0) { Column column = (Column) mFollowAdapter.getItem(which); editor.putBoolean(column.name, false); notFollowList.add(column); mNotFollowAdapter.notifyDataSetChanged(); }else { notFollowList.clear(); notFollowList.addAll(allList); mNotFollowAdapter.notifyDataSetChanged(); } //再删除 mFollowAdapter.remove(which); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置无标题 setContentView(R.layout.activity_main); initView(); initData(); initSavaData(); followListView.setDropListener(onDrop); followListView.setRemoveListener(onRemove); mFollowAdapter = new FollowAdapter(MainActivity.this, followList); followListView.setAdapter(mFollowAdapter); followListView.setDragEnabled(true); // 设置是否可拖动。 mNotFollowAdapter = new NotFollowAdapter(MainActivity.this, notFollowList); notFollowListView.setAdapter(mNotFollowAdapter); } private void initView() { followListView = (DragSortListView) findViewById(R.id.follow_list); notFollowListView = (ListView) findViewById(R.id.not_follow_list); } private void initData() { // followList = NewsCategoryDao.getInstance(context).queryAll(); Column column1 = new Column(); column1.id = 1; column1.name = "国际足球"; followList.add(column1); allList.add(column1); Column column2 = new Column(); column2.id = 2; column2.name = "NBA"; followList.add(column2); allList.add(column2); Column column3 = new Column(); column3.id = 3; column3.name = "中国足球"; followList.add(column3); allList.add(column3); Column column4 = new Column(); column4.id = 4; column4.name = "中国蓝球"; followList.add(column4); allList.add(column4); Column column5 = new Column(); column5.id = 5; column5.name = "视频"; followList.add(column5); allList.add(column5); } private void initSavaData() { preferences = getSharedPreferences("column", MODE_WORLD_READABLE); editor = preferences.edit(); for (int i = 0; i < followList.size(); i++) { editor.putBoolean(followList.get(i).name, true); } editor.commit(); } public FollowAdapter getFollowAdapter() { return mFollowAdapter; } public ArrayList<Column> getFollowList() { return followList; } public NotFollowAdapter getNotFollowAdapter() { return mNotFollowAdapter; } public ArrayList<Column> getNotFollowList() { return notFollowList; } }
FollowAdapter
public class FollowAdapter extends BaseAdapter { private Context mContext; private LayoutInflater mInflater; private List<Column> followList; public FollowAdapter(Context context, List<Column> followList) { this.mContext = context; this.followList = followList; this.mInflater = LayoutInflater.from(context); } @Override public int getCount() { return followList.size(); } @Override public Object getItem(int arg0) { return followList.get(arg0); } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int position, View convertView, ViewGroup parent) { Column column = (Column) getItem(position); ViewHolder viewHolder; if (convertView == null) { viewHolder = new ViewHolder(); convertView = mInflater.inflate( R.layout.follow_listview_item, null); viewHolder.columnName = (TextView) convertView .findViewById(R.id.columnName); viewHolder.click_remove = (ImageView) convertView .findViewById(R.id.click_remove); viewHolder.drag_handle = (ImageView) convertView .findViewById(R.id.drag_handle); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.columnName.setText(column.name); return convertView; } class ViewHolder { TextView columnName; //栏目名 ImageView click_remove; //删除按钮 ImageView drag_handle; //拖动条 } public void remove(int arg0) {// 删除指定位置的item followList.remove(arg0); this.notifyDataSetChanged();// 不要忘记更改适配器对象的数据源 } public void insert(Column item, int arg0) {//在指定位置插入item followList.add(arg0, item); this.notifyDataSetChanged(); } }
NotFollowAdapter
public class NotFollowAdapter extends BaseAdapter{ private LayoutInflater mInflater; private Context mContext; private List<Column> notFollowList; private MainActivity mActivity; public NotFollowAdapter(Context context, List<Column> notFollowList) { this.mInflater = LayoutInflater.from(context); this.mContext = context; this.notFollowList = notFollowList; this.mActivity = (MainActivity)context; } @Override public int getCount() { return notFollowList.size(); } @Override public Object getItem(int position) { return notFollowList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { final Column column = (Column) getItem(position); ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.notfollow_listview_item, null); holder.columnName = (TextView) convertView.findViewById(R.id.columnName); holder.columnAdd = (ImageView) convertView.findViewById(R.id.columnAdd); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.columnName.setText(column.name); //添加按钮点击后 移除该行 上面列表增加一行 修改本地数据 final int tmp = position; holder.columnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { notFollowList.remove(tmp); mActivity.getNotFollowAdapter().notifyDataSetChanged(); mActivity.getFollowList().add(column); mActivity.getFollowAdapter().notifyDataSetChanged(); //保存数据相关 SharedPreferences preferences = mContext.getSharedPreferences("column", Context.MODE_WORLD_READABLE); SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean(column.name, false); } }); return convertView; } // ViewHolder静态类 static class ViewHolder { TextView columnName; //栏目名 ImageView columnAdd; //添加按钮 } }
Column
//栏目数据类 public class Column { public int id = 0; //l栏目ID号 public String name = ""; //栏目名称 }
代码见github https://github.com/huanyi0723/dragsort
时间: 2024-11-03 20:49:59