卡布局操作按钮

时间:2017-03-24 20:36:29

标签: android android-layout android-fragments material-design cardlayout

我创建了一张带有四张牌的卡片布局。

我的卡片样本可以在图片中找到。enter image description here 动态创建它们的代码如下:

public class four_future extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters


    public four_future() {
        // Required empty public constructor
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        RecyclerView recyclerView = (RecyclerView) inflater.inflate(
                R.layout.recycler_view, container, false);
        ContentAdapter adapter = new ContentAdapter(recyclerView.getContext());
        recyclerView.setAdapter(adapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        return recyclerView;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView picture;
        public TextView name;
        public TextView description;
        public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.four_future, parent, false));
            picture = (ImageView) itemView.findViewById(R.id.card_image);
            name = (TextView) itemView.findViewById(R.id.card_title);
            description = (TextView) itemView.findViewById(R.id.card_text);
            // Adding Snackbar to Action Button inside card
            Button button = (Button)itemView.findViewById(R.id.action_button);
            button.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Snackbar.make(v, "Action is pressed",
                            Snackbar.LENGTH_LONG).show();
                }
            });


        }
    }
    /**
     * Adapter to display recycler view.
     */
    public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
        // Set numbers of List in RecyclerView.
        private static final int LENGTH = 3
                ;

        private final String[] mPrograms;

        private final Drawable[] mProgramPictures;

        public ContentAdapter(Context context) {
            Resources resources = context.getResources();
            mPrograms = resources.getStringArray(R.array.programs);

            TypedArray a = resources.obtainTypedArray(R.array.program_picture);
            mProgramPictures = new Drawable[a.length()];
            for (int i = 0; i < mProgramPictures.length; i++) {
                mProgramPictures[i] = a.getDrawable(i);
            }
            a.recycle();
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.picture.setImageDrawable(mProgramPictures[position % mProgramPictures.length]);
            holder.name.setText(mPrograms[position % mPrograms.length]);

        }

        @Override
        public int getItemCount() {
            return LENGTH;
        }
    }
}

我在每张卡片上都有操作按钮。如何在单击不同卡上的操作按钮时打开不同的活动。截至目前,每个动作按钮都会给出一个小吃店。如果我单击第一张卡上的操作按钮,则必须打开first_activity.xml。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

onBindViewHolder实施商店中,有关在Button中打开哪个活动的信息(您可以做一些简单的操作,只需使用正确的活动代码设置按钮标记,或者存储一个意图)。然后在你执行的ViewHolder中

Button button = (Button)itemView.findViewById(R.id.action_button);

您可以检索标记并使用它来打开正确的活动。

编辑:试试这个:

更改您的ViewHolder:

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView picture;
        public TextView name;
        public TextView description;
        public Button button;
        public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.four_future, parent, false));
            picture = (ImageView) itemView.findViewById(R.id.card_image);
            name = (TextView) itemView.findViewById(R.id.card_title);
            description = (TextView) itemView.findViewById(R.id.card_text);
            // Adding Snackbar to Action Button inside card
            button = (Button)itemView.findViewById(R.id.action_button);
            button.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    if("activity1".equals(button.getTag()){
                        startActivity1();
                    }else if("activity2".equals(button.getTag()){
                        startActivity2();
                    }else{
                    Snackbar.make(v, "Action is pressed",
                            Snackbar.LENGTH_LONG).show();
                    }
                }
            });
        }
    }

然后修改onBindViewHolder调用以设置标记:

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.picture.setImageDrawable(mProgramPictures[position % mProgramPictures.length]);
        holder.name.setText(mPrograms[position % mPrograms.length]);
        if(position == 0) holder.button.setTag("activity1");
        else if(position == 1) holder.button.setTag("activity2");
    }