RecyclerView适配器中的弹出窗口 [英] Popup window in RecyclerView Adapter

查看:80
本文介绍了RecyclerView适配器中的弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的新手,当单击RecyclerView项时,我尝试显示弹出窗口,但有些困惑,我看着

I am newbie at android, I am trying show popup window when RecyclerView item clicked but a little bit confused, I looked this question but cant figure out this Context issue

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

private ArrayList<Mission> mDataset;

public MyAdapter(ArrayList<Mission> myDataset) {
    mDataset = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.mission_card_item, parent, false);
    // set the view's size, margins, paddings and layout parameters
    MyViewHolder vh = new MyViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.mTextView.setText(mDataset.get(position).getName());
    holder.mPointView.setText(mDataset.get(position).getPoint());
    holder.mRankView.setText(mDataset.get(position).getRank());

    holder.btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Toast.makeText(v.getContext(),"Buton Clicked", Toast.LENGTH_SHORT).show();
        }
    });
}

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


// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public  class MyViewHolder extends RecyclerView.ViewHolder {

    public CardView mCardView;
    public TextView mTextView;
    public TextView mPointView;
    public TextView mRankView;
    public Button btnAdd;

    public MyViewHolder(final View itemView) {
        super(itemView);

        mCardView = (CardView) itemView.findViewById(R.id.card_view);
        mTextView = (TextView) itemView.findViewById(R.id.tv_text);
        mRankView = (TextView) itemView.findViewById(R.id.tv_rank);
        mPointView = (TextView) itemView.findViewById(R.id.tv_point);

        btnAdd = (Button) itemView.findViewById(R.id.button_add);


        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showPopup(itemView);

                Toast.makeText(itemView.getContext(),"Element " + getAdapterPosition() + " clicked", Toast.LENGTH_SHORT).show();
                Log.d("hello", "Element " + getAdapterPosition() + " clicked.");
            }
        });
    }
}


public void showPopup(View view) {
    View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_layout, null);
    final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    Button btnDismiss = (Button) popupView.findViewById(R.id.ib_close);
    btnDismiss.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popupWindow.dismiss();
        }
    });

    popupWindow.showAsDropDown(popupView, 0, 0);
}}

这是我简单的弹出窗口布局

This is my simple popup window layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_custom_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="2dp"
    android:background="#ab2fc4"
    >
    <ImageButton
        android:id="@+id/ib_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:background="@null"
        />
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a sample popup window."
        android:layout_centerInParent="true"
        android:padding="25sp"
        />
</RelativeLayout>

getActivity()无法解析并给出错误,如果有人可以提供帮助,我将不胜感激

getActivity() cannot be resolve and gives error, if any one can help, I will be appreciate

推荐答案

问题是您正试图在适配器内调用方法 getActivity().适配器将没有对活动或上下文的引用.一个活动将引用上下文,而片段将引用父活动和上下文( getActivity()和getContext()).

The problem is you are trying to invoke the method getActivity() inside an adapter. The adapter will have no reference to the activity or context. An activity will have the reference to the context and fragment will have a reference to the parent Activity and context (getActivity() and getContext()).

因此可以在代码中进行这些更改. 首先将片段或活动中的上下文传递给适配器的构造函数.然后使用该上下文来增加您的布局.

Hence make these changes in the code. First pass the context to the constructor of your Adapter from your Fragment or activity. Then use that context to inflate your layout.

private ArrayList<Mission> mDataset;

private Context mContext;

public MyAdapter(ArrayList<Mission> myDataset, Context context) {
    mDataset = myDataset;

    this.mContext = context;
}

接下来,使用您的上下文实例来增加您的布局.

Next use this instance of your context to inflate your layout.

public void showPopup(View view) {
    View popupView = LayoutInflater.from(mContext).inflate(R.layout.popup_layout, null);

    // Blah Blah remaining stuff...
}

此外,您可以使用相同的上下文在onCreateViewHolder中扩大布局.您所做的也是正确的parent.getContext().

In addition, you could use the same context to inflate the layout in onCreateViewHolder. What you have done is also correct parent.getContext().

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(mContext)
            .inflate(R.layout.mission_card_item, parent, false);

    return new MyViewHolder(v);

}

如果您只需要一个弹出窗口.创建一个对话框片段.

If you just need a popup window. Create a dialog fragment.

public class PopupDialogFragment extends DialogFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.popup_layout, container,
                false);

        return rootView;
    }
}

下一步启动此对话框片段.尝试一次.您还可以自定义对话框片段以适合您的需求.

Next Launch this dialog fragment. Try this once. You can also customize the dialogfragment to suit your needs.

public void showPopup() {
   PopupDialogFragment dialogFragment = new PopupDialogFragment();

   dialogFragment.show(((FragmentActivity)mContext).getSupportFragmentManager(), "OpenPopup");
}

这篇关于RecyclerView适配器中的弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