0

我想打开一个关于刷 recyclerview 项目的意图。我在 onSwiped 事件中有以下类错误。

import android.support.v7.widget.helper.ItemTouchHelper.Callback;
import static android.support.v7.widget.helper.ItemTouchHelper.*;
class SwipeController extends Callback {
    private boolean swipeBack = false;

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        return makeMovementFlags(0, LEFT);
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        // Create Intent to show the Item details
        Intent intent = new Intent( getActivity(), CreateBoxActivity.class);
        intent.putExtra("parent_BoxID", parent_BoxID);
        MainActivity.this.startActivityForResult(intent, CREATE_BOX_REQUEST ); ;

    }

}
4

1 回答 1

1

尝试这个:

public class SwipeController extends Callback {
    private boolean swipeBack = false;
    private WeakReference<Activity> context;

    public SwipeController(Activity a) {
        context = new WeakReference<>(a);
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        return makeMovementFlags(0, LEFT);
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        // Create Intent to show the Item details
        Intent intent = new Intent( getActivity(), CreateBoxActivity.class);
        intent.putExtra("parent_BoxID", parent_BoxID);
        Activity a = context.get();
        if (a != null && !a.isFinishing())
              a.startActivityForResult(intent, CREATE_BOX_REQUEST );
    }

}
于 2018-05-01T14:28:49.393 回答