FanFataL / swipe-controller-demo

This is source code from the article about creating swipe menu with RecyclerView without any external libraries.
284 stars 127 forks source link

onClick Method #7

Closed eymel closed 6 years ago

eymel commented 6 years ago

I want to use onclick when item clicked. how can I use onclick method?

jerome2710 commented 6 years ago

Take a look at the MainActivity.java. You may use the abstract class SwipeControllerActions to hook into click events.

eymel commented 6 years ago

I checked MainActivity. But I can't use click and left-right action together. These actions conflict with swipecontrolleractions click method.

jerome2710 commented 6 years ago

These do not conflict with the SwipeControllerActions-methods. This class is an abstract class, which should be implemented in your activity like the MainActivity example. The SwipeControllerActions-class should be left untouched.

eymel commented 6 years ago

Thank you for answer . For Example, I added SwipeControllerActions onClickItem but I can't catch this method because when I click Left or Right method . I change setItemsClickable in setTouchListener method but not running.

//SwipeControllerActions
public abstract class SwipeControllerActions {
    public void onLeftClicked(int position) {
    }

    public void onRightClicked(int position) {
    }

    public void onClickItem(int position) {
    }
}

//MainActivity
  swipeController = new SwipeController(new SwipeControllerActions() {
            @Override
            public void onRightClicked(int position) {
                baseActivity.replaceFragment(new Fragment());
            }

            @Override
            public void onClickItem(int position) {
                super.onClickItem(position);
            }

            @Override
            public void onLeftClicked(int position) {
                super.onLeftClicked(position);
            }
        });
        ItemTouchHelper itemTouchhelper = new ItemTouchHelper(swipeController);
        itemTouchhelper.attachToRecyclerView(recyclerView);
        recyclerViewDoctor.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                swipeController.onDraw(c);
            }

        });
vkovalch commented 6 years ago

OnClick method will catch via holder of RecyclerView in onBindViewHolder.

public interface ClickItemListener { void OnItemClick(SomeModel someModel); }

public class MainActivity extends AppCompatActivity implements ClickItemListener {

private RecyclerView mRecyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   mRecyclerView =  findViewById(R.id.somelist);
  mRecyclerView.recyclerView.setLayoutManager(new LinearLayoutManager(this));
  mRecyclerView.setAdapter(someAdapter(data, **this**));
    swipeController = new SwipeController(new SwipeControllerActions() {
        @Override
        public void onRightClicked(int position) {
             //catch swipe 
        }

        @Override
        public void onLeftClicked(int position) {
            //catch swipe 
        }
    });

    ItemTouchHelper itemTouchhelper = new ItemTouchHelper(swipeController);
    itemTouchhelper.attachToRecyclerView(recyclerView);

    recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            swipeController.onDraw(c);
        }
    });
  . . . 
  }

@Override
public void OnItemClick(SomeModel someModel) {
       /// catch click
}

}

public class SomeAdapter extends RecyclerView.Adapter {

private ClickItemListener mListener; private List data; public SomeAdapter(List data, ClickItemListener listener){ this.data = data; this.mListener = listener; }

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
     SomeModel sm = data.get(position) ;
    holder.someview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mListener!=null){
                mListener.OnItemClick(sm);
            }
        }
    });
}

... }

eymel commented 6 years ago

Thank you for answer. I didn't resolve this problem because when swipe left or right setItemsClickable(false) run . So I can't catch onclick method . I resolved this problem with https://medium.com/@kitek/recyclerview-swipe-to-delete-easier-than-you-thought-cff67ff5e5f6 this article.

vkovalch commented 6 years ago

I can help you, if U share you part copy of code? and i will edit your file