You can drag the sorted ListView and drag the sorted ListView.

Source: Internet
Author: User

You can drag the sorted ListView and drag the sorted ListView.

I,


Ii. Brief Introduction

1. the effect to be achieved is to long press the right side of the drag part of the layout to achieve the drag sorting of list items

2. When you click the single-choice button in front of a list item, the delete icon is displayed on the right of the item, and the icon is clicked to delete the current entry.


Iii. Implementation ideas

Use the open-source code drag-sort-listview-master on github for transformation.


4. Main source code Display

1. Activity Code

Package com. gengducun. dslvdemo; import java. util. arrayList; import android. app. activity; import android. OS. bundle; import android. widget. listView; import com. mobeta. android. dslv. dragSortListView; public class MainActivity extends Activity {protected static final String TAG = "MainActivity";/** product information list **/private ArrayList <ProductInfoBean> productInfoList; /** product list control **/private DragSortListView mDslvProductList ;/** Product information list */private ProductListAdapter mProductListAdapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mDslvProductList = (DragSortListView) findViewById (R. id. dslv_product_list); initProductList (); mProductListAdapter = new ProductListAdapter (productInfoList, this); mDslvProductList. setAdapter (mProductListAdapt Er); mDslvProductList. setDropListener (onDrop); mDslvProductList. setChoiceMode (ListView. CHOICE_MODE_SINGLE);} private DragSortListView. dropListener onDrop = new DragSortListView. dropListener () {@ Overridepublic void drop (int from, int to) {if (from! = To) {ProductInfoBean item = (ProductInfoBean) mProductListAdapter. getItem (from); mProductListAdapter. remove (item); mProductListAdapter. insert (item, from, to); mDslvProductList. moveCheckState (from, to) ;}}; private void initProductList () {productInfoList = new ArrayList <ProductInfoBean> (); ProductInfoBean productInfo1 = new ProductInfoBean ("EURUSD1 "); productInfoBean productInfo2 = new ProductInfoBean ("EURUSD2"); ProductInfoBean productInfo3 = new ProductInfoBean ("EURUSD3"); ProductInfoBean productInfo4 = new ProductInfoBean ("EURUSD4 "); productInfoBean productInfo5 = new ProductInfoBean ("EURUSD5"); ProductInfoBean productInfo6 = new ProductInfoBean ("EURUSD6"); ProductInfoBean productInfo7 = new ProductInfoBean ("EURUSD7"); productInfoList. add (productInfo1); productInfoList. add (productInfo2); productInfoList. add (productInfo3); productInfoList. add (productInfo4); productInfoList. add (productInfo5); productInfoList. add (productInfo6); productInfoList. add (productInfo7 );}}


2. Adapter code

