android - 在弹出窗口中显示 RecyclerView

标签 android popup android-recyclerview android-cardview

我有一个 RecyclerView,当单击 RecyclerView 项目时,想要打开一个包含另一个 RecyclerView 的弹出窗口。快完成了,但是在弹出窗口中,卡片 View 没有出现。我不明白为什么,有人可以帮忙吗?

1- 我的主要 RecyclerView 适配器

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

private ArrayList<Mission> mDataset;
private Context mContext;

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<Mission> myDataset, Context context) {
    mDataset = myDataset;
    this.mContext = context;
}

// Create new views (invoked by the layout manager)
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // create a new view
    View v = LayoutInflater.from(mContext)
            .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.mPuanView.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(mContext,"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 mPuanView;
        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);
            mPuanView = (TextView) itemView.findViewById(R.id.tv_puan);

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


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

                    showPopup();

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

 public void showPopup(){
        final View popupView = LayoutInflater.from(mContext).inflate(R.layout.recycler_popup_window, null);
        final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);

Button btn = (Button) popupView.findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        RecyclerView recyclerView = (RecyclerView) popupView.findViewById(R.id.rv_recycler_view);
        ArrayList<String> data = new ArrayList<>();
        data.add("my data");
        data.add("my test data");
        PopupRecyclerViewAdapter adapter = new PopupRecyclerViewAdapter(mContext,data);
        recyclerView.setAdapter(adapter);

        popupWindow.showAtLocation(popupView,Gravity.CENTER, 0, 0);

    }

}

2- 我的第二个 RecyclerView 适配器,用于弹出窗口

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

    private Context mContext;
    private ArrayList<String> data;

    public PopupRecyclerViewAdapter(Context mContext, ArrayList<String> data) {
        this.mContext = mContext;
        this.data = data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.recycler_popup_card_item, parent,false);
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.mTextView.setText(data.get(position));
    }

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


    //View Holder
    public class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView mTextView;

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

            mTextView = (TextView) itemView.findViewById(R.id.tv_text2);
        }
    }
}

3- Recycler 弹出窗口布局

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


    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_recycler_view2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/button"
        android:background="#ff4545">
    </android.support.v7.widget.RecyclerView>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Close"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

4- CardView Layout for popup RecyclerView

<?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">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="4dp"
        card_view:elevation="14dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="175dp"
            android:id="@+id/imageView2"
            android:src="@mipmap/testimage"
            android:layout_marginBottom="10dp"
            android:scaleType="centerCrop"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tv_text2"
            android:text="Blah blah blah..."
            android:gravity="center"
            android:layout_marginBottom="10dp"/>

        </LinearLayout>


    </android.support.v7.widget.CardView>

</RelativeLayout>

最佳答案

在您的recyclerview 弹出窗口中添加以下行:

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
recyler_view.setLayoutManager(mLayoutManager); 

关于android - 在弹出窗口中显示 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39492447/

相关文章:

javascript - 使弹出模式对话框在出现后可移动/可拖动

java - setOnItemClickListener“RecyclerView”的问题

android - Mono.Linker.MarkException : Error processing method: 'System. 无效 AndroidX.RecyclerView.Widget.RecyclerView/LayoutManager

java - 如何在Fragments中设置RecyclerView OnClickListener?

android - 我们可以将我们的应用程序设置菜单添加到 android 设备设置菜单吗

css - 帮助在 IE 中隐藏 css 弹出窗口

javascript - 传递提示框的输入值

android - 在两个 recyclerview 之间拖放(再次)

android - SharedPreferences在android程序中产生强制关闭

Android:Google MapView 在加载 map 时显示进度指示器