1

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

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

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

在此处输入图像描述

问题:我无法获得每个 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>

我的回收站视图适配器

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)
4

2 回答 2

2

如果要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();
        }
    });

overflow里面的图像图标在哪里CardViewviewHolder是类的对象ViewHolder。现在为上下文或溢出菜单创建点击事件。

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;
}
于 2016-06-16T07:52:25.960 回答
0

你的问题不清楚,你能在这里显示你的代码吗?我认为您可能会以错误的方式执行 onClick。只需点击该链接 java.lang.illegalstateexception 在活动类 android 片段中找不到方法(视图)

于 2016-06-16T06:59:13.067 回答