Recyclerview上的选项菜单不显示

时间:2018-01-23 08:09:32

标签: android android-recyclerview android-optionsmenu

我在recyclerview和cardview中创建选项菜单。菜单工作正常。但为什么我在recyclerview中创建的选项菜单没有显示?

on the preview in android studio design he displays but, when I run it, the options menu in the recyclerview does not display

2 个答案:

答案 0 :(得分:1)

这里我附上了我的代码段

FavoritRecyclerViewAdapter.java

package com.mimdudin.carekkerje.Adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.mimdudin.carekkerje.Activities.FavoriteFragmentDetail;
import com.mimdudin.carekkerje.Helper.AppController;
import com.mimdudin.carekkerje.Model.Favorit;
import com.mimdudin.carekkerje.R;
import com.shashank.sony.fancydialoglib.FancyAlertDialog;

import java.util.List;


public class FavoritRecyclerViewAdapter extends RecyclerView.Adapter<FavoritRecyclerViewAdapter.ViewHolder> {

    private Context context;
    private List<Favorit> favoritList;

    public FavoritRecyclerViewAdapter(Context context, List<Favorit> favoritList) {
        this.context = context;
        this.favoritList = favoritList;
    }

    @Override
    public FavoritRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.favorit_row, parent, false);
        return new ViewHolder(view, context);
    }


    @Override
    public void onBindViewHolder(final FavoritRecyclerViewAdapter.ViewHolder holder, int position) {
        Favorit favorit = favoritList.get(position);

        holder.titleIDfav.setText(favorit.getGajiFav());
        holder.lokasiIDfav.setText(favorit.getLokasiFav());
        holder.gajiIDfav.setText(favorit.getGajiFav());

        String img_logoFavUrl = favorit.getImg_logoFav();
        Glide.with(context)
                .load(img_logoFavUrl)
                .apply(new RequestOptions()
//                                .placeholder(android.R.drawable.ic_dialog_info)
//                                .error(android.R.drawable.ic_dialog_alert).centerCrop().dontAnimate()
//                         .centerCrop(), .crossFade(), .thumbnail(), .dontAnimate(), .dontTransform() BitmapTransformation(.circleCrop())
                )
                .into(holder.img_logoFav);

        holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //creating a popup menu
                PopupMenu popup = new PopupMenu(context, holder.buttonViewOption);
                //inflating menu from xml resource
                popup.inflate(R.menu.fav_options_menu);
                //adding click listener
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.menu1:
                                //handle menu1 click
                                break;
                            case R.id.menu2:
                                //handle menu2 click
                                break;

//                            case R.id.menu3:
//                                //handle menu3 click
//                                break;
                        }
                        return false;
                    }
                });
                //displaying the popup
                popup.show();
            }
        });

    }

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

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private ImageView img_logoFav;
        private TextView titleIDfav;
        private TextView lokasiIDfav;
        private TextView gajiIDfav;
        private TextView buttonViewOption;

        private FancyAlertDialog dialog;
        private FancyAlertDialog.Builder fancyAlertDialogBuilder;

        ViewHolder(View itemView, final Context ctx) {
            super(itemView);
            context = ctx;

            img_logoFav = itemView.findViewById(R.id.img_logoFav);
            titleIDfav = itemView.findViewById(R.id.titleIDfav);
            lokasiIDfav = itemView.findViewById(R.id.lokasiIDFav);
            gajiIDfav = itemView.findViewById(R.id.gajiIDFav);
            buttonViewOption = itemView.findViewById(R.id.textViewOptions);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            FavoriteFragmentDetail favoriteFragmentDetail = new FavoriteFragmentDetail();
            Bundle bundle = new Bundle();
            Favorit favorit = favoritList.get(getAdapterPosition());
//            bundle.putParcelable("favorit", favorit);
            favoriteFragmentDetail.setArguments(bundle);

            AppCompatActivity activity = (AppCompatActivity) itemView.getContext();

            android.support.v4.app.FragmentManager fragmentManager = activity.getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction().addToBackStack(null);
//            fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            fragmentTransaction.replace(R.id.frame_fragmentFavorit, favoriteFragmentDetail);
            fragmentTransaction.commit();

        }


    }
}

favorit_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app2="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_blue_bright">

    <android.support.v7.widget.CardView
        android:id="@+id/favorit_cardView"
        android:layout_width="196dp"
        android:layout_height="188dp"
        android:layout_margin="1dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <ImageView
                android:id="@+id/img_logoFav"

                android:layout_width="match_parent"
                android:layout_height="100dp" />
            <!--android:background="@color/black_55"-->
            <TextView
                android:id="@+id/titleIDfav"
                android:layout_width="wrap_content"
                android:layout_height="63dp"
                android:layout_alignParentLeft="true"
                android:padding="10dp"
                android:paddingTop="10dp"
                android:textSize="15sp"
                android:textStyle="bold"
                android:textColor="@color/colorPrimary"
                android:layout_below="@id/img_logoFav"
                android:maxLines="2"
                android:ellipsize="end"
                android:text="Salesman Executive" />

            <ImageView
                android:id="@+id/logo_mapFav"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@id/titleIDfav"
                android:layout_alignParentLeft="true"
                android:paddingBottom="1.7dp"
                android:paddingLeft="10dp"
                app2:srcCompat="@drawable/ic_pin_drop_black_24dp" />

            <TextView
                android:id="@+id/lokasiIDFav"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/logo_mapFav"
                android:layout_alignBottom="@id/logo_mapFav"
                android:paddingLeft="3dp"
                android:text="Siantan Hulu"
                android:textSize="12sp" />


            <ImageView
                android:id="@+id/img_gajiFav"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:paddingLeft="10dp"
                android:paddingRight="2dp"
                android:paddingBottom="10dp"
                android:layout_alignParentLeft="true"
                app2:srcCompat="@drawable/ic_attach_money_black_24dp"
                />

            <TextView
                android:id="@+id/gajiIDFav"
                android:layout_toRightOf="@id/img_gajiFav"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@id/img_gajiFav"
                android:paddingBottom="8dp"
                android:paddingRight="10dp"
                android:maxLines="1"
                android:ellipsize="end"
                android:text="Gaji: Rp. 5.000.000 - Rp. 10.000.000"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/textViewOptions"
                android:layout_height="wrap_content"
                android:layout_width="40dp"
                android:gravity="center"
                android:textColor="@color/white"
                android:background="@color/black"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:text="&#8942;"
                android:textAppearance="?android:textAppearanceLarge" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

答案 1 :(得分:0)

尝试使用POPUP_MENU!

在我的知识选项菜单中应该首选菜单栏。并且对于长按使用上下文菜单和适配器项目,您应该去弹出菜单

试试这个Android文档here