package com.ryg.slideview; import com.ryg.slideview.MainActivity.MessageItem; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.ListView; public class ListViewCompat extends ListView { private static final String TAG = "ListViewCompat"; private SlideView mFocusedItemView; public ListViewCompat(Context context) { super(context); } public ListViewCompat(Context context, AttributeSet attrs) { super(context, attrs); } public ListViewCompat(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void shrinkListItem(int position) { View item = getChildAt(position); if (item != null) { try { ((SlideView) item).shrink(); } catch (ClassCastException e) { e.printStackTrace(); } } } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { int x = (int) event.getX(); int y = (int) event.getY(); int position = pointToPosition(x, y); Log.e(TAG, "postion=" + position); if (position != INVALID_POSITION) { MessageItem data = (MessageItem) getItemAtPosition(position); mFocusedItemView = data.slideView; Log.e(TAG, "FocusedItemView=" + mFocusedItemView); } } default: break; } if (mFocusedItemView != null) { mFocusedItemView.onRequireTouchEvent(event); } return super.onTouchEvent(event); } }
package com.ryg.slideview; import java.util.ArrayList; import java.util.List; import com.ryg.slideview.SlideView.OnSlideListener; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; 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.ImageView; import android.widget.TextView; public class MainActivity extends Activity implements OnItemClickListener, OnClickListener, OnSlideListener { private static final String TAG = "MainActivity"; private ListViewCompat mListView; private List<MessageItem> mMessageItems = new ArrayList<MainActivity.MessageItem>(); private SlideView mLastSlideViewWithStatusOn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mListView = (ListViewCompat) findViewById(R.id.list); for (int i = 0; i < 20; i++) { MessageItem item = new MessageItem(); if (i % 3 == 0) { item.iconRes = R.drawable.default_qq_avatar; item.title = "腾讯新闻"; item.msg = "青岛爆炸满月:大量鱼虾死亡"; item.time = "晚上18:18"; } else { item.iconRes = R.drawable.wechat_icon; item.title = "微信团队"; item.msg = "欢迎你使用微信"; item.time = "12月18日"; } mMessageItems.add(item); } mListView.setAdapter(new SlideAdapter()); mListView.setOnItemClickListener(this); } private class SlideAdapter extends BaseAdapter { private LayoutInflater mInflater; SlideAdapter() { super(); mInflater = getLayoutInflater(); } @Override public int getCount() { return mMessageItems.size(); } @Override public Object getItem(int position) { return mMessageItems.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; SlideView slideView = (SlideView) convertView; if (slideView == null) { View itemView = mInflater.inflate(R.layout.list_item, null); slideView = new SlideView(MainActivity.this); slideView.setContentView(itemView); holder = new ViewHolder(slideView); slideView.setOnSlideListener(MainActivity.this); slideView.setTag(holder); } else { holder = (ViewHolder) slideView.getTag(); } MessageItem item = mMessageItems.get(position); item.slideView = slideView; item.slideView.shrink(); holder.icon.setImageResource(item.iconRes); holder.title.setText(item.title); holder.msg.setText(item.msg); holder.time.setText(item.time); holder.deleteHolder.setOnClickListener(MainActivity.this); return slideView; } } public class MessageItem { public int iconRes; public String title; public String msg; public String time; public SlideView slideView; } private static class ViewHolder { public ImageView icon; public TextView title; public TextView msg; public TextView time; public ViewGroup deleteHolder; ViewHolder(View view) { icon = (ImageView) view.findViewById(R.id.icon); title = (TextView) view.findViewById(R.id.title); msg = (TextView) view.findViewById(R.id.msg); time = (TextView) view.findViewById(R.id.time); deleteHolder = (ViewGroup)view.findViewById(R.id.holder); } } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Log.e(TAG, "onItemClick position=" + position); } @Override public void onSlide(View view, int status) { if (mLastSlideViewWithStatusOn != null && mLastSlideViewWithStatusOn != view) { mLastSlideViewWithStatusOn.shrink(); } if (status == SLIDE_STATUS_ON) { mLastSlideViewWithStatusOn = (SlideView) view; } } @Override public void onClick(View v) { if (v.getId() == R.id.holder) { Log.e(TAG, "onClick v=" + v); } } }
package com.ryg.slideview; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; import android.view.MotionEvent; import android.view.View; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Scroller; import android.widget.TextView; public class SlideView extends LinearLayout { private static final String TAG = "SlideView"; private Context mContext; private LinearLayout mViewContent; private RelativeLayout mHolder; private Scroller mScroller; private OnSlideListener mOnSlideListener; private int mHolderWidth = 120; private int mLastX = 0; private int mLastY = 0; private static final int TAN = 2; public interface OnSlideListener { public static final int SLIDE_STATUS_OFF = 0; public static final int SLIDE_STATUS_START_SCROLL = 1; public static final int SLIDE_STATUS_ON = 2; /** * @param view current SlideView * @param status SLIDE_STATUS_ON or SLIDE_STATUS_OFF */ public void onSlide(View view, int status); } public SlideView(Context context) { super(context); initView(); } public SlideView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } private void initView() { mContext = getContext(); mScroller = new Scroller(mContext); setOrientation(LinearLayout.HORIZONTAL); View.inflate(mContext, R.layout.slide_view_merge, this); mViewContent = (LinearLayout) findViewById(R.id.view_content); mHolderWidth = Math.round(TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, mHolderWidth, getResources() .getDisplayMetrics())); } public void setButtonText(CharSequence text) { ((TextView)findViewById(R.id.delete)).setText(text); } public void setContentView(View view) { mViewContent.addView(view); } public void setOnSlideListener(OnSlideListener onSlideListener) { mOnSlideListener = onSlideListener; } public void shrink() { if (getScrollX() != 0) { this.smoothScrollTo(0, 0); } } public void onRequireTouchEvent(MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); int scrollX = getScrollX(); Log.d(TAG, "x=" + x + " y=" + y); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { if (!mScroller.isFinished()) { mScroller.abortAnimation(); } if (mOnSlideListener != null) { mOnSlideListener.onSlide(this, OnSlideListener.SLIDE_STATUS_START_SCROLL); } break; } case MotionEvent.ACTION_MOVE: { int deltaX = x - mLastX; int deltaY = y - mLastY; if (Math.abs(deltaX) < Math.abs(deltaY) * TAN) { break; } int newScrollX = scrollX - deltaX; if (deltaX != 0) { if (newScrollX < 0) { newScrollX = 0; } else if (newScrollX > mHolderWidth) { newScrollX = mHolderWidth; } this.scrollTo(newScrollX, 0); } break; } case MotionEvent.ACTION_UP: { int newScrollX = 0; if (scrollX - mHolderWidth * 0.75 > 0) { newScrollX = mHolderWidth; } this.smoothScrollTo(newScrollX, 0); if (mOnSlideListener != null) { mOnSlideListener.onSlide(this, newScrollX == 0 ? OnSlideListener.SLIDE_STATUS_OFF : OnSlideListener.SLIDE_STATUS_ON); } break; } default: break; } mLastX = x; mLastY = y; } private void smoothScrollTo(int destX, int destY) { // 缂撴參婊氬姩鍒版寚瀹氫綅缃� int scrollX = getScrollX(); int delta = destX - scrollX; mScroller.startScroll(scrollX, 0, delta, 0, Math.abs(delta) * 3); invalidate(); } @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); } } }
holder_bg.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/dimgrey" android:state_pressed="true"></item> <item android:drawable="@color/dimgrey" android:state_focused="true"></item> <item android:drawable="@color/dimgrey" android:state_selected="true"></item> <item android:drawable="@color/gray" android:state_enabled="true"></item> <item android:drawable="@color/gray" android:state_enabled="false"></item> </selector>
list_item_bg.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/item_point_bg" android:state_pressed="true"></item> <item android:drawable="@drawable/item_point_bg" android:state_focused="true"></item> <item android:drawable="@drawable/item_point_bg" android:state_selected="true"></item> <item android:drawable="@android:color/transparent" android:state_enabled="true"></item> <item android:drawable="@android:color/transparent" android:state_enabled="false"></item> </selector>
main.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" > <com.ryg.slideview.ListViewCompat android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff4f7f9" android:cacheColorHint="#00000000" android:divider="#dddbdb" android:dividerHeight="1.0px" android:drawSelectorOnTop="false" android:listSelector="@android:color/transparent" android:scrollbars="none" /> </LinearLayout>
list_item.xml
<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="58dp" android:background="@drawable/list_item_bg" android:descendantFocusability="blocksDescendants" android:gravity="center_vertical" android:paddingBottom="5dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="5dp" > <ImageView android:id="@+id/icon" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginRight="5dp" android:src="@drawable/wechat_icon" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/icon" android:textColor="@color/black" android:textSize="15sp" /> <TextView android:id="@+id/msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@id/icon" android:layout_alignLeft="@id/title" android:textColor="@color/grey" /> <TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@id/title" android:textColor="@color/grey" /> </RelativeLayout>
slide_view_merge.xml
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/view_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > </LinearLayout> <RelativeLayout android:id="@+id/holder" android:layout_width="120dp" android:layout_height="match_parent" android:clickable="true" android:background="@drawable/holder_bg"> <TextView android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@drawable/del_icon_normal" android:layout_centerInParent="true" android:gravity="center" android:textColor="@color/floralwhite" android:text="删除" /> </RelativeLayout> </merge>
colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="white">#FFFFFF</color> <!-- 白色 --> <color name="ivory">#FFFFF0</color> <!-- 象牙色 --> <color name="lightyellow">#FFFFE0</color> <!-- 亮黄色 --> <color name="yellow">#FFFF00</color> <!-- 黄色 --> <color name="snow">#FFFAFA</color> <!-- 雪白色 --> <color name="floralwhite">#FFFAF0</color> <!-- 花白色 --> <color name="lemonchiffon">#FFFACD</color> <!-- 柠檬绸色 --> <color name="cornsilk">#FFF8DC</color> <!-- 米绸色 --> <color name="seashell">#FFF5EE</color> <!-- 海贝色 --> <color name="lavenderblush">#FFF0F5</color> <!-- 淡紫红 --> <color name="papayawhip">#FFEFD5</color> <!-- 番木色 --> <color name="blanchedalmond">#FFEBCD</color> <!-- 白杏色 --> <color name="mistyrose">#FFE4E1</color> <!-- 浅玫瑰色 --> <color name="bisque">#FFE4C4</color> <!-- 桔黄色 --> <color name="moccasin">#FFE4B5</color> <!-- 鹿皮色 --> <color name="navajowhite">#FFDEAD</color> <!-- 纳瓦白 --> <color name="peachpuff">#FFDAB9</color> <!-- 桃色 --> <color name="gold">#FFD700</color> <!-- 金色 --> <color name="pink">#FFC0CB</color> <!-- 粉红色 --> <color name="lightpink">#FFB6C1</color> <!-- 亮粉红色 --> <color name="orange">#FFA500</color> <!-- 橙色 --> <color name="lightsalmon">#FFA07A</color> <!-- 亮肉色 --> <color name="darkorange">#FF8C00</color> <!-- 暗桔黄色 --> <color name="coral">#FF7F50</color> <!-- 珊瑚色 --> <color name="hotpink">#FF69B4</color> <!-- 热粉红色 --> <color name="tomato">#FF6347</color> <!-- 西红柿色 --> <color name="orangered">#FF4500</color> <!-- 红橙色 --> <color name="deeppink">#FF1493</color> <!-- 深粉红色 --> <color name="fuchsia">#FF00FF</color> <!-- 紫红色 --> <color name="magenta">#FF00FF</color> <!-- 红紫色 --> <color name="red">#FF0000</color> <!-- 红色 --> <color name="oldlace">#FDF5E6</color> <!-- 老花色 --> <color name="lightgoldenrodyellow">#FAFAD2</color> <!-- 亮金黄色 --> <color name="linen">#FAF0E6</color> <!-- 亚麻色 --> <color name="antiquewhite">#FAEBD7</color> <!-- 古董白 --> <color name="salmon">#FA8072</color> <!-- 鲜肉色 --> <color name="ghostwhite">#F8F8FF</color> <!-- 幽灵白 --> <color name="mintcream">#F5FFFA</color> <!-- 薄荷色 --> <color name="whitesmoke">#F5F5F5</color> <!-- 烟白色 --> <color name="beige">#F5F5DC</color> <!-- 米色 --> <color name="wheat">#F5DEB3</color> <!-- 浅黄色 --> <color name="sandybrown">#F4A460</color> <!-- 沙褐色 --> <color name="azure">#F0FFFF</color> <!-- 天蓝色 --> <color name="honeydew">#F0FFF0</color> <!-- 蜜色 --> <color name="aliceblue">#F0F8FF</color> <!-- 艾利斯兰 --> <color name="khaki">#F0E68C</color> <!-- 黄褐色 --> <color name="lightcoral">#F08080</color> <!-- 亮珊瑚色 --> <color name="palegoldenrod">#EEE8AA</color> <!-- 苍麒麟色 --> <color name="violet">#EE82EE</color> <!-- 紫罗兰色 --> <color name="darksalmon">#E9967A</color> <!-- 暗肉色 --> <color name="lavender">#E6E6FA</color> <!-- 淡紫色 --> <color name="lightcyan">#E0FFFF</color> <!-- 亮青色 --> <color name="burlywood">#DEB887</color> <!-- 实木色 --> <color name="plum">#DDA0DD</color> <!-- 洋李色 --> <color name="gainsboro">#DCDCDC</color> <!-- 淡灰色 --> <color name="crimson">#DC143C</color> <!-- 暗深红色 --> <color name="palevioletred">#DB7093</color> <!-- 苍紫罗兰色 --> <color name="goldenrod">#DAA520</color> <!-- 金麒麟色 --> <color name="orchid">#DA70D6</color> <!-- 淡紫色 --> <color name="thistle">#D8BFD8</color> <!-- 蓟色 --> <color name="lightgray">#D3D3D3</color> <!-- 亮灰色 --> <color name="lightgrey">#D3D3D3</color> <!-- 亮灰色 --> <color name="tan">#D2B48C</color> <!-- 茶色 --> <color name="chocolate">#D2691E</color> <!-- 巧可力色 --> <color name="peru">#CD853F</color> <!-- 秘鲁色 --> <color name="indianred">#CD5C5C</color> <!-- 印第安红 --> <color name="mediumvioletred">#C71585</color> <!-- 中紫罗兰色 --> <color name="silver">#C0C0C0</color> <!-- 银色 --> <color name="darkkhaki">#BDB76B</color> <!-- 暗黄褐色 --> <color name="rosybrown">#BC8F8F</color> <!-- 褐玫瑰红 --> <color name="mediumorchid">#BA55D3</color> <!-- 中粉紫色 --> <color name="darkgoldenrod">#B8860B</color> <!-- 暗金黄色 --> <color name="firebrick">#B22222</color> <!-- 火砖色 --> <color name="powderblue">#B0E0E6</color> <!-- 粉蓝色 --> <color name="lightsteelblue">#B0C4DE</color> <!-- 亮钢兰色 --> <color name="paleturquoise">#AFEEEE</color> <!-- 苍宝石绿 --> <color name="greenyellow">#ADFF2F</color> <!-- 黄绿色 --> <color name="lightblue">#ADD8E6</color> <!-- 亮蓝色 --> <color name="darkgray">#A9A9A9</color> <!-- 暗灰色 --> <color name="darkgrey">#A9A9A9</color> <!-- 暗灰色 --> <color name="brown">#A52A2A</color> <!-- 褐色 --> <color name="sienna">#A0522D</color> <!-- 赭色 --> <color name="darkorchid">#9932CC</color> <!-- 暗紫色 --> <color name="palegreen">#98FB98</color> <!-- 苍绿色 --> <color name="darkviolet">#9400D3</color> <!-- 暗紫罗兰色 --> <color name="mediumpurple">#9370DB</color> <!-- 中紫色 --> <color name="lightgreen">#90EE90</color> <!-- 亮绿色 --> <color name="darkseagreen">#8FBC8F</color> <!-- 暗海兰色 --> <color name="saddlebrown">#8B4513</color> <!-- 重褐色 --> <color name="darkmagenta">#8B008B</color> <!-- 暗洋红 --> <color name="darkred">#8B0000</color> <!-- 暗红色 --> <color name="blueviolet">#8A2BE2</color> <!-- 紫罗兰蓝色 --> <color name="lightskyblue">#87CEFA</color> <!-- 亮天蓝色 --> <color name="skyblue">#87CEEB</color> <!-- 天蓝色 --> <color name="gray">#808080</color> <!-- 灰色 --> <color name="grey">#808080</color> <!-- 灰色 --> <color name="olive">#808000</color> <!-- 橄榄色 --> <color name="purple">#800080</color> <!-- 紫色 --> <color name="maroon">#800000</color> <!-- 粟色 --> <color name="aquamarine">#7FFFD4</color> <!-- 碧绿色 --> <color name="chartreuse">#7FFF00</color> <!-- 黄绿色 --> <color name="lawngreen">#7CFC00</color> <!-- 草绿色 --> <color name="mediumslateblue">#7B68EE</color> <!-- 中暗蓝色 --> <color name="lightslategray">#778899</color> <!-- 亮蓝灰 --> <color name="lightslategrey">#778899</color> <!-- 亮蓝灰 --> <color name="slategray">#708090</color> <!-- 灰石色 --> <color name="slategrey">#708090</color> <!-- 灰石色 --> <color name="olivedrab">#6B8E23</color> <!-- 深绿褐色 --> <color name="slateblue">#6A5ACD</color> <!-- 石蓝色 --> <color name="dimgray">#696969</color> <!-- 暗灰色 --> <color name="dimgrey">#696969</color> <!-- 暗灰色 --> <color name="mediumaquamarine">#66CDAA</color> <!-- 中绿色 --> <color name="cornflowerblue">#6495ED</color> <!-- 菊兰色 --> <color name="cadetblue">#5F9EA0</color> <!-- 军兰色 --> <color name="darkolivegreen">#556B2F</color> <!-- 暗橄榄绿 --> <color name="indigo">#4B0082</color> <!-- 靛青色 --> <color name="mediumturquoise">#48D1CC</color> <!-- 中绿宝石 --> <color name="darkslateblue">#483D8B</color> <!-- 暗灰蓝色 --> <color name="steelblue">#4682B4</color> <!-- 钢兰色 --> <color name="royalblue">#4169E1</color> <!-- 皇家蓝 --> <color name="turquoise">#40E0D0</color> <!-- 青绿色 --> <color name="mediumseagreen">#3CB371</color> <!-- 中海蓝 --> <color name="limegreen">#32CD32</color> <!-- 橙绿色 --> <color name="darkslategray">#2F4F4F</color> <!-- 暗瓦灰色 --> <color name="darkslategrey">#2F4F4F</color> <!-- 暗瓦灰色 --> <color name="seagreen">#2E8B57</color> <!-- 海绿色 --> <color name="forestgreen">#228B22</color> <!-- 森林绿 --> <color name="lightseagreen">#20B2AA</color> <!-- 亮海蓝色 --> <color name="dodgerblue">#1E90FF</color> <!-- 闪兰色 --> <color name="midnightblue">#191970</color> <!-- 中灰兰色 --> <color name="aqua">#00FFFF</color> <!-- 浅绿色 --> <color name="cyan">#00FFFF</color> <!-- 青色 --> <color name="springgreen">#00FF7F</color> <!-- 春绿色 --> <color name="lime">#00FF00</color> <!-- 酸橙色 --> <color name="mediumspringgreen">#00FA9A</color> <!-- 中春绿色 --> <color name="darkturquoise">#00CED1</color> <!-- 暗宝石绿 --> <color name="deepskyblue">#00BFFF</color> <!-- 深天蓝色 --> <color name="darkcyan">#008B8B</color> <!-- 暗青色 --> <color name="teal">#008080</color> <!-- 水鸭色 --> <color name="green">#008000</color> <!-- 绿色 --> <color name="darkgreen">#006400</color> <!-- 暗绿色 --> <color name="blue">#0000FF</color> <!-- 蓝色 --> <color name="mediumblue">#0000CD</color> <!-- 中兰色 --> <color name="darkblue">#00008B</color> <!-- 暗蓝色 --> <color name="navy">#000080</color> <!-- 海军色 --> <color name="black">#000000</color> <!-- 黑色 --> </resources>
styles.xml
<resources> <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. --> <style name="AppBaseTheme" parent="android:Theme.Light"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. --> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> </resources>