Android allows you to slide the user list information and select Delete.

Source: Internet
Author: User

Android allows you to slide the user list information and select Delete.

During project development, you often need to delete the user list information. There are two common deletion methods in Android, one is similar to sliding the delete button, and the other is selecting through the CheckBox and then deleting through the button. The original Instance integrates the above two methods to delete the user list.

Design Concept: In the adapter class MyAdapter, a sliding delete button shows or hides a Map, a Map used to check whether the CheckBox is selected, and an interface ContentsDeleteListener that interacts with MainAcitivyt, this interface also contains two methods. The contentsDeleteSelect (int position, boolean isChecked) method is used to add or delete selected or canceled content from the selected List, contentDelete (int position) deletes a column entry at a specified position in the List. The sliding effect is mainly to display the sliding delete button when the sliding distance is greater than 40. When you perform the delete operation, all other settings are invisible and the CheckBox is set to unselected.

I. Code Implementation Effect


Ii. Code Implementation

1. activity_main.xml

     
      
  
 
The layout of the main interface is very simple. You can delete a Button and a ListView that stores user information.

2. activity_main_list_view.xml

 
     
      
      
  
 
The layout contains a selected CheckBox that displays the TextView of the content and a TextView that can be slide for deletion.

3. sliding effect anim_right_left.xml

 
     
  
 

4. CheckBox selector my_list_checekbox_selector.xml

 
      
        
        
        
    
 

5. User List adapter MyAdapter. java

