MissAmna
MissAmna

Reputation: 317

Override Activity onbackpressed method in Adapter

I have implemented 'BottomSheet' in my application. Calling its behaviour methods in my custom adapter. Now i want to collapsed or hide 'BottomSheet' when back button is pressed. For this how do i override 'onbackpressed' method in adapter to implement this.

Here is my Adapter.

public class CustomAdapter extends RecyclerView.Adapter<ViewHolder> {

LinearLayout bottom_sheet;
CoordinatorLayout mainLayout;

private BottomSheetBehavior mBottomSheetBehavior;

public CustomAdapter(Context context, ArrayList<Post> posts,LinearLayout bottom_sheet,CoordinatorLayout mainLayout) {
    this.posts = posts;
    this.context = context;
    this.bottom_sheet=bottom_sheet;
    this.mainLayout=mainLayout;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_layout, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
   mBottomSheetBehavior = BottomSheetBehavior.from(bottom_sheet);

    mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            switch (newState) {
                case BottomSheetBehavior.STATE_HIDDEN:
                    break;
                case BottomSheetBehavior.STATE_EXPANDED: {
                }
                break;
                case BottomSheetBehavior.STATE_COLLAPSED: {
                }
                break;
                case BottomSheetBehavior.STATE_DRAGGING:
                    break;
                case BottomSheetBehavior.STATE_SETTLING:
                    break;
            }
        }
        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {

        }
    });

    holder.comment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mBottomSheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            } else {
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
            }
        }
    });

}
@Override
public int getItemCount() {
    return posts.size();
}

}

Upvotes: 0

Views: 1469

Answers (2)

Santanu Sur
Santanu Sur

Reputation: 11487

        mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); 
        if(mBottomSheetBehavior.getState() !=     
            BottomSheetBehavior
                  .STATE_EXPANDED) {

             super.onBackPressed();

          }
            else {
                     mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);

            }

put this code in onBackPressed of your activity

Upvotes: 1

Lassi Kinnunen
Lassi Kinnunen

Reputation: 416

You can't.

Easy way is to override in your activity the onBackPressed() in the activity.

or make your thing open a dialog. or a context. or make a custom view and override onKeyDown(int keyCode, KeyEvent event). or use setOnKeyListener(View.OnKeyListener l) on bottom_sheet. docs link . you should return true as for the keypress to be consumed, however I cannot guarantee this to actually work for system keys nowadays.

Upvotes: 0

Related Questions