先给出效果图,但是不会弄动画。。。所以给出了三张图
效果就这样,滑动非常流畅,ok首先来讲讲原理,ListView+HorizontalScrollView的组合,listView中的美个item的内容就是HorizontalScrollView,注意我这里使用的自定义HorizontalScrollView,具体效果小伙伴们跑起来就知道了
首先写好布局文件,一共4个,主布局main_activity.xml, listview的布局文件 list_item.xml , 还有left.xml、right.xmlfe分别是上图中的白色部分和红色部分
main_activity.xml
<LinearLayout 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" android:orientation="vertical"> <ListView android:id="@+id/id_lv" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
<?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="50dp" android:orientation="vertical"> <!-- HorizontalScrollView里面的view在宽度上设置match没有效果 --> <com.ex.listviewdelitem.view.MyHorizontalScrollView android:id="@+id/id_hsv" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none"> <LinearLayout android:layout_width="360dp" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center_vertical"> <include layout="@layout/left"/> <include layout="@layout/right" /> </LinearLayout> </com.ex.listviewdelitem.view.MyHorizontalScrollView> </LinearLayout>
left.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="match_parent" android:orientation="vertical" android:gravity="center_vertical" > <TextView android:id="@+id/id_tv_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:layout_marginLeft="60dp"/> </LinearLayout>
right.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="match_parent" android:orientation="vertical" android:gravity="center_vertical" android:background="#FF2D2D"> <Button android:id="@+id/id_tv_2" android:layout_width="match_parent" android:layout_height="40dp" android:text="删除" android:textSize="15dp" android:textColor="#000000" android:layout_marginLeft="50dp" android:background="@drawable/bt_bg"/> </LinearLayout>
第二部,自定义我们的MyHorizontalScrollView
/** * @author solo * 自定义水平滑动view分三步: * 1、初始化各个子View,注意不能再构造方法中执行,否则返回的结果都是null * 2、设置其中每个子View的宽和高 * 3、在onlayout中设置HorizontalScrollView的初始位置 * 4、在onTouchEvent中设置滑动时的触发事件 */ public class MyHorizontalScrollView extends HorizontalScrollView { private ViewGroup mLeftView; private ViewGroup mRightView; private ViewGroup mWapper; private int mScreenWidth; private int mRightViewWidth; private boolean once; public MyHorizontalScrollView(Context context) { this(context,null); } public MyHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public MyHorizontalScrollView(Context context, AttributeSet attrs) { this(context, attrs,0); DisplayMetrics dm = new DisplayMetrics(); WindowManager wm=(WindowManager) context.getSystemService(context.WINDOW_SERVICE); wm.getDefaultDisplay().getMetrics(dm); mScreenWidth=dm.widthPixels; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); Log.i("TAG", 1111111+""); //设置各个view的宽高 if(!once){ mWapper=(ViewGroup) getChildAt(0); mLeftView=(ViewGroup) mWapper.getChildAt(0); mRightView=(ViewGroup) mWapper.getChildAt(1); mLeftView.getLayoutParams().width=mScreenWidth; mRightView.getLayoutParams().width=mScreenWidth/3; mRightViewWidth=mScreenWidth/3; } } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { Log.i("TAG", changed+""); super.onLayout(changed, l, t, r, b); scrollTo(0, 0); } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); int x=getScrollX(); switch (action) { case MotionEvent.ACTION_UP:{ if(x<mRightViewWidth/2){ smoothScrollTo(0, 0); }else { smoothScrollTo(mRightViewWidth, 0); } }return true; } return super.onTouchEvent(event); } }
给ListView自定义一个Adapter
package com.ex.listviewdelitem; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MyAdapter extends ArrayAdapter<String> { LayoutInflater mInflater; Context mContext; List<String> mDatas; public MyAdapter(Context context,List<String> datas){ super(context, 0,datas); mContext=context; mDatas=datas; mInflater=LayoutInflater.from(context); } private class ViewHloder{ TextView text; Button bt; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHloder holder; if (convertView==null) { convertView=mInflater.inflate(R.layout.list_item, parent, false); holder=new ViewHloder(); holder.text=(TextView) convertView.findViewById(R.id.id_tv_1); holder.bt=(Button) convertView.findViewById(R.id.id_tv_2); convertView.setTag(holder); }else { holder=(ViewHloder) convertView.getTag(); } holder.text.setText(mDatas.get(position)); holder.bt.setText("删除"); convertView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { } }); holder.bt.setOnClickListener(new OnClickListener() { //把item上buton的点击事件放到这里就行了,不能直接放到mainActivity @Override public void onClick(View v) { remove(mDatas.get(position)); } }); return convertView; } }
最后在Main_Activity中进行最后的工作
package com.ex.listviewdelitem; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { private ListView mListView; private List<String> mDatas; private MyAdapter mAdapter; Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initDatas(); mAdapter=new MyAdapter(this, mDatas); mListView.setAdapter(mAdapter); } private void initView() { mListView=(ListView) findViewById(R.id.id_lv); mButton=(Button) findViewById(R.id.id_tv_2); mDatas=new ArrayList<String>(); } private void initDatas(){ mDatas.add("北京天气晴朗"); mDatas.add("上海多云小雨"); mDatas.add("杭州风和日丽"); mDatas.add("广州狂风暴雨"); mDatas.add("深圳狂风暴雨"); } }
最后我这里有一个问题,ListView的本身不能点击了,不过我的删除按钮还是能点击的,滑动删除效果没有影响,有哪位大神研究出怎么使ListView也能点击求告知