2016-09-14 102 views
3

我有一个RecyclerView,当RecyclerView项目点击时,想要打开一个包含另一个RecyclerView的弹出窗口。即将完成,但在弹出窗口中,cardviews不会显示。我不明白为什么,有人可以帮忙吗?在弹出窗口中显示RecyclerView

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- 布局回收站弹出窗口

<?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布局弹出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> 
+0

把土司showpopup来检查它是否得到调用或不 – kgandroid

+0

我用烤面包也检查我设置红色背景颜色检查回收站弹出窗口,弹出显示,但cardviews应该在那里,不出现 – ysfcyln

+0

代码似乎没问题...打印data.size()....还查logcat – kgandroid

回答

2

添加下面的线在你recyclerview弹出:

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