ysfcyln ysfcyln - 2 months ago 38
Android Question

Popup window in RecyclerView Adapter

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() cannot be resolve and gives error, if any one can help, I will be appreciate

Answer

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

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(((Activity)mContext).getSupportFragmentManager(), "OpenPopup");
}
Comments