如何使用共享首选项实现类似按钮

时间:2017-06-12 13:26:31

标签: android android-fragments sharedpreferences android-cardview

我正在做一个简单的引号应用,其中包含cardView中的引号。我已将数据>数组中的引号存储起来。我的应用程序中有两个片段,其中包含cardview中的引号,下一个是收藏夹。我在cardView中创建了一个like按钮,这样当按下like按钮时,我希望cardview出现在收藏夹片段中。我可以使用共享偏好来做到这一点吗?如果是,怎么样?

我的cardView的recylerAdapter如下:

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(favButton.getContext(), 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 favButton, boolean isChecked){
                    if (isChecked)
                        favButton.setBackgroundDrawable(ContextCompat.getDrawable(favButton.getContext(),R.mipmap.ic_launcher));

                    else
                        favButton.setBackgroundDrawable(ContextCompat.getDrawable(favButton.getContext(), R.mipmap.ic_cart));
                }
            });
        }

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

这是我的片段,其中包含引号:

public class Category1 extends Fragment {

    private List<CardItemModel> cardItems = new ArrayList<>(20);



    private MainActivity mainActivity;
    private Toolbar toolbar;
    private RecyclerView recyclerView;
    private RecyclerAdapter recyclerAdapter;

    public static ArrayList<CardItemModel> data;


    public Category1() {

    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mainActivity = (MainActivity)activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.cat1_fragment, container, false);

        toolbar = (Toolbar)view.findViewById(R.id.fab_toolbar);

        setupToolbar();



        recyclerView = (RecyclerView)view.findViewById(R.id.fab_recycler_view);

        setupRecyclerView();

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {





        super.onActivityCreated(savedInstanceState);
        mainActivity.setupNavigationDrawer(toolbar);
    }

    private void setupToolbar(){
        toolbar.setTitle(getString(R.string.cat1_fragment_title));
        mainActivity.setSupportActionBar(toolbar);
    }

    private void setupRecyclerView(){
        recyclerView.setLayoutManager(new LinearLayoutManager(mainActivity));
        recyclerView.setHasFixedSize(true);
        initializeCardItemList();
        recyclerAdapter = new RecyclerAdapter(cardItems);
        recyclerView.setAdapter(recyclerAdapter);
    }

    private void initializeCardItemList(){
        CardItemModel cardItemModel;
        String[] cardTitles = getResources().getStringArray(R.array.cat1_cards);
        String[] cardContents = getResources().getStringArray(R.array.cat1_cards_content);
        final int length = cardTitles.length;
        for(int i=0;i<length;i++){
            cardItemModel = new CardItemModel(cardTitles[i],cardContents[i]);
            cardItems.add(cardItemModel);
        }
    }



}

如何设置我最喜欢的片段,以便我可以获得喜欢的项目。 这是我的模特课。

public class CardItemModel {

    public String title;
    public String content;

    public CardItemModel(String title, String content) {
        this.title = title;
        this.content = content;
    }
}

1 个答案:

答案 0 :(得分:1)

  1. 请勿使用static viewHolder。
  2. 不要在BindViewHolder中使用onClickListener。在ViewHolder中使用它。显然,它使用viewHolder中的实例。
  3. 不要为引号(String)和收藏夹(布尔值)使用两个单独的数组。使用模型类进行类似的操作。
  4. 如果您有任何疑问,DM给我。一切顺利。