【问题标题】:Popup window in RecyclerView AdapterRecyclerView 适配器中的弹出窗口
【发布时间】:2016-09-13 15:44:46
【问题描述】:

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

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

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

<?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() 无法解决并给出错误,如果有人可以提供帮助,我将不胜感激

【问题讨论】:

  • 我在回答您的问题时添加了一个答案,如果有帮助请告诉我...
  • 我添加了额外的内容。您可以检查对话框片段的代码。

标签: android popup android-recyclerview adapter


【解决方案1】:

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

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

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

Next 启动此对话框片段。试试这个。您还可以自定义对话框片段以满足您的需求。

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

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

【讨论】:

  • 我也在我的主要片段中进行了更改,我添加了这样的上下文 MyAdapter adapter = new MyAdapter(myData, getContext());但是当我运行应用程序并单击一些 RecyclerView 项目时它会崩溃
  • 另一件事。我不确定您为什么要尝试将另一个布局膨胀为弹出窗口...请参阅 Android 中的 DialogFragment。也许 DialogFragment 就是你要找的东西androidbegin.com/tutorial/android-dialogfragment-tutorial
  • java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton 不能在 com.example.yusuf.firebasedemoapp.MyAdapter.showPopup(MyAdapter.java:104 ) 在 com.example.yusuf.firebasedemoapp.MyAdapter$MyViewHolder$1.onClick(MyAdapter.java:91) 这行是 Button btnDismiss = (Button) popupView.findViewById(R.id.ib_close);和 showPopup();
  • 等等,我将添加 DialogFragment 的代码。你不需要像这样膨胀另一个布局。
  • 天哪,错误与逻辑无关,我犯了一个简单的错误,我在弹出布局中使用了 ImageButton,但定义为按钮,我更改了它并且它起作用了,非常感谢
猜你喜欢
  • 2021-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-24
  • 2017-12-05
  • 2016-06-05
  • 1970-01-01
相关资源
最近更新 更多