【问题标题】:create favourite button using toogleButton使用切换按钮创建收藏按钮
【发布时间】:2017-10-13 03:33:45
【问题描述】:

我正在尝试使用 toogleButton 添加一个收藏按钮,该按钮在未选中时显示某个图标,在选中时显示另一个图标。我试过这个但没有用。 这是切换按钮

<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 添加到另一个片段?

【问题讨论】:

  • 请告诉我们到底是什么问题。您所说的“它不起作用”是什么意思?

标签: android onclicklistener buttonclick android-togglebutton oncheckedchanged


【解决方案1】:

添加一个 Drawable 选择器并将其用作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>

【讨论】:

  • 感谢它的帮助。现在你能告诉我如何将最喜欢的 recyclerView 添加到另一个片段吗?
  • 看到这个:stackoverflow.com/questions/31496177/… 将您最喜欢的项目数据作为捆绑包传递给其他活动
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-27
  • 1970-01-01
  • 2016-04-04
  • 2016-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多