daimajia / AndroidSwipeLayout

The Most Powerful Swipe Layout!
MIT License
12.38k stars 2.67k forks source link

swipelayout issue (listview remove and add) #213

Open wk476855 opened 9 years ago

wk476855 commented 9 years ago

When I remove the last swipelayout from the listview and then notifydatachanged, then add another new swipelayout in the listview, i get the swipe layout status is close, but it shows open, why?

here is listview item xml: <?xml version="1.0" encoding="utf-8"?> <com.daimajia.swipe.SwipeLayout xmlns:swipe="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipe" android:layout_width="match_parent" swipe:leftEdgeSwipeOffset="0dp" swipe:rightEdgeSwipeOffset="0dp" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:padding="10dp" android:background="#FF5534" android:gravity="center_vertical" android:tag="Bottom3" android:weightSum="10"> <ImageView android:id="@+id/trash" android:layout_width="27dp" android:layout_height="30dp" android:layout_weight="1" android:src="@drawable/trash" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:padding="5dp" android:layout_height="50dp">

<TextView
    android:id="@+id/textview1"
    android:layout_width="0dp"
    android:text="西红柿炒牛腩"
    android:layout_gravity="center"
    android:layout_weight="2"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textview2"
    android:layout_width="0dp"
    android:text="100g"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textview3"
    android:layout_width="0dp"
    android:text="234kcal"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:layout_height="wrap_content" />

</com.daimajia.swipe.SwipeLayout>

here is my adapter: class RecipeAdapter extends BaseSwipeAdapter {

    Context context;
    List<Object> data;

    public RecipeAdapter(Context context, List<Object> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getSwipeLayoutResourceId(int i) {
        return R.id.swipe;
    }

    @Override
    public View generateView(int position, ViewGroup viewGroup) {
        View v = LayoutInflater.from(context).inflate(R.layout.plan_select_recipe_item, null);
        final SwipeLayout swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position));
        swipeLayout.addSwipeListener(new SimpleSwipeListener() {
            @Override
            public void onOpen(SwipeLayout layout) {
                YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash));
            }

        });
        return v;
    }

    @Override
    public void fillValues(final int i, View view) {
        Log.v(i + "", view.toString());
        TextView text1 = (TextView) view.findViewById(R.id.textview1);
        TextView text2 = (TextView) view.findViewById(R.id.textview2);
        TextView text3 = (TextView) view.findViewById(R.id.textview3);
        Object obj = data.get(i);
        if(obj instanceof Recipe) {
            text1.setText(((Recipe)obj).getTitle());
            text2.setText(((Recipe)obj).getTotal_amount() + "g");
            text3.setText(Math.round(((Recipe)obj).getCalories() * ((Recipe)obj).getTotal_amount() / 100) + "kcal");
        }

        view.findViewById(R.id.trash).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                data.remove(i);
                notifyDataSetChanged();
                Toast.makeText(context, "click delete", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}

image

wk476855 commented 9 years ago

However, when i delete the the last swipelayout after it complete the animation of the close, it seems run correctly! But when it delete the layout, i don't want to have this close animatiom, what should I do?