本文主要是介绍侧滑删除进阶(四),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近在写滑动删除,网上代码又是看的云里雾里,果断自己研究,虽然效果不太好把,但是思路很清晰,代码逻辑很简单
先来看主页面布局
activity_main.xml--很简单就一个自定义listview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.slidelistview.MainActivity" ><com.example.slidelistview.SlideListViewandroid:id="@+id/listview"android:layout_width="fill_parent"android:layout_height="fill_parent"/></RelativeLayout>
再看看每条item的布局--很简单就2个textview(我都懒得用imageview,怕麻烦)
listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="30dp"android:orientation="horizontal" ><TextViewandroid:id="@+id/textview"android:gravity="center"android:background="#FFA07A"android:layout_width="fill_parent"android:layout_height="50dp"android:text="TextView" /><TextViewandroid:id="@+id/delete"android:gravity="center"android:background="#FF3E96"android:layout_width="50dp"android:layout_height="50dp"android:text="删除" /></LinearLayout>
MainActivity
package com.example.slidelistview;import java.util.ArrayList;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.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;public class MainActivity extends Activity {private SlideListView listView;private MyAdapter myAdapter;private ArrayList<String> data = new ArrayList<String>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myAdapter = new MyAdapter();for (int i = 0; i < 10; i++) {data.add("A");}listView = (SlideListView) findViewById(R.id.listview);listView.setAdapter(myAdapter);}class MyAdapter extends BaseAdapter {@Overridepublic int getCount() {return data.size();}@Overridepublic Object getItem(int position) {return data.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(final int position, View convertView,ViewGroup parent) {ViewHolder viewHolder = null;if (convertView == null) {viewHolder = new ViewHolder();convertView = View.inflate(MainActivity.this,R.layout.listview_item, null);viewHolder.textView = (TextView) convertView.findViewById(R.id.textview);viewHolder.deleTextView = (TextView) convertView.findViewById(R.id.delete);convertView.setTag(viewHolder);} else {viewHolder = (ViewHolder) convertView.getTag();}viewHolder.textView.setText(data.get(position));viewHolder.deleTextView.setOnClickListener(new OnClickListener() {public void onClick(View v) {data.remove(position);myAdapter.notifyDataSetChanged();listView.getChildAt(position).scrollTo(0, 0);}});return convertView;}class ViewHolder {private TextView textView;private TextView deleTextView;}}
}
SlideListView
package com.example.slidelistview;import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.Scroller;
import android.widget.Toast;public class SlideListView extends ListView {private Scroller scroller;private int downX;private int downY;private int moveX;private int moveY;private int downPosition;private int upPosition;private boolean deleOpen = false;private View slideListItem;private int lastPos = -1;/*** 构造器*/public SlideListView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context);}public SlideListView(Context context, AttributeSet attrs) {super(context, attrs);init(context);}public SlideListView(Context context) {super(context);init(context);}private void init(Context context) {scroller = new Scroller(context);}@Overridepublic boolean onTouchEvent(MotionEvent ev) {final int action = ev.getAction();switch (action) {case MotionEvent.ACTION_DOWN:downX = (int) ev.getX();downY = (int) ev.getY();// 获取按下的条目索引downPosition = pointToPosition(downX, downY);slideListItem = getChildAt(downPosition);if (slideListItem != null) {slideListItem.scrollTo(0, 0);}// 如果按下的不是当前的itemif (lastPos != -1) {if (lastPos != downPosition) {View childAt = getChildAt(lastPos);childAt.scrollTo(0, 0);}}lastPos = downPosition;break;case MotionEvent.ACTION_MOVE:moveX = (int) ev.getX();moveY = (int) ev.getY();int upPosition = pointToPosition(moveX, moveY);if (downPosition == upPosition) {// 是同一个条目if ((downX - moveX) > 50) {if (slideListItem != null) {slideListItem.scrollTo(100, 0);}}}break;case MotionEvent.ACTION_UP:break;default:break;}return super.onTouchEvent(ev);}}
这篇关于侧滑删除进阶(四)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!