Android: open dialog on adapter

给你一囗甜甜゛ 提交于 2019-12-11 12:22:13

问题


How do I open a DialogFragment inside a RecyclerView Adapter, or how do I do it through the main activity? I need to open it by clicking on every view that is added into the RecyclerView.


回答1:


This is what I normally do when dealing with recyclerView items click events:

Create a click listener to reuse later.

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
   @SuppressWarnings("CanBeFinal")
   private OnItemClickListener mListener;

   public interface OnItemClickListener {
       void onItemClick(View view, int position);
   }

   @SuppressWarnings("CanBeFinal")
   private GestureDetector mGestureDetector;

   public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
       mListener = listener;
       mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
           @Override
           public boolean onSingleTapUp(MotionEvent e) {
              return true;
           }
      });
   }

   @Override
   public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
       View childView = view.findChildViewUnder(e.getX(), e.getY());
       if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
           mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
       }
       return false;
   }

   @Override
   public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
   }

   @Override
   public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

   }}

Then to use it in your Activity, do this:

mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            showDialog();
        }
 }));

I hope this helps!




回答2:


Inside your RecyclerviewAdapter, and in the onClick method of the view (the view that clicked shows this dialog), u can place the below code. Edited:

MyDialogFragment newFragment = MyDialogFragment.newInstance();
newFragment.show(((Activity) context).getSupportFragmentManager(), "Title");

Note: the "context" is the context of the activity from where the recyclerview is created. (the context variable passed as an argument in the cnstructor of RecyclerviewAdapter.)

Note2: MyDialogFragment is a dialogfragment that u created. In this MyDialogFragment u should have the constructor newInstance()

The MyDialogFragment can look like this:

public class MyDialogFragment extends DialogFragment {

public static MyDialogFragment newInstance() {
    MyDialogFragment frag = new MyDialogFragment();

    return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.fragment_myDialog, null))
            .setTitle("Title")
            ...
           // Set ur code 

    return builder.create();
   }
}

So now for how to pass the context, from the mainActivity, when u create the adapter n the activity context that u pass to the adapter is the context i m talking about. For Example: the "this" in the code below is the activity context that u will be passing to your adapter. This code is present in ur MainActivity.

 MyAdapter adapter1 = new MyAdapter(this, array_list);
    mRecyclerView.setAdapter(adapter1);



回答3:


Set this on the DialogFragment:

 public static myFragment newInstance() {
    return new myFragment();
}

Set this on the adapter:

public Activity mcontext;

public SubjectsAdapter(Activity context) {
// Here we're getting the activity's context, 
// by setting the adapter on the activity with (this)   
this.mcontext=context;

}

Show it in through the adapter like this:

 myFragment newFragment = myFragment.newInstance();
 newFragment.show(mcontext.getFragmentManager(), "Title");


来源:https://stackoverflow.com/questions/36582037/android-open-dialog-on-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!