RecyclerView

时间:2017-07-19 12:04:19

标签: java android android-recyclerview android-menu

我正在尝试在每个RecyclerView项目中实现选项菜单 我在MainActivity Layout中有这个RecyclerView。一个CardView项目填充RecyclerView。 utils类进行序列化和保存。 addtracking活动将新内容添加到RecyclerView和我的RecyclerView适配器。现在的问题是每当我在addtracking活动中添加一个新项目。填充了RecyclerView但没有选项菜单图标 itemadapter类:

public class Itemadapter extends 
RecyclerView.Adapter<Itemadapter.itemviewholder> {
ArrayList<Item> items = new ArrayList<Item>();
 Context mcontext;
public Itemadapter(MainActivity mainActivity, int item, ArrayList<Item> 
 items)
 {
 this.items = items;
 }


 @Override
public itemviewholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);       
itemviewholder itemviewholder = new itemviewholder(view);
return itemviewholder;





}

@Override
public void onBindViewHolder(final itemviewholder holder, final int   position) {
 Item item = items.get(position);
holder.postnumber.setText(item.getPost_number());
holder.itemname.setText(item.getItem_name());

 holder.txtoptiondigit.setOnClickListener(new View.OnClickListener() {
   @Override
  public void onClick(View view) {
       //display menu
       final PopupMenu pop = new PopupMenu(mcontext,holder.txtoptiondigit);
      pop.inflate(R.menu.recycler_item_menu);
     pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
           @Override
           public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()){
                case R.id.item_delete:
                     //delete file
                  items.remove(position);
                     notifyDataSetChanged();
                     Toast.makeText(mcontext,"item deleted",Toast.LENGTH_SHORT).show();

                    break;
                 case R.id.item_share:
                     //share info
             break;
            }

               return false;
           }
       });pop.show();
   }
 });



  }

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

}
  public static class itemviewholder extends RecyclerView.ViewHolder
  {
      TextView itemname,postnumber,txtoptiondigit;
 public itemviewholder(View view)
{
 super(view);

 itemname =  view.findViewById(R.id.itemdetailsdisplay);
 postnumber =  view.findViewById(R.id.itemnuberdisplay);
txtoptiondigit = view.findViewById(R.id.optiondigit);
 }


 }

item.xml

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

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding= "@dimen/cardview_default_elevation">

<TextView
    android:id="@+id/itemdetailsdisplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:text="example_name"
    android:textAppearance="?android:attr/textAppearanceLarge" />

   <TextView
       android:id="@+id/optiondigit"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="&#8942;"
      android:textAppearance="?android:textAppearanceLarge"
       android:layout_above="@+id/itemnuberdisplay"
       android:layout_alignParentRight="true"
       android:layout_alignParentEnd="true"
       android:layout_alignParentTop="true"
       >






   <TextView
       android:id="@+id/itemnuberdisplay"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:layout_alignParentLeft="true"
       android:layout_alignParentStart="true"
       android:text="example_number"
       android:textAppearance="?android:attr/textAppearanceSmall" />

 </RelativeLayout>

0 个答案:

没有答案