2

i want to set the left margin of a specific listview item, but when i try to change it, the whole margin of listview is changed. I want to achieve the swipe left and delete of view (let's say the first one, see img1), by altering the margins of the specific view. Below you can find some code...

/*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>

The initial layout is the following, before i choose to move the first img1

But what i get is all the views to be moved...not only the "touched" one... img2

2 Answers2

1

Basically what you want to achieve is a swipe to dismiss effect.

Your best bet is to change your ListView to a RecyclerView. Read the answer Swipe to Dismiss for RecyclerView how to achieve such effect :

Community
  • 1
  • 1
mt0s
  • 5,781
  • 10
  • 42
  • 53
0

There is a bit of a guess: I think call to (LinearLayout.MarginLayoutParams) v.getLayoutParams(); returns common layout params for all items. You may need to create individual variable for an item "in action"

eddie
  • 56
  • 4