Android Material 设计导航抽屉 setClickListener

Android Material Design navigation drawer setClickListener

我正在尝试将我的旧应用程序转换为新应用程序 android Material design.but 在我的 onclick 事件中我无法使用我的 getSupportFragmentManager() method.i 不知道我在这里做错了什么..

public class DrawerListAdapter extends RecyclerView.Adapter<DrawerListAdapter.DrawerViewHolder> {

    private Context context;
    private Activity activity;
    private LayoutInflater inflater;
    List<info> data = Collections.emptyList();

    public DrawerListAdapter(Context context, List<info> data) {
        inflater = LayoutInflater.from(context);
        this.data = data;
        this.context = context;
    }

    @Override
    public DrawerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.custome_row, parent, false);
        DrawerViewHolder holder = new DrawerViewHolder(view);
        return holder;
    }

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

        info current = data.get(position);
        holder.title.setText(current.title);
        holder.icon.setImageResource(current.iconId);

        holder.setClickListener(new DrawerViewHolder.ClickListener() {
            @Override
            public void onClick(View v, int position, boolean isLongClick) {

                switch (position) {
                    case 1:
                        getSupportFragmentManager()
                                .beginTransaction()
                                .replace(R.id.content_frame, Fragment_home.newInstance(),
                                        Fragment_home.TAG).commit();

                        break;
                    case 2:
                        break;

                }

            }
        });


    }

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

    static class DrawerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView title;
        ImageView icon;

        private ClickListener clickListener;

        public DrawerViewHolder(View itemView) {
            super(itemView);

            title = (TextView) itemView.findViewById(R.id.text_title);
            icon = (ImageView) itemView.findViewById(R.id.imageView);

            itemView.setOnClickListener(this);
        }

        /* Interface for handling clicks - both normal and long ones. */
        public interface ClickListener {

            /**
             * Called when the view is clicked.
             *
             * @param v           view that is clicked
             * @param position    of the clicked item
             * @param isLongClick true if long click, false otherwise
             */
            public void onClick(View v, int position, boolean isLongClick);

        }

        /* Setter for listener. */
        public void setClickListener(ClickListener clickListener) {
            this.clickListener = clickListener;
        }


        @Override
        public void onClick(View view) {
            clickListener.onClick(view, getPosition(), false);

        }
    }
}

我建议使用 Android 设计支持库中的 NavigationView class 设置抽屉,它还允许 xml 资源中的菜单 inflation。然后,您将通过注册 NavigationView.NavigationItemSelectedListener

来处理点击

在您的 ViewHolder 中实现 OnClickListener 非常简单,然后在 onClick 方法中使用 getAdapterPosition() 方法。请检查下面给出的代码。它会对你有所帮助

 public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setClickable(true);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            Log.e(TAG, "POSITION " + getAdapterPosition());
            switch (getAdapterPosition()) {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
            }
        }
    }