浮动上下文菜单RecyclerView

时间:2017-12-30 18:44:39

标签: android android-recyclerview

下面, 他们很少发现将Floating Context Menu添加到RecyclerView项目和许多不同的信息。我想要一个单一的上下文菜单按钮,我已经用这种方式实现了它:

itemView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                MenuItem delete = menu.add(0, v.getId(), 0, "Delete");
                delete.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        int position = getAdapterPosition();
                        long id = mUploads.get(position).getId();
                        if (position != RecyclerView.NO_POSITION) {
                            mListener.onDeleteClick(position, id);
                        }
                        return true;
                    }
                });

            }
        });

然后调用此接口方法:

 @Override
public void onDeleteClick(int position, long id) {
    Toast.makeText(this, "position: " + position + " ID: " + id, Toast.LENGTH_SHORT).show();
}

然而,这一切看起来都非常混乱,我想知道这种方法以后是否会导致问题。

1 个答案:

答案 0 :(得分:0)

我经常使用自定义对话框,所以我使用DialogFragment。请注意,此对话框具有“确定”和“取消”按钮。如果您不需要,可以删除按钮。

您需要为自定义DialogFragment“fragment_submit_cancel_dialog”创建XML布局。创建自己的设计的能力为您的对话框外观提供了极大的灵活性。这是一个简单的例子,但可以根据需要进行修改 - 甚至可以添加图像或列表。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">


    <TextView
        android:id="@+id/tvTitle"
        android:text="Title"
        style="@style/alert_dialog_title"
        />

    <TextView
        android:id="@+id/tvMessage"
        android:text="MyMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_below="@id/tvTitle"
        android:layout_centerInParent="true"
        android:layout_marginTop="15dp"
        />

</RelativeLayout>

在您调用DialogFragment的活动中,您需要添加以下内容:

implements  OkCancelDialogFragment.OkCancelDialogListener{

并添加侦听器方法:

@Override
public void onFinishOkCancelDialog(boolean submit) {
    if(submit){
        // Do what you need here
    }
}

像这样呼叫DialogFragment

private void startOkDialog(){
    String title = "What ever you want as a Title";
    String mess = "Your Message!";
    OkCancelDialogFragment dialog = OkCancelDialogFragment.newInstance(title, mess);
    show(getFragmentManager(), "OkDialogFragment");
}

现在自定义对话框片段的代码:

public class OkCancelDialogFragment extends DialogFragment {

    private static final String ARG_TITLE = "title";
    private static final String ARG_MESSAGE = "message";

    Context context = null;

    private String title;
    private String message;
    private boolean submitData = false;

    private OkCancelDialogListener mListener;

    public OkCancelDialogFragment() {
    }

    public static OkCancelDialogFragment newInstance(String title, String message) {
        OkCancelDialogFragment fragment = new OkCancelDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_TITLE, title);
        args.putString(ARG_MESSAGE, message);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            title = getArguments().getString(ARG_TITLE);
            message = getArguments().getString(ARG_MESSAGE);
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle saveIntsanceState){

        context = getActivity();

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        View rootView = inflater.inflate(R.layout.fragment_submit_cancel_dialog, null, false);
        final TextView titleView = (TextView)rootView.findViewById(R.id.tvTitle);
        final TextView messView = (TextView)rootView.findViewById(R.id.tvMessage);

        titleView.setText(title);
        messView.setText(message);

        builder.setView(rootView)
                .setPositiveButton(R.string.button_Ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        submitData = true;
                        //The onDetach will call the Listener! Just in case the user taps the back button
                    }
                })
                .setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        submitData = false;
                    }
                });
        return builder.create();
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            if(mListener == null) mListener = (OkCancelDialogListener) context;
        }
        catch (Exception ex){
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener.onFinishOkCancelDialog(submitData);
        mListener = null;
    }


    public interface OkCancelDialogListener {
        void onFinishOkCancelDialog(boolean submit);
    }

}

如果您对此如何实施有任何疑问,请告诉我。