片段中的cardview溢出菜单

时间:2016-06-16 06:52:31

标签: android android-fragments android-cardview overflow-menu

我在cardviewactivity中实现了cardview溢出菜单,但不知道我如何在片段中使用相同的

我有标签视图和标签片段,现在每个片段都在访问cardview-list view-activity。

这是我想要实现的屏幕截图:

enter image description here

问题:我无法获得每个cardview listitem的溢出菜单。应用已停止。

卡片视图xml

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

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="10dp"
        android:foreground="?android:attr/selectableItemBackground">

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

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_alignParentTop="true"/>

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_below="@+id/textView"/>
   ----->         <ImageButton
                android:id="@+id/img_menu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                 android:background="@drawable/ic_action_navigation_more_vert"
                android:layout_alignParentRight="true"
                android:layout_marginTop="12dp"
               android:onClick="showPopup"/>

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


</LinearLayout>

MY RECYCLER VIEW ADAPTER

public class MyRecyclerViewAdapter extends RecyclerView
        .Adapter<MyRecyclerViewAdapter
        .DataObjectHolder> {
    private static String LOG_TAG = "MyRecyclerViewAdapter";
    private ArrayList<DataObject> mDataset;
    private static MyClickListener myClickListener;
    Context mContext = null;
    public static class DataObjectHolder extends RecyclerView.ViewHolder{
          /*  implements View
            .OnClickListener {*/
        TextView label;
        TextView dateTime;
        ImageButton overflowMenu;
        public View overflow;

        public DataObjectHolder(View itemView) {
            super(itemView);
            label = (TextView) itemView.findViewById(R.id.textView);
            dateTime = (TextView) itemView.findViewById(R.id.textView2);
          overflowMenu = (ImageButton) itemView.findViewById(R.id.img_menu);
            Log.i(LOG_TAG, "Adding Listener");
          // itemView.setOnClickListener(this);
           // overflowMenu.setOnClickListener(this);
        }

      /*  @Override
        public void onClick(View v) {
           *//* myClickListener.onItemClick(getAdapterPosition(), v);
            Toast.makeText(getAdapterPosition(), "STRING MESSAGE", Toast.LENGTH_LONG).show();*//*
        }*/
    }


    public void setOnItemClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public MyRecyclerViewAdapter(ArrayList<DataObject> myDataset) {
        mDataset = myDataset;
    }

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

        DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
        return dataObjectHolder;
    }

    @Override
    public void onBindViewHolder(DataObjectHolder holder, int position) {

        holder.label.setText(mDataset.get(position).getmText1());
        holder.dateTime.setText(mDataset.get(position).getmText2());

        holder.overflowMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PopupMenu popup = new PopupMenu(mContext, view);
                MenuInflater inflater = popup.getMenuInflater();
                inflater.inflate(R.menu.card_overflow_menu, popup.getMenu());
                popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
                popup.show();
            }
        });

    }

    public void addItem(DataObject dataObj, int index) {
        mDataset.add(index, dataObj);
        notifyItemInserted(index);
    }

    public void deleteItem(int index) {
        mDataset.remove(index);
        notifyItemRemoved(index);
    }

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

    public interface MyClickListener {
        public void onItemClick(int position, View v);
    }

    private class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {
        public MyMenuItemClickListener() {
        }

        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.one:
                    Toast.makeText(mContext, "Action 1", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.two:
                    Toast.makeText(mContext, "Action 2", Toast.LENGTH_SHORT).show();
                    return true;
                default:
            }
            return false;
        }

    }
}

我正在调用溢出菜单的片段

 private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private static String LOG_TAG = "CardViewActivity";
    ImageButton overflowMenu;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // return inflater.inflate(R.layout.activity_card_view, null);
        View rootView = inflater.inflate(R.layout.activity_card_view, container, false);
        overflowMenu = (ImageButton) rootView.findViewById(R.id.img_menu);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new MyRecyclerViewAdapter(getDataSet());
        mRecyclerView.setAdapter(mAdapter);


        return rootView;
    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.card_overflow_menu, menu);

    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.one:
                Toast.makeText(getContext(), " Clicked 1 ", Toast.LENGTH_LONG).show();
                return true;

            case R.id.two:
                Toast.makeText(getContext(), "Clicked 2 ", Toast.LENGTH_LONG).show();
                return true;

            default:
                break;
        }

        return false;
    }


 /*   public void showPopup(View v) {
        PopupMenu popup = new PopupMenu(getActivity(), v);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.card_overflow_menu, popup.getMenu());
        popup.show();

        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.one:
                        //Or Some other code you want to put here.. This is just an example.
                        Toast.makeText(getActivity(), " Clicked 1 " + " : ", Toast.LENGTH_LONG).show();
                        break;
                    case R.id.two:

                        Toast.makeText(getActivity(), "Clicked 2 " + " : ", Toast.LENGTH_LONG).show();

                        break;

                    default:
                        break;
                }

                return true;
            }
        });


    }*/

    private ArrayList<DataObject> getDataSet() {
        ArrayList results = new ArrayList<DataObject>();
        for (int index = 0; index < 20; index++) {
            DataObject obj = new DataObject("Test " + index,
                    "Doc number " + index);
            results.add(index, obj);
        }
        return results;
    }
}

我面临的错误:

java.lang.IllegalStateException: Could not find method showPopup(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageButton with id 'img_menu'
                                                                          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:325)
                                                                          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                          at android.view.View.performClick(View.java:4802)
                                                                          at android.view.View$PerformClick.run(View.java:20101)
                                                                          at android.os.Handler.handleCallback(Handler.java:810)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                          at android.os.Looper.loop(Looper.java:189)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5532)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:372)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)

2 个答案:

答案 0 :(得分:1)

如果要在列表中为CardView创建溢出菜单,则应在RecyclerView适配器本身中创建它。

这是代码

 viewHolder.overflow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PopupMenu popup = new PopupMenu(mContext, view);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.menu_action, popup.getMenu());
            popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
            popup.show();
        }
    });

其中overflowCardView内的图片图标,而viewHolderViewHolder类的对象。现在为上下文或溢出菜单创建单击事件。

class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {

    public MyMenuItemClickListener() {
    }

    @Override
    public boolean onMenuItemClick(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.action_first:
                Toast.makeText(mContext, "Action 1", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.action_second:
                Toast.makeText(mContext, "Action 2", Toast.LENGTH_SHORT).show();
                return true;
            default:
        }
        return false;
    }
}

您必须为上下文菜单创建单独的Menu资源文件。

更改此

public MyRecyclerViewAdapter(ArrayList<DataObject> myDataset) {
    mDataset = myDataset;
}

public MyRecyclerViewAdapter(Context context, ArrayList<DataObject> myDataset) {
    mContext = context;
    mDataset = myDataset;
}

答案 1 :(得分:0)

您的问题不明确,可以在此处显示您的代码吗? 我想你可能会以错误的方式进行onClick。 只需按照该链接 java.lang.illegalstateexception could not find a method (view) in the activity class android fragment