使用ToggleButton创建收藏按钮

3

我正在尝试使用ToggleButton添加一个收藏按钮,当未选中时显示某个图标,选中时显示另一个图标。我尝试了这个方法但它没有起作用。这是ToggleButton:

<ToggleButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/fav_fragment_title"
            android:id="@+id/favButton"
            android:layout_weight="0.33"
            android:layout_gravity="center"
            />

这是我的回收器适配器。

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    private ClipboardManager myClipboard;
    private ClipData myClip;
    private Context context;




    public List<CardItemModel> cardItems;

    public RecyclerAdapter(List<CardItemModel> cardItems){
        this.cardItems = cardItems;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView copyButton;
        ImageView shareButton;
        ToggleButton favButton;


        TextView title;
        TextView content;
        public ViewHolder(View itemView) {
            super(itemView);
            this.title = (TextView)itemView.findViewById(R.id.card_title);
            this.content = (TextView)itemView.findViewById(R.id.card_content);
            this.copyButton= (ImageView) itemView.findViewById(R.id.copyButton);
            this.shareButton=(ImageView) itemView.findViewById(R.id.shareButton);
            this.favButton=(ToggleButton) itemView.findViewById(R.id.favButton);
            favButton.setChecked(false);
            favButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.ic_launcher));


        }
    }



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

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.title.setText(cardItems.get(position).title);
        holder.content.setText(cardItems.get(position).content);
        holder.copyButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){


                myClipboard = (ClipboardManager) v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);


                myClip = ClipData.newPlainText("label", holder.content.getText().toString());
                myClipboard.setPrimaryClip(myClip);
                Toast.makeText(v.getContext(), "Copied to clipboard" , Toast.LENGTH_SHORT ).show();

            }
        });
        holder.shareButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("text/plain");
                share.putExtra(Intent.EXTRA_TEXT, holder.content.getText().toString());
                v.getContext().startActivity(Intent.createChooser(share, "Share Text"));
            }
        });

        holder.favButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
                if (isChecked)
                    favButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(),R.mipmap.ic_launcher));
                else
                    favButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.ic_cart));
            }
        });
    }

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

另外,我如何将已收藏的recyclerView添加到另一个片段中?

请告诉我们问题具体是什么。你所说的“不起作用”是什么意思? - Bertram Gilfoyle
1个回答

4

添加一个可绘制选择器并将其用作ToggleButton的背景。

<ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/fav_fragment_title"
        android:id="@+id/favButton"
        android:layout_weight="0.33"
        android:layout_gravity="center"
        android:textOn=""
        android:textOff=""
        android:background="@drawable/toggle_image"
        />

res/drawable/toggle_image.xml文件中:

添加你的选中/未选中图片。

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected -->
    <item android:drawable="@drawable/selected_image"
          android:state_checked="true" />
    <!-- When not selected-->
    <item android:drawable="@drawable/unselected_image"
        android:state_checked="false"/>

 </selector>

1
谢谢,这很有帮助。现在,你能否给一些建议,我该如何将“收藏”的recyclerView添加到另一个fragment中? - Dinesh Neupane
将RecyclerView中喜欢的项目数据作为Bundle传递到其他活动中。请参见:https://dev59.com/zI3da4cB1Zd3GeqPvyiv - rafsanahmad007

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接