Android Learning Series (10)-drag and drop the App list ListView (top)

Source: Internet
Author: User
Tags addall

I have studied the implementation of drag-and-drop ListView for a long time and have benefited a lot.
In view of the fact that the content on the Internet is small and simple, and the specific implementation process may be helpful to everyone, in order to be detailed without distortion, we will analyze it step by step and divide it into two articles.

I. preparation.

1. Requirements
Preliminary: implement the drag-and-drop effect of the List (For details, refer to TouchInterceptor. java source code in packages/apps/Music in the Android source code ).
(It should be explained in advance that this article is not fully implemented in Music. The code implementation method has been adjusted to remove a lot of irrelevant things for your convenience, the effect has also been changed to another one that I personally think is simpler and more efficient .)
Extended: refer to the previous article Android Learning Series (9)-group ListView of App list to implement drag-and-drop effects of group list.
The following is an example of initial implementation.

2. Build the main interface DragListActivity. java and the main layout drag_list_activity.xml.

View sourceprint? 01 public class DragListActivity extends Activity {

02

03 // data list

04 private List <String> list = null;

05

06 // data adapter

07 private DragListAdapter adapter = null;

08

09 // store group tags

10 public static List <String> groupKey = new ArrayList <String> ();

11 // Group 1

12 private List <String> navList = new ArrayList <String> ();

13 // Group 2

14 private List <String> moreList = new ArrayList <String> ();

15

16 @ Override

17 public void onCreate (Bundle savedInstanceState ){

18 super. onCreate (savedInstanceState );

19 setContentView (R. layout. drag_list_activity );

20

21 // initialize sample data

22 initData ();

23

24 // DragListView will be introduced later

25 DragListView dragListView = (DragListView) findViewById (R. id. drag_list );

26 adapter = new DragListAdapter (this, list );

27 dragListView. setAdapter (adapter );

28}

29}

3. The layout of list items is drag_list_item.xml.

View sourceprint? 01 <? Xml version = "1.0" encoding = "UTF-8"?>

02 <! -- Emphasize one point and use relative layout -->

03 android: layout_width = "fill_parent"

04 android: layout_height = "wrap_content">

05 <TextView

06 android: id = "@ + id/drag_list_item_text"

07 android: layout_width = "wrap_content"

08 android: layout_height = "@ dimen/drag_item_normal_height"

09 android: paddingLeft = "5dip"

10 android: layout_alignParentLeft = "true"

11 android: layout_centerVertical = "true"

12 android: gravity = "center_vertical"/>

13 <ImageView android: id = "@ + id/drag_list_item_image"

14 android: src = "@ drawable/list_icon"

15 android: layout_alignParentRight = "true"

16 android: layout_centerVertical = "true"

17 android: layout_width = "wrap_content"

18 android: layout_height = "@ dimen/drag_item_normal_height"/>

19 </RelativeLayout>

4. Prepare sample data.
I have prepared two groups of data and initialized them in the initData () method mentioned above.

View sourceprint? 01 public void initData (){

02 // data result

03 list = new ArrayList <String> ();

04

05 // groupKey stores group tags

06 groupKey. add ("group ");

07 groupKey. add ("group B ");

08

09 for (int I = 0; I <5; I ++ ){

10 navList. add ("A option" + I );

11}

12 list. add ("group ");

13 list. addAll (navList );

14

15 for (int I = 0; I <8; I ++ ){

16 moreList. add ("B Option" + I );

17}

18 list. add ("group B ");

19 list. addAll (moreList );

20}

The groupKey of the group tag set is defined here and will be used for grouping.

5. Customize the DragListAdapter class.
Then we set up a data adapter to fill the list data into the ListView.

View sourceprint? 01 public static class DragListAdapter extends ArrayAdapter <String> {

02 public DragListAdapter (Context context, List <String> objects ){

03 super (context, 0, objects );

04}

05 @ Override

06 public View getView (int position, View convertView, ViewGroup parent ){

07 View view = convertView;

08 if (view = null ){

09 // load the list item Template

10 view = LayoutInflater. from (getContext (). inflate (R. layout. drag_list_item, null );

11}

12 TextView textView = (TextView) view. findViewById (R. id. drag_list_item_text );

13 textView. setText (getItem (position ));

14 return view;

15}

16}

Note that getItem (position) will obtain the position T (here it is a string) in the array adapter, a better method.

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.