Package com. example. slideandselectdeletedemo; import java. util. hashMap; import java. util. list; import java. util. map; import android. annotation. suppressLint; import android. content. context; import android. view. layoutInflater; import android. view. motionEvent; import android. view. view; import android. view. view. onClickListener; import android. view. view. onTouchListener; import android. view. animation. animation; import android. view. animation. animationUtils; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. checkBox; import android. widget. compoundButton; import android. widget. compoundButton. onCheckedChangeListener; import android. widget. relativeLayout; import android. widget. textView; import com. example. slidedeleteandselectdemo. r; @ SuppressLint ("UseSparseArrays") public class MyAdapter extends BaseAdapter {private LayoutInflater mInflater; private List
 
  
MContentsList; private Context mContext; private ContentsDeleteListener mContentsDeleteListener; // you can specify whether to display the private Map by swiping the delete button.
  
   
VisibleDeleteTv; // CheckBox selection and private Map not selected
   
    
SelectCb; // X coordinate point after sliding private int mLastX = 0; // private int mLastY = 0; public MyAdapter (Context mContext, List
    
     
MContentsList, ContentsDeleteListener mContentsDeleteListener) {this. mContext = mContext; this. mContentsList = mContentsList; this. mContentsDeleteListener = mContentsDeleteListener; this. mInflater = (LayoutInflater) mContext. getSystemService (Context. LAYOUT_INFLATER_SERVICE); visibleDeleteTv = new HashMap
     
      
(); SelectCb = new HashMap
      
        (); // When updating the interface, the record is not selected and the delete button is not visible for (int I = 0; I <mContentsList. size (); I ++) {visibleDeleteTv. put (I, View. GONE); selectCb. put (I, false) ;}} public void updateView (List
       
         MContentsList) {this. mContentsList = mContentsList; this. policydatasetchanged () ;}@ Overridepublic int getCount () {return mContentsList. size () ;}@ Overridepublic Object getItem (int position) {return mContentsList. get (position) ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (final int position, View convertView, ViewGroup parent) {final HolderView holderView; I F (convertView = null) {holderView = new HolderView (); convertView = mInflater. inflate (R. layout. activity_main_list_view, null); holderView. listSelectCb = (CheckBox) convertView. findViewById (R. id. my_select_cb); holderView. listContentTv = (TextView) convertView. findViewById (R. id. my_content_ TV); holderView. listDeleteTv = (TextView) convertView. findViewById (R. id. my_delete_ TV); holderView. listRl = (Relative Layout) convertView. findViewById (R. id. my_rl); convertView. setTag (holderView);} else {holderView = (HolderView) convertView. getTag (); if (holderView. listSelectCb. isChecked () {holderView. listSelectCb. setChecked (false) ;}}// display the content holderView. listContentTv. setText (mContentsList. get (position); if (visibleDeleteTv! = Null) {holderView. listDeleteTv. setVisibility (visibleDeleteTv. get (position);} if (selectCb! = Null) {holderView. listSelectCb. setChecked (selectCb. get (position); mContentsDeleteListener. contentsDeleteSelect (position, selectCb. get (position);} // process the selected event holderView. listRl. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {if (visibleDeleteTv. containsValue (View. VISIBLE) {// when VISIBLE, click again to make the settings invisible. for (int I = 0; I <getCount (); I ++) {visibleDeleteTv. put (I, View. GONE); select Cb. put (I, false); mContentsDeleteListener. contentsDeleteSelect (I, false);} policydatasetchanged ();} else {boolean isChecked = holderView. listSelectCb. isChecked ()? False: true; holderView. listSelectCb. setChecked (isChecked); mContentsDeleteListener. contentsDeleteSelect (position, isChecked) ;}}); holderView. listSelectCb. setOnCheckedChangeListener (new OnCheckedChangeListener () {@ Overridepublic void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {mContentsDeleteListener. contentsDeleteSelect (position, isChecked) ;}}); holderView. listSelectCb. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {if (visibleDeleteTv. containsValue (View. VISIBLE) {for (int I = 0; I <getCount (); I ++) {visibleDeleteTv. put (I, View. GONE); selectCb. put (I, false); mContentsDeleteListener. contentsDeleteSelect (I, false) ;}notifydatasetchanged () ;}}); convertView. setOnTouchListener (new OnTouchListener () {@ Overridepublic boolean onTouch (View v, MotionEvent event) {final Animation alpha = AnimationUtils. loadAnimation (mContext, R. anim. anim_right_left); int x = (int) event. getX (); // int y = (int) event. getY (); // Log. d (TAG, "x =" + x + "y =" + y); // press downif (event. getAction () = MotionEvent. ACTION_DOWN) {alpha. cancel ();} else if (event. getAction () = MotionEvent. ACTION_MOVE) {alpha. cancel (); int deltaX = mLastX-x; // int deltaY = mLastY-y; // Log. d (TAG, "deltaX =" + deltaX + ", deltaY =" + deltaY); if (deltaX> 40) {// when the sliding distance is greater than 40, display the deletion buttons for (int I = 0; I <getCount (); I ++) {visibleDeleteTv. put (I, View. GONE); selectCb. put (I, false); mContentsDeleteListener. contentsDeleteSelect (I, false); if (I = position) {visibleDeleteTv. put (position, View. VISIBLE); selectCb. put (I, true); mContentsDeleteListener. contentsDeleteSelect (I, true); if (visibleDeleteTv. get (position) = View. VISIBLE) {holderView. listDeleteTv. startAnimation (alpha) ;}}notifydatasetchanged () ;}return true ;}else {// otheralpha. cancel () ;}mlastx = x; // mLastY = y; return false ;}}); holderView. listDeleteTv. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// Log. d (TAG, "onClick: position =" + position); mContentsList. remove (position); mContentsDeleteListener. contentDelete (position); for (int I = 0; I <mContentsList. size (); I ++) {visibleDeleteTv. put (I, View. GONE); selectCb. put (I, false); mContentsDeleteListener. revoke (I, false);} yydatasetchanged () ;}}); return convertView;} public class HolderView {public TextView listContentTv, listDeleteTv; public CheckBox listSelectCb; public RelativeLayout listRl ;} public void setContentsDeleteListener (ContentsDeleteListener mContentsDeleteListener) {this. mContentsDeleteListener = mContentsDeleteListener;}/*** interface used to delete content ** @ author liangming. deng **/public interface ContentsDeleteListener {/*** select and cancel the specified location * @ param position * @ param isChecked */public void contentsDeleteSelect (int position, boolean isChecked);/*** Delete the content of a specified position * @ param position */public void contentDelete (int position);} public void setVisibleDeleteTv (Map
        
          VisibleDeleteTv) {this. visibleDeleteTv = visibleDeleteTv;} public void setSelectCb (Map
         
           SelectCb) {this. selectCb = selectCb ;}}
         
        
       
      
     
    
   
  
 

5. Main Interface code MainActivity. java

Package com. example. slideandselectdeletedemo; import java. util. arrayList; import java. util. collections; import java. util. list; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. listView; import com. example. slideandselectdeletedemo. myAdapter. contentsDeleteListener; import com. example. slidedeleteandselectdemo. r; public class MainActivity extends Activity implements ContentsDeleteListener, OnClickListener {private ListView myLv; private Button myDeleteBtn; private MyAdapter myAdapter; private String [] myContentsArray; private List
 
  
MyContentsList = new ArrayList
  
   
(); Private List
   
    
MySelectedList = new ArrayList
    
     
(); @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findById (); myContentsArray = this. getResources (). getStringArray (R. array. my_contents); if (myContentsArray! = Null) {Collections. addAll (myContentsList, myContentsArray);} myAdapter = new MyAdapter (this, myContentsList, this); myLv. setAdapter (myAdapter);} private void findById () {myLv = (ListView) this. findViewById (R. id. my_lv); myDeleteBtn = (Button) this. findViewById (R. id. my_delete_btn); myDeleteBtn. setOnClickListener (this) ;}@ Overridepublic void onResume () {super. onResume () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml.int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item);}/** Based on isChecked, add or delete data to or from the selected List * (non-Javadoc) * @ see com. example. slideandselectdeletedemo. myAdapter. contentsDeleteListener # contentsDeleteSelect (int, boolean) */@ Overridepublic void contentsDeleteSelect (int position, boolean isChecked) {if (isChecked) {mySelectedList. add (myContentsList. get (position);} else {mySelectedList. remove (myContentsList. get (position) ;}}/** delete data at the specified position * (non-Javadoc) * @ see com. example. slideandselectdeletedemo. myAdapter. contentsDeleteListener # contentDelete (int) */@ Overridepublic void contentDelete (int position) {// TODO Auto-generated method stubmyContentsList. remove (position) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. id. my_delete_btn: myContentsList. removeAll (mySelectedList); myAdapter. updateView (myContentsList); break ;}}}
    
   
  
 

Analysis: mySelectedList is used to store information about the CheckBox selected list. The delete button is convenient for deleting all selected items.

The above sections provide comments.

Source Code address: http://download.csdn.net/detail/a123demi/7751141





Related Article

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.