ListView中每行颜色不同,如何使他拖曳排序的时候背景颜色也跟着一起交换?

oyymiko 2016-03-09 02:35:55
目前想达成一个效果,app初始时,会呈现这样的画面。

当用户想更改色列顺序时,可以直接拖曳,ex:把选项2的黄色色列拖曳至最底层。
但我的画面却只会改字的顺序,色列不会跟着改变,试问,数据交换那边,应该如何改写?

我的写法是参考谦虚的天下博主的文章改写的。
http://www.cnblogs.com/qianxudetianxia/archive/2011/06/12/2068761.html
改写的部份:

DragListActivity

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(groupKey.contains(getItem(position)) ){
//如果是分组标签,就加载分组标签的布局文件,两个布局文件显示效果不同
view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_tag, null);
}else{
pos = position % 10;
switch (pos)
{
case 1:
view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_red, null);
break;
case 2:
view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_yellow, null);
break;
case 3:
view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_green, null);
break;
case 4:
view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_blue, null);
break;
case 5:
view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_darkblue, null);
break;
case 6:
view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_purple, null);
break;
case 7:
view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_orange, null);
break;
case 8:
view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_midnightblue, null);
break;
case 9:
view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_pink, null);
break;
case 0:
view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_white, null);
break;
}
}
TextView textView = (TextView)view.findViewById(R.id.drag_list_item_text);
textView.setText(getItem(position));

return view;
}


除了红色列,其他列的layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffff00">
<TextView
android:id="@+id/drag_list_item_text"
android:layout_width="wrap_content"
android:layout_height="@dimen/drag_item_normal_height"
android:paddingLeft="5dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"/>
<ImageView android:id="@+id/drag_list_item_image"
android:src="@drawable/list_icon"
android:layout_width="wrap_content"
android:layout_height="@dimen/drag_item_normal_height"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#ffffff" />
</RelativeLayout>


DragListView

public void onDrop(int y){

//为了避免滑动到分割线的时候,返回-1的问题
int tempPosition = pointToPosition(0, y);
if(tempPosition != INVALID_POSITION){
dragPosition = tempPosition;
}

//超出边界处理
if(y < getChildAt(1).getTop()){
//超出上边界
dragPosition = 1;
}else if(y > getChildAt(getChildCount()-1).getBottom()){
//超出下边界
dragPosition = getAdapter().getCount()-1;
}

//数据交换

if(dragPosition > 1 && dragPosition < getAdapter().getCount()){
@SuppressWarnings("unchecked")
DragListActivity.DragListAdapter adapter = (DragListActivity.DragListAdapter)getAdapter();
String dragItem = adapter.getItem(dragSrcPosition);
adapter.remove(dragItem);
adapter.insert(dragItem, dragPosition);

自己觉得应该要改写在这...............却不知要如何改写
}

}


跪求大家帮帮忙了
...全文
210 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
oyymiko 2016-03-10
  • 打赏
  • 举报
回复
面临新问题! 如果想在红色列那行的CheckBox被选择时,不允许下面列再拖曳,这想法可行吗?可以执行?
oyymiko 2016-03-10
  • 打赏
  • 举报
回复
你点出了我的盲点,已改好,感动! 万分感谢
wei220000 2016-03-09
  • 打赏
  • 举报
回复
...你在getview里面已经把每一栏的颜色写死了啊,第二行必定是黄色。你应该根据getitem来判断用哪个layout,而不是position

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