天天看點

Android開發-實作pulltorefresh上類似購物車加減方法和EditText焦點問題的解決方法

需求:在pulltorefresh上實作購物車加減和手寫數量的功能。

先上圖

Android開發-實作pulltorefresh上類似購物車加減方法和EditText焦點問題的解決方法
Android開發-實作pulltorefresh上類似購物車加減方法和EditText焦點問題的解決方法

下面隻貼出擴充卡的代碼:

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清單資料中,每一步操作就重新整理一次清單,就可以簡單實作這個功能。