<Span style = "font-size: 14px;"> package com. gengducun. dslvdemo; import java. util. arrayList; import android. content. context; import android. util. log; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. linearLayout; import android. widget. radi OButton; import android. widget. textView; public class ProductListAdapter extends BaseAdapter {private static final String TAG = "ProductListAdapter1";/** product information list */private ArrayList <ProductInfoBean> productInfoList; /** Context */private Context mContext;/** layout loader */private LayoutInflater mInflater;/** layout cache */private ViewHolder mHolder; /** this type of adapter object **/private ProductListAdapter mProductListAdapter1;/** selected position **/privat E int mSelectPosition =-1; public ProductListAdapter (ArrayList <ProductInfoBean> productInfoList, Context context) {super (); this. productInfoList = productInfoList; this. mContext = context; this. mInflater = LayoutInflater. from (context); this. mProductListAdapter1 = this;} @ Overridepublic int getCount () {return null = productInfoList? 0: productInfoList. size () ;}@ Overridepublic Object getItem (int position) {return productInfoList. get (position) ;}@ Overridepublic long getItemId (int position) {return position;} public int getmSelectPosition () {return mSelectPosition;} public void setmSelectPosition (int mSelectPosition) {this. mSelectPosition = mSelectPosition;}/*** Delete product * @ param productInfoBean */public void remove (ProductInfoBean pr OductInfoBean) {synchronized (this) {productInfoList. remove (productInfoBean);} notifyDataSetChanged ();} /*** move the product to the specified position * @ param productInfoBean * @ param from the previous position * @ param to move, location of the product */public void insert (ProductInfoBean productInfoBean, int from, int to) {synchronized (this) {Log. I (TAG, "from =" + from + "to =" + to); if (mSelectPosition! =-1) {// if (from <mSelectPosition & to <mSelectPosition) | (from> mSelectPosition & to> mSelectPosition) with a selected item )) {// drag the selected item at the position before the selected item or the area after the selected item does not affect the selected item} else if (from <mSelectPosition & to> mSelectPosition) {// move the selected item to the position after the selected item, then the index of the selected item needs to change. It should be the position of the selected item-1 mSelectPosition = mSelectPosition-1 ;} else if (from> mSelectPosition & to <mSelectPosition) {// move the items after the selected item to the position before the selected item. The index also needs to change and should be selected. Position + 1 mSelectPosition = mSelectPosition + 1;} else if (from = mSelectPosition & from> to) | (from = mSelectPosition & from <)) {// if you drag the selected item to the direction before the selected item, the index of the selected item also needs to change. It should be the position mSelectPosition = ;} else if (from <mSelectPosition & to = mSelectPosition) {// move the selected item to the position of the selected item. The index needs to change, the index of the selected item should be the value of the selected item-1 mSelectPosition = mSelectPosition-1;} else if (from> mSelectPosition & to = mSelectPositi On) {// move the selected item to the position of the current item. The index needs to be bent, the index of the selected item should be the value of the selected item + 1 mSelectPosition = mSelectPosition + 1 ;}} productInfoList. add (to, productInfoBean);} notifyDataSetChanged () ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {if (null = convertView) {convertView = mInflater. inflate (R. layout. list_item_product_list_1, null); mHolder = new ViewHolder (); mHolder. rbSelect = (RadioButton) convertV Iew. findViewById (R. id. rb_select); mHolder. tvPurductName = (TextView) convertView. findViewById (R. id. TV _product_name); mHolder. llDragLayout = (LinearLayout) convertView. findViewById (R. id. drag_handle); mHolder. ivDrag = (ImageView) convertView. findViewById (R. id. iv_drag); mHolder. ivDeleteProduct = (ImageView) convertView. findViewById (R. id. iv_product_delete); convertView. setTag (mHolder);} else {mHolder = (Vi EwHolder) convertView. getTag ();} ProductInfoBean productInfoBean = productInfoList. get (position); if (null! = ProductInfoBean) {// set the selected status of the single-choice button if (mSelectPosition = position) {mHolder. rbSelect. setChecked (true); mHolder. ivDrag. setVisibility (View. GONE); mHolder. ivDeleteProduct. setVisibility (View. VISIBLE); mHolder. ivDeleteProduct. setOnClickListener (new RemoveItemClickListener (mSelectPosition);} else {mHolder. rbSelect. setChecked (false); mHolder. ivDrag. setVisibility (View. VISIBLE); mHolder. ivDeleteProduct. setVisibi Lity (View. GONE);} mHolder. rbSelect. setOnClickListener (new ProductClickListener (position); // set the product name if (null! = ProductInfoBean. getProductName ()&&! ("". Equals (productInfoBean. getProductName () {mHolder. tvPurductName. setText (productInfoBean. getProductName () ;}} return convertView;} class ViewHolder {RadioButton rbSelect; TextView tvPurductName; LinearLayout llDragLayout; ImageView ivDrag; ImageView ivDeleteProduct ;} /*** single button event listener * @ author Wilson */class ProductClickListener implements OnClickListener {private int position; public ProductClickListener (int position) {super (); this. position = position ;}@ Overridepublic void onClick (View v) {if (mSelectPosition = position) {mSelectPosition =-1;} else {mSelectPosition = position;} Log. I (TAG, "mSelectPosition =" + mSelectPosition + "position =" + position); mproductlistadapter1.policydatasetchanged ();}} /*** Delete the current product item listening event */class RemoveItemClickListener implements OnClickListener {private int position; public RemoveItemClickListener (int position) {super (); this. position = position ;}@ Overridepublic void onClick (View v) {// 1. Delete the data productInfoList in the set. remove (position); // 2. Delete the server data mProductListAdapter1.notifyDataSetChanged (); // mSelectPosition =-1 ;}}</span>


Iv. Source Code

: Http://download.csdn.net/detail/xiogjie_67/8687813



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.