ListView 侧滑展示操作 http://download.csdn.net/download/g540203430/8040645

  1 package com.example.xupeng.swipelistview;
  2 
  3 /**
  4  * Created by xupeng on 15/3/25.
  5  */
  6 
  7 import java.util.ArrayList;
  8 import java.util.List;
  9 
 10 import android.content.Context;
 11 import android.view.LayoutInflater;
 12 import android.view.MotionEvent;
 13 import android.view.View;
 14 import android.view.ViewGroup;
 15 import android.view.ViewGroup.LayoutParams;
 16 import android.widget.BaseAdapter;
 17 import android.widget.Button;
 18 import android.widget.HorizontalScrollView;
 19 import android.widget.TextView;
 20 import android.widget.Toast;
 21 
 22 /**
 23  * ◊‘∂®“Âadapter
 24  *@Title:
 25  *@Description:
 26  *@Author:justlcw
 27  *@Since:2013-11-22
 28  *@Version:
 29  */
 30 class MyAdapter extends BaseAdapter implements View.OnClickListener
 31 {
 32     /**
 33      * ’‚∏ˆ”√¿¥ÃÓ≥‰list
 34      */
 35     private List<Integer> colors;
 36     /**
 37      * context…œœ¬Œƒ,”√¿¥ªÒµ√convertView
 38      */
 39     private Context mContext;
 40     /**
 41      * ∆¡ƒªøÌ∂»,”…”⁄Œ“√«”√µƒ «HorizontalScrollView,À˘“‘∞¥≈•—°œÓ”¶∏√‘⁄∆¡ƒªÕ‚
 42      */
 43     private int mScreentWidth;
 44 
 45     private boolean isOps;
 46 
 47     private int lastPosition;
 48     /**
 49      * ππ‘Ï∑Ω∑®
 50      * @param context
 51      * @param screenWidth
 52      */
 53     public MyAdapter(Context context, int screenWidth)
 54     {
 55         //≥ı ºªØ
 56         mContext = context;
 57         mScreentWidth = screenWidth;
 58         isOps = false;
 59         lastPosition = -1;
 60 
 61         //ÃÓ≥‰listµƒƒ⁄»›
 62         colors = new ArrayList<Integer>();
 63         for (int i = 0; i < 5; i++)
 64         {
 65             colors.add(android.R.color.holo_blue_bright);
 66         }
 67     }
 68 
 69     @Override
 70     public int getCount()
 71     {
 72         return colors.size();
 73     }
 74 
 75     @Override
 76     public Object getItem(int position)
 77     {
 78         return position;
 79     }
 80 
 81     @Override
 82     public long getItemId(int position)
 83     {
 84         return position;
 85     }
 86 
 87     public boolean getIsOps() {
 88         return isOps;
 89     }
 90 
 91     public void setIsOps(boolean isOps) {
 92         this.isOps = isOps;
 93     }
 94 
 95     @Override
 96     public View getView(final int position, View convertView, ViewGroup parent)
 97     {
 98         final ViewHolder holder;
 99         //»Áπ˚√ª”–…Ë÷√π˝,≥ı ºªØconvertView
100         if (convertView == null)
101         {
102             //ªÒµ√…Ë÷√µƒview
103             convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list, parent, false);
104 
105             //≥ı ºªØholder
106             holder = new ViewHolder();
107             holder.hSView = (HorizontalScrollView) convertView.findViewById(R.id.hsv);
108 
109             holder.action = convertView.findViewById(R.id.ll_action);
110             holder.btOne = (Button) convertView.findViewById(R.id.button1);
111             holder.btTwo = (Button) convertView.findViewById(R.id.button2);
112             holder.btThree = (Button) convertView.findViewById(R.id.button3);
113 
114             holder.bt = (Button) convertView.findViewById(R.id.bt);
115             holder.bt.setOnTouchListener(new View.OnTouchListener() {
116                 @Override
117                 public boolean onTouch(View v, MotionEvent event) {
118                     if (isOps) {
119                         isOps = false;
120                         notifyDataSetChanged();
121                     }
122                     return false;
123                 }
124             });
125 
126             //∞—Œª÷√∑≈µΩview÷–,’‚—˘µ„ª˜ ¬º˛æÕø…“‘÷™µ¿µ„ª˜µƒ «ƒƒ“ªÃıitem
127             holder.btOne.setTag(position);
128             holder.btTwo.setTag(position);
129             holder.btThree.setTag(position);
130 
131             holder.tvContent = (TextView) convertView.findViewById(R.id.tv);
132 
133             //…Ë÷√ƒ⁄»›viewµƒ¥Û–°Œ™∆¡ƒªøÌ∂»,’‚—˘∞¥≈•æÕ’˝∫√±ªº∑≥ˆ∆¡ƒªÕ‚
134             holder.content = convertView.findViewById(R.id.ll_content);
135             LayoutParams lp = holder.content.getLayoutParams();
136             lp.width = mScreentWidth;
137 
138             convertView.setTag(holder);
139         }
140         else//”–÷±Ω”ªÒµ√ViewHolder
141         {
142             holder = (ViewHolder) convertView.getTag();
143         }
144 
145         //…Ë÷√º‡Ã˝ ¬º˛
146         convertView.setOnTouchListener(new View.OnTouchListener()
147         {
148             @Override
149             public boolean onTouch(View v, MotionEvent event)
150             {
151                 switch (event.getAction())
152                 {
153                     case MotionEvent.ACTION_UP:
154 
155 
156                         //ªÒµ√ViewHolder
157                         ViewHolder viewHolder = (ViewHolder) v.getTag();
158 
159                         //ªÒµ√HorizontalScrollViewª¨∂صƒÀÆ∆Ω∑ΩœÚ÷µ.
160                         int scrollX = viewHolder.hSView.getScrollX();
161 
162                         //ªÒµ√≤Ÿ◊˜«¯”Úµƒ≥§∂»
163                         int actionW = viewHolder.action.getWidth();
164 
165                         //◊¢“‚ π”√smoothScrollTo,’‚—˘–ßπ˚ø¥∆¿¥±»Ωœ‘≤ª¨,≤ª…˙”≤
166                         //»Áπ˚ÀÆ∆Ω∑ΩœÚµƒ“∆∂Ø÷µ<≤Ÿ◊˜«¯”Úµƒ≥§∂»µƒ“ª∞Î,æÕ∏¥‘≠
167                         if (scrollX < actionW / 2)
168                         {
169                             viewHolder.hSView.smoothScrollTo(0, 0);
170                             isOps = false;
171                         }
172                         else//∑Ò‘Úµƒª∞œ‘ æ≤Ÿ◊˜«¯”Ú
173                         {
174                             viewHolder.hSView.smoothScrollTo(actionW, 0);
175                             isOps = true;
176                         }
177                         lastPosition = position;
178                         return true;
179                     case MotionEvent.ACTION_DOWN:
180                        // if (lastPosition != position) {
181                        if (isOps) {
182                             isOps = false;
183                             notifyDataSetChanged();
184                         }
185                         lastPosition = position;
186                         return true;
187                 }
188                 return false;
189             }
190         });
191 
192 
193 
194         holder.bt.setOnClickListener(new View.OnClickListener() {
195             @Override
196             public void onClick(View v) {
197                 Toast.makeText(mContext, position + "", Toast.LENGTH_SHORT).show();
198             }
199         });
200 
201         //’‚¿Ô∑¿÷π…æ≥˝“ªÃıitem∫Û,ListView¥¶”⁄≤Ÿ◊˜◊¥Ã¨,÷±Ω”ªπ‘≠
202         if (holder.hSView.getScrollX() != 0)
203         {
204             holder.hSView.smoothScrollTo(0, 0);
205         }
206 
207         //…Ë÷√±≥æ∞—’…´,…Ë÷√ÃÓ≥‰ƒ⁄»›.
208         holder.content.setBackgroundResource(colors.get(position));
209         holder.tvContent.setText("" + position);
210 
211         //…Ë÷√º‡Ã˝ ¬º˛
212         holder.btOne.setOnClickListener(this);
213         holder.btTwo.setOnClickListener(this);
214         holder.btThree.setOnClickListener(this);
215 
216         return convertView;
217     }
218 
219     /**
220      * ViewHolder
221      *@Title:
222      *@Description:÷˜“™ «±‹√‚¡À≤ª∂œµƒviewªÒ»°≥ı ºªØ.
223      *@Author:justlcw
224      *@Since:2013-11-22
225      *@Version:
226      */
227     class ViewHolder
228     {
229         public HorizontalScrollView hSView;
230 
231         public View content;
232         public TextView tvContent;
233 
234         public View action;
235         public Button btOne;
236         public Button btTwo;
237         public Button btThree;
238 
239         public Button bt;
240     }
241 
242     @Override
243     public void onClick(View v)
244     {
245         int position = (Integer) v.getTag();
246         switch (v.getId())
247         {
248             case R.id.button1:
249 
250                 colors.add(android.R.color.holo_blue_bright);
251                 break;
252 
253             case R.id.button2:
254 
255                 colors.remove(position);
256                 break;
257 
258             case R.id.button3:
259 
260                 if (colors.get(position) == android.R.color.holo_blue_bright)
261                 {
262                     colors.set(position, android.R.color.holo_red_light);
263                 }
264                 else
265                 {
266                     colors.set(position, android.R.color.holo_blue_bright);
267                 }
268                 break;
269 
270             default:
271                 break;
272         }
273         //À¢–¬ListViewƒ⁄»›
274         notifyDataSetChanged();
275     }
276 }
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     tools:context=".MainActivity">
 5 
 6     <ListView
 7         android:id="@+id/lv_slide"
 8         android:layout_width="fill_parent"
 9         android:layout_height="fill_parent" />
10 
11 
12 </LinearLayout>
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:id="@+id/hsv"
 4     android:layout_width="wrap_content"
 5     android:layout_height="80dip"
 6     android:scrollbars="none" >
 7 
 8     <LinearLayout
 9         android:layout_width="wrap_content"
10         android:layout_height="match_parent"
11         android:orientation="horizontal" >
12 
13         <LinearLayout
14             android:id="@+id/ll_content"
15             android:layout_width="match_parent"
16             android:layout_height="match_parent"
17             android:orientation="horizontal" >
18 
19             <TextView
20                 android:id="@+id/tv"
21                 android:layout_width="0dp"
22                 android:layout_height="match_parent"
23                 android:layout_weight="1"
24                 android:gravity="center"
25                 android:text="" />
26 
27             <Button
28                 android:id="@+id/bt"
29                 android:layout_width="0dp"
30                 android:layout_height="wrap_content"
31                 android:layout_weight="1"
32                 android:text="click" />
33 
34         </LinearLayout>
35 
36         <LinearLayout
37             android:id="@+id/ll_action"
38             android:layout_width="wrap_content"
39             android:layout_height="match_parent"
40             android:orientation="horizontal" >
41 
42             <Button
43                 android:id="@+id/button1"
44                 android:layout_width="wrap_content"
45                 android:layout_height="match_parent"
46                 android:background="@android:color/darker_gray"
47                 android:paddingLeft="20dip"
48                 android:paddingRight="20dip"
49                 android:text="action1" />
50 
51             <Button
52                 android:id="@+id/button2"
53                 android:layout_width="wrap_content"
54                 android:layout_height="match_parent"
55                 android:layout_marginLeft="2dip"
56                 android:layout_marginRight="2dip"
57                 android:background="@android:color/darker_gray"
58                 android:paddingLeft="20dip"
59                 android:paddingRight="20dip"
60                 android:visibility="gone"
61                 android:text="action2" />
62 
63             <Button
64                 android:id="@+id/button3"
65                 android:layout_width="wrap_content"
66                 android:layout_height="match_parent"
67                 android:background="@android:color/darker_gray"
68                 android:paddingLeft="20dip"
69                 android:paddingRight="20dip"
70                 android:visibility="gone"
71                 android:text="action3" />
72         </LinearLayout>
73     </LinearLayout>
74 
75 </HorizontalScrollView>

posted on 2015-03-26 01:31  mimamo  阅读(178)  评论(0)    收藏  举报

导航