RecyclerView适配器中的弹出窗口

时间:2016-09-13 15:44:47

标签: android popup android-recyclerview adapter

我是android的新手,我在点击RecyclerView项目时尝试显示弹出窗口,但有点困惑,我看了this question但是无法弄清楚这个上下文问题

$.ajax({
    type: "GET",
    url: "@Url.Action("GetPrice", "YourControllerNameHere")",
    data: { Id: id, Exterior: exterior }

}).done(function (data) {       
    if (data.success) {
        var val = data.lowest_price;
        $(e).html(val);
    }
});

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

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);
}}

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

1 个答案:

答案 0 :(得分:1)

问题是您正在尝试在适配器中调用 getActivity()方法。适配器将不参考活动或上下文。一个活动将引用上下文,片段将引用父Activity和上下文( getActivity()和getContext())

因此在代码中进行这些更改。 首先从Fragment或activity将上下文传递给Adapter的构造函数。然后使用该上下文来夸大您的布局。

private ArrayList<Mission> mDataset;

private Context mContext;

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

    this.mContext = context;
}

接下来,使用此上下文实例来扩充您的布局。

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

    // Blah Blah remaining stuff...
}

此外,您可以使用相同的上下文来扩充onCreateViewHolder中的布局。你所做的也是正确的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);

}

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

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;
    }
}

下一步启动此对话框片段。试一试。您还可以自定义对话框片段以满足您的需求。

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

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