如何记住根据适配器位置选择切换按钮

时间:2016-10-01 07:26:23

标签: android android-recyclerview sharedpreferences

Iam使用sharedprefrence记住切换按钮选择,每当我重新打开应用程序时,但问题iam面对iam使用回收器视图与cardlayout当我选择切换按钮(tg1)并退出应用程序并重新输入它选择布局中的所有按钮而不是我想要根据适配器位置记住特定按钮。下面是代码。

 public MyViewHolder (View view){
        super (view);
        context= itemView.getContext();
        inflator = (LayoutInflater) context.getApplicationContext().getSystemService(context.LAYOUT_INFLATER_SERVICE);



        im = (ImageView) view.findViewById(R.id.image1);
        b1 = (Button) view.findViewById(R.id.button1a);
        tg1 = (ToggleButton) view.findViewById(R.id.button1b);
        tg2 = (ToggleButton) view.findViewById(R.id.button1c);
        t1= (TextView) view.findViewById(R.id.text1a);
        t2= (TextView) view.findViewById(R.id.text1b);
        t3= (TextView) view.findViewById(R.id.text1c);

        SharedPreferences sharedpreferences= context.getSharedPreferences("pre",context.MODE_PRIVATE);
        boolean check = sharedpreferences.getBoolean("status", true);
        tg1.setChecked(check);






        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent =new Intent(context, Overlay.class);
                click=true;


                int pos= getAdapterPosition();
                switch (pos){
                    case 0:
                        context.startService(intent);
                        container = inflator.inflate(R.layout.preview_layout, null);
                        b11= (Button) container.findViewById(R.id.p1);
                        b11.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                               context.stopService(intent);
                                container=null;

                            }
                        });


                        break;
                    case 1:
                        context.startService(intent);


                        break;
                }


            }
        });
        tg1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                intent = new Intent(context, Overlay.class);
                click=false;
                int pos= getAdapterPosition();

                switch (pos) {
                    case 0:
                    if (b) {

                        img = new ImageView(context);
                        img.setImageResource(R.drawable.heart);
                        context.startService(intent);
                        SharedPreferences sharedpreferences= context.getSharedPreferences("pre",context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedpreferences.edit();
                        editor.putBoolean("status",tg1.isChecked());
                        editor.apply();
                    } else {
                        context.stopService(intent);
                        img=null;
                    }
                        break;
                    case 1:
                        if (b) {
                            img = new ImageView(context);
                            img.setImageResource(R.drawable.heart);
                            context.startService(intent);




                        } else {
                            context.stopService(intent);
                            img=null;
                        }
                        break;
                }
            }
        });

以下是我想要的图像说明.. enter image description here

0 个答案:

没有答案