2016-03-27 20 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への呼び出しを考えます。項目「アクション中」に個別の変数を作成する必要があるかもしれません

1

基本的に達成するものはスワイプで効果を消すことです。

リストビューをRecyclerViewに変更することをお勧めします。 Swipe to Dismiss for RecyclerViewそのような効果を達成する方法を読む: