Android开发-实现pulltorefresh上类似购物车加减方法和EditText焦点问题的解决方法

需求:在pulltorefresh上实现购物车加减和手写数量的功能。

先上图





下面只贴出适配器的代码:

class MyPullListViewAdapter extends BaseAdapter {  
	    Context context;// 上下文   
	    private int index = -1;
	    
	    public MyPullListViewAdapter(Context context) {  
	        super();  
	        this.context = context;   
	    }  
  
	    @Override  
	    public int getCount() {  
	        // TODO Auto-generated method stub  
	        return mDataLists.size();  
	    }  
	  
	    @Override  
	    public Object getItem(int position) {  
	        // TODO Auto-generated method stub  
	        return mDataLists.get(position);  
	    }  
	  
	    @Override  
	    public long getItemId(int position) {  
	        // TODO Auto-generated method stub  
	        return position;  
	    }  
	    
	    // getView什么时候执行  
	    @Override  
	    public View getView(final int position, View convertView, ViewGroup parent) {  
	        ViewHolder holder;
                       
            try{
            	if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.listview_circulate_purchase_business_all_goods,null);
                    holder = new ViewHolder();
                    /**得到各个控件的对象*/   
                    holder.text_goods_name = (TextView) convertView.findViewById(R.id.text_goods_name);
                    holder.text_goods_price = (TextView) convertView.findViewById(R.id.text_goods_price);  
                    holder.img_reduce = (ImageView) convertView.findViewById(R.id.img_reduce);                    
                    holder.img_add = (ImageView) convertView.findViewById(R.id.img_add);                                      
                    holder.edit_goods_amount = (EditText) convertView.findViewById(R.id.edit_goods_amount);                   
                    convertView.setTag(holder);//绑定ViewHolder对象          
                    
                }else{
                    holder = (ViewHolder)convertView.getTag();//取出ViewHolder对象                   
                }
            	
                Map<String, String> m = mDataLists.get(position);          
                holder.text_goods_name.setText(Commons.format(m.get("GOODS_NAME")));
                holder.text_goods_price.setText("单价: ¥"+Commons.format(m.get("PRICE")));
                holder.edit_goods_amount.setText(Commons.format(m.get("SELECT_GOODS_NUM")));
                holder.img_add.setTag(position);//保存位置信息
                holder.img_reduce.setTag(position); //保存位置信息    
  
                //设置焦点变化监听
		holder.edit_goods_amount.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
		<span style="white-space:pre">	</span>@Override
			public void onFocusChange(View v, boolean hasFocus) {
			<span style="white-space:pre">	</span>if (hasFocus) {
				// 此处为得到焦点时的处理内容							
				<span style="white-space:pre">	</span>index = position;//保存焦点的所在列表位置					
				} else {
				// 此处为失去焦点时的处理内容
<span style="white-space:pre">					</span>String tempValue = ((EditText)v).getText().toString();
<span style="white-space:pre">					</span>int t = Integer.valueOf("".equals(tempValue) ? "1":tempValue); //获取失去焦点时的内容
					if(t > 0) {
	 				<span style="white-space:pre">	</span>Map<String, String> tempMap = mDataLists.get(Integer.valueOf(index));												
						tempMap.put("SELECT_GOODS_NUM", t+""); //更新列表内容
						mMyPullListViewAdapter.notifyDataSetChanged();
					}
				}
			}
		});
                
                holder.edit_goods_amount.clearFocus();
				if (index != -1 && index == position) {
					// 如果当前的行下标和点击事件中保存的index一致,手动为EditText设置焦点。
					holder.edit_goods_amount.requestFocus();
				}
           
				//点击加号方法
                holder.img_add.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {						
						Map<String, String> tempMap = mDataLists.get(Integer.valueOf(arg0.getTag().toString()));
						int num = Integer.valueOf(tempMap.get("SELECT_GOODS_NUM"));						
						tempMap.put("SELECT_GOODS_NUM", (num + 1)+"");
						mMyPullListViewAdapter.notifyDataSetChanged();
					}
				});
                //点击减号方法
                holder.img_reduce.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						Map<String, String> tempMap = mDataLists.get(Integer.valueOf(arg0.getTag().toString()));
						int num = Integer.valueOf(tempMap.get("SELECT_GOODS_NUM"));	
						if(num > 1) {
							tempMap.put("SELECT_GOODS_NUM", (num - 1)+"");
							mMyPullListViewAdapter.notifyDataSetChanged();					
						}
					}
				});
               
            }catch (Exception e) {
    			e.printStackTrace();
    			Logger.showToastDialog(context, "系统异常");
    		}
	        return convertView;  
	    }  
	  
	    public final class ViewHolder{
		    public CheckBox cb_unit;
		    public TextView text_goods_name;
		    public TextView text_goods_price;
		    public ImageView img_reduce;
		    public ImageView img_add;
		    public EditText edit_goods_amount;
	    }	    	    
	}


listview_circulate_purchase_business_all_goods.xml (列表项的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="100dp"
    android:layout_margin="10dp"
    android:orientation="horizontal" >
    
	<LinearLayout 
	    android:layout_width="0dp"
	    android:layout_height="100dp"
	    android:layout_weight="1"
	    android:layout_marginLeft="20dp"
	    android:gravity="center_vertical"
	    android:orientation="horizontal">

	    <CheckBox
	        android:id="@+id/cb_unit"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:layout_gravity="center_vertical"
	        android:checked="false">
	    </CheckBox>
	    <LinearLayout 
	        android:layout_width="0dp"
	        android:layout_weight="1"
	        android:layout_marginLeft="30dp"
	        android:layout_height="match_parent"
	        android:orientation="vertical"
	        android:gravity="center_vertical">
	        <TextView 
	            android:id="@+id/text_goods_name"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="xxxxxx面包"
	            android:singleLine="true"
	            android:layout_weight="1"
	            android:gravity="center_vertical"
	            android:ellipsize="end"
	            android:textSize="18sp"/>
	        <TextView 
	            android:id="@+id/text_goods_price"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="单价:¥11"
	            android:singleLine="true"
	            android:gravity="center_vertical"
	            android:layout_weight="1"
	            android:ellipsize="end"
	            android:textColor="#ff6600"
	            android:textSize="18sp"/>
	    </LinearLayout>

	</LinearLayout>
	<LinearLayout 
	    android:layout_width="0dp"
	    android:layout_height="100dp"
	    android:layout_weight="1"
	    android:layout_marginRight="20dp"
	    android:gravity="center_vertical|right"
	    android:orientation="horizontal">
	    <ImageView 
	        android:id="@+id/img_reduce"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/shop_cart_reduce"/>
	    <LinearLayout 
	        android:layout_width="70dp"
	        android:layout_height="40dp"
	        android:orientation="vertical"
	        android:gravity="center"
	        android:background="@drawable/shopping_cart_edittext">
	        <EditText 
	            android:id="@+id/edit_goods_amount"
		        android:layout_width="70dp"
		        android:gravity="center"
		        android:layout_height="match_parent"
		        android:background="@null"
		        android:singleLine="true"
		        android:inputType="number"/>
	    </LinearLayout>	    
	    <ImageView 
	        android:id="@+id/img_add"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:layout_marginRight="10dp"
	        android:src="@drawable/shop_cart_add"/>
	</LinearLayout>
</LinearLayout>



主要的思路就是,将数量加入到mDataLists列表数据中,每一步操作就刷新一次列表,就可以简单实现这个功能。

猜你喜欢

转载自blog.csdn.net/yozheng/article/details/52072709