2016-03-27 46 views
2

我想設置特定列表視圖項的左邊距,但是當我嘗試更改它時,列表視圖的整個邊距被更改。我想通過改變特定視圖的邊界來實現向左滑動和刪除視圖(假設第一個視圖,請參閱img1)。下面你可以找到一些代碼...將保證金設置爲一個列表視圖項,而不會影響整個列表視圖的邊距

/*custom adapter*/ 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) myContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     convertView = infalInflater.inflate(R.layout.summary_list_item1, null); 
     convertView.setTag(position); 

     TextView tt1 = (TextView) convertView.findViewById(R.id.listText); 

     tt1.setText(myItems.get(position)); 
     } 

     return convertView; 
} 

/*onTouchListener*/  
lv.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      Log.i("onTouch", "Down"); 
      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        historicX = event.getX(); 
        historicY = event.getY(); 
        /*keep view first time that is touched*/ 
        if (removeView == null) { 
         removeView = v; 
        } 
        break; 

       case MotionEvent.ACTION_MOVE: 
        performSlideViewEffect(v, historicX, historicY, event.getX(), event.getY()); 

        historicX = event.getX(); 
        historicY = event.getY(); 
        break; 
      } 
    } 

public void performSlideViewEffect(View v,float historicX,float historicy,float x,float y) { 

    TextView tt1 = (TextView) v.findViewById(R.id.listText); 

    LinearLayout.MarginLayoutParams lp = (LinearLayout.MarginLayoutParams) v.getLayoutParams(); 

    lp.setMarginStart(lp.leftMargin - Math.round(historicX - x)); 
    v.setLayoutParams(lp); 
    v.invalidate(); 
} 

/*Layout with the ListView*/ 

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:background="#f4f4f4" 
android:scrollbars="vertical"> 


<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="50sp" 
    android:layout_gravity="center_horizontal" 
    android:id="@+id/table" 
    android:padding="10sp" 
    android:textSize="20sp" 
    android:text="" 
    android:textStyle="bold|italic"/> 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="3sp" 
    android:background="#EFEFEF" 
    android:layout_margin="2sp" /> 

<ListView 
    android:id="@+id/summary_list" 
    android:layout_height="wrap_content" 
    android:layout_width="280sp" 
    android:background="#E7E7E7" 
    android:layout_marginTop="5sp" 
    android:layout_gravity="center_horizontal" 
    android:scrollbars="vertical"> 
</ListView> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/submit_order" 
    android:text="@string/SubmitOrder" 
    android:layout_gravity="center_horizontal"/> 

/*summary_list_item1.xml*/ 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
<TextView 
    android:id="@+id/listText" 
    android:tag="" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="14sp" 
    android:textColor="#ffffff" 
    android:layout_marginTop="2sp" 
    android:layout_marginBottom="1sp" 
    android:layout_marginLeft="3sp" 
    android:layout_marginRight="3sp" 
    android:padding="15sp" 
    android:background="#000000" 
    android:layout_gravity="center_vertical|center_horizontal" /> 
</LinearLayout> 

的初始佈局如下,之前我選擇移動第一 img1

但我得到的是將要移動的所有觀點......不僅是「感動」的.. 。 img2

回答

0

有一點猜測的:我想呼籲(LinearLayout.MarginLayoutParams) v.getLayoutParams();返回通用佈局params用於在所有項目。您可能需要爲某個項目創建單個變量「in action」

1

基本上你想實現的是一個刷卡來解僱的效果。

最好的辦法是將ListView改爲RecyclerView。 閱讀答案Swipe to Dismiss for RecyclerView如何實現這樣的效果:

相關問題