Zhao yazhi _ AndroidUI_listview's item sliding deletion effect, listview sliding Deletion

Source: Internet
Author: User

Zhao yazhi _ AndroidUI_listview's item sliding deletion effect, listview sliding Deletion

Package com. example. listsildedel; import java. util. arrayList; import java. util. list; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. widget. listView; public class MainActivity extends Activity {private ListView mListView; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mListView = (ListView) this. findViewById (R. id. list); List <String> list = new ArrayList <String> (); for (int I = 0; I <10; I ++) {list. add ("option" + I);} // instantiate the custom content adapter class MyAdapter adapter = new MyAdapter (this, this, list); // set the listView adaptation to mListView. setAdapter (adapter);} public void openActivity () {Intent intent = new Intent (this, TestActivity. class); startActivity (intent );}}


Adapter

Package com. example. listsildedel; import java. util. list; 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. viewGroup; import android. widget. baseAdapter; import android. widget. button; import android. widget. textView; public class MyAd Apter extends BaseAdapter {private List <String> arrays = null; private Context mContext; private Button curDel_btn; private float x, ux; private MainActivity mActivity; public MyAdapter (MainActivity activity, Context mContext, list <String> arrays) {this. mContext = mContext; this. arrays = arrays; mActivity = activity;} public int getCount () {return this. arrays. size ();} public Object getItem (int Position) {return null;} public long getItemId (int position) {return position;} public View getView (final int position, View view, ViewGroup arg2) {ViewHolder viewHolder = null; if (view = null) {viewHolder = new ViewHolder (); view = LayoutInflater. from (mContext ). inflate (R. layout. item, null); viewHolder. tvTitle = (TextView) view. findViewById (R. id. title); viewHolder. btnDel = (Button) view. f IndViewById (R. id. del); view. setTag (viewHolder);} else {viewHolder = (ViewHolder) view. getTag () ;}// replaced the onItemClick <span style = "font-size: 18px;"> <strong> view. setOnClickListener (new OnClickListener () {public void onClick (View v) {// TODO Auto-generated method stub mActivity. openActivity () ;}}); </strong> </span> // set the touch listener view for each view item. setOnTouchListener (new OnTouchListener () {public Boolean onTouch (View v, MotionEvent event) {final ViewHolder holder = (ViewHolder) v. getTag (); // process if (event. getAction () = MotionEvent. ACTION_DOWN) {// set the background to the selected status // v. setBackgroundResource (R. drawable. mm_listitem_pressed); // obtain the x axis coordinate x = event. getX (); // determine whether the delete button exists. if so, hide if (curDel_btn! = Null) {if (curDel_btn.getVisibility () = View. VISIBLE) {curDel_btn.setVisibility (View. GONE); return true ;}} else if (event. getAction () = MotionEvent. ACTION_UP) {// release processing // set the background to unselected normal status // v. setBackgroundResource (R. drawable. mm_listitem_simple); // obtain the x coordinate ux = event when releasing. getX (); // if (holder. btnDel! = Null) {// when the difference between pressing and releasing the absolute value is greater than 20, the delete button is displayed; otherwise, if (Math. abs (x-ux)> 20) {holder. btnDel. setVisibility (View. VISIBLE); curDel_btn = holder. btnDel; return true ;}} else if (event. getAction () = MotionEvent. ACTION_MOVE) {// return true if the background is selected when sliding; // v. setBackgroundResource (R. drawable. mm_listitem_pressed);} else {// other mode // set the background to unselected normal status // v. setBackgroundResource (R. drawable. mm_listitem_simple);} return fal Se ;}}); viewHolder. tvTitle. setText (this. arrays. get (position); // Add a listener event for the delete button to delete the viewHolder when you click the delete button. btnDel. setOnClickListener (new OnClickListener () {public void onClick (View v) {if (curDel_btn! = Null) curDel_btn.setVisibility (View. GONE); arrays. remove (position); policydatasetchanged () ;}}); return view ;}final static class ViewHolder {TextView tvTitle; Button btnDel ;}}








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.