使用切换按钮切换DarkTheme /过渡

时间:2019-11-04 09:37:30

标签: c# listview

我之所以来找你,是因为我的切换按钮和Listview中的转换都存在问题...

当我单击切换按钮时,我的主题变化很好,但是没有动画淡入/淡出,您知道为什么吗?因为我把意图放在动画上...我听不懂...谢谢

这是我的代码

问题出在这里:

                RelativeLayout relativeLayoutSettings = convertView.findViewById(R.id.relativeLayoutSettings);
                relativeLayoutSettings.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick (View v) {
                        if(!itemSwitchIconRightView.isChecked()) {
                            itemSwitchIconRightView.setChecked(true);
                            sharedPref.setDarkModeState(true);
                            Intent intent = new Intent(SettingsActivity.this, SettingsActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                            Animatoo.animateFade(SettingsActivity.this);
                            finish();
                        } else {
                            itemSwitchIconRightView.setChecked(false);
                            sharedPref.setDarkModeState(false);
                            Intent intent = new Intent(SettingsActivity.this, SettingsActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                            Animatoo.animateFade(SettingsActivity.this);
                            finish();
                        }
                    }

                });

public class SettingsActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private ListView listView;
    private SharedPref sharedPref;


    @Override
    protected void onCreate (Bundle savedInstanceState) {

        sharedPref = new SharedPref(this);
        if(sharedPref.loadDarkModeState()) {
            setTheme(R.style.AppThemeDark);
        } else {
            setTheme(R.style.AppTheme);
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);


        // Action Bar
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);


        List<Item> itemList = new ArrayList<>();
        itemList.add(new SectionItem("PERSONNALISER"));
        itemList.add(new SwitchItem(R.drawable.item_nightmode_icon, "", "Mode sombre"));
        itemList.add(new ColorItem(R.drawable.item_themecolor_icon, "Thème de l'application", "Mode color"));
        itemList.add(new SectionItem("COMPTE"));
        itemList.add(new EntryItem(R.drawable.item_profil_icon, "Profil"));
        itemList.add(new EntryItem(R.drawable.item_logout_icon, "Se déconnecter"));

        ListView listView = findViewById(R.id.listSettings);
        listView.setAdapter(new ItemAdapter(this, itemList));
        listView.setDivider(null);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
                if(position == 2) {

                }

                if(position == 4) {
                    Toast.makeText(getApplicationContext(), "Profil", Toast.LENGTH_SHORT).show();
                }

                if(position == 5) {
                    Toast.makeText(getApplicationContext(), "Se déconnecter", Toast.LENGTH_SHORT).show();
                }
            }
        });


    }


    public interface Item {
        public boolean isColor();
        public boolean isSwitch();
        public boolean isSection();
        public String getTitle();
    }

    public class SectionItem implements Item {
        private String title;

        public SectionItem (String title) {
            this.title = title;
        }

        public String getTitle () {
            return title;
        }

        @Override
        public boolean isColor() {
            return false;
        }

        @Override
        public boolean isSwitch() {
            return false;
        }

        @Override
        public boolean isSection() {
            return true;
        }
    }

    public class SwitchItem implements Item {
        private int switchIconLeft;
        private String title;
        private String switchIconRight;

        public SwitchItem (int switchIconLeft, String title, String switchIconRight) {
            this.switchIconLeft = switchIconLeft;
            this.title = title;
            this.switchIconRight = switchIconRight;
        }

        public int getSwitchIconLeft () {
            return switchIconLeft;
        }

        public String getTitle () {
            return title;
        }

        public String getSwitchIconRight () {
            return switchIconRight;
        }

        @Override
        public boolean isColor() {
            return false;
        }

        @Override
        public boolean isSwitch() {
            return true;
        }

        @Override
        public boolean isSection() {
            return false;
        }
    }


    public class EntryItem implements Item {
        private int iconleft;
        private String title;

        public EntryItem (int iconleft, String title) {
            this.iconleft = iconleft;
            this.title = title;
        }

        public int getIconleft () {
            return iconleft;
        }

        public String getTitle () {
            return title;
        }

        @Override
        public boolean isColor() {
            return false;
        }

        @Override
        public boolean isSwitch() {
            return false;
        }

        @Override
        public boolean isSection() {
            return false;
        }


    }


    public class ColorItem implements Item {
        private int colorIconLeft;
        private String title;
        private String colorIconRight;

        public ColorItem (int colorIconLeft, String title, String colorIconRight) {
            this.colorIconLeft = colorIconLeft;
            this.title = title;
            this.colorIconRight = colorIconRight;
        }

        public int getColorIconLeft () {
            return colorIconLeft;
        }

        public String getTitle () {
            return title;
        }

        public String getColorIconRight () {
            return colorIconRight;
        }

        @Override
        public boolean isColor() {
            return true;
        }

        @Override
        public boolean isSwitch() {
            return false;
        }

        @Override
        public boolean isSection() {
            return false;
        }


    }

    public class ItemAdapter extends BaseAdapter {

        private Context context;
        private List<Item> itemList;
        private LayoutInflater inflater;

        public ItemAdapter(Context context, List<Item> itemList) {
            this.context = context;
            this.itemList = itemList;
            this.inflater = LayoutInflater.from(context);
        }


        @Override
        public int getCount () {
            return itemList.size();
        }

        @Override
        public Object getItem (int position) {
            return itemList.get(position);
        }

        @Override
        public long getItemId (int i) {
            return 0;
        }

        @Override
        public View getView (int i, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if(itemList.get(i).isSection()) {
                // if header
                convertView = inflater.inflate(R.layout.header_item, null);
                TextView itemHeaderTitle = convertView.findViewById(R.id.headerSectionTitle);
                itemHeaderTitle.setText(((SectionItem) itemList.get(i)).getTitle());
                convertView.setEnabled(false);

            } else if(itemList.get(i).isSwitch()) {
                convertView = inflater.inflate(R.layout.switch_item, null);

                // Switch Icon left
                ImageView itemSwitchIconLeftView = convertView.findViewById(R.id.switchIconLeftSettings);
                itemSwitchIconLeftView.setImageResource(((SwitchItem) itemList.get(i)).getSwitchIconLeft());

                TextView itemSwitchNameView = convertView.findViewById(R.id.switchNameSettings);
                itemSwitchNameView.setText(((SwitchItem) itemList.get(i)).getSwitchIconRight());

                Switch itemSwitchIconRightView = convertView.findViewById(R.id.switchIconRightSettings);
                itemSwitchIconRightView.setText(((SwitchItem) itemList.get(i)).getTitle());

                itemSwitchIconRightView.setClickable(false);

                if(sharedPref.loadDarkModeState()) {
                    itemSwitchIconRightView.setChecked(true);
                }

                RelativeLayout relativeLayoutSettings = convertView.findViewById(R.id.relativeLayoutSettings);
                relativeLayoutSettings.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick (View v) {
                        if(!itemSwitchIconRightView.isChecked()) {
                            itemSwitchIconRightView.setChecked(true);
                            sharedPref.setDarkModeState(true);
                            Intent intent = new Intent(SettingsActivity.this, SettingsActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                            Animatoo.animateFade(SettingsActivity.this);
                            finish();
                        } else {
                            itemSwitchIconRightView.setChecked(false);
                            sharedPref.setDarkModeState(false);
                            Intent intent = new Intent(SettingsActivity.this, SettingsActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                            Animatoo.animateFade(SettingsActivity.this);
                            finish();
                        }
                    }

                });



            } else if(itemList.get(i).isColor()) {
                convertView = inflater.inflate(R.layout.choose_color_item, null);


                // Switch Icon left
                ImageView itemColorIconLeftView = convertView.findViewById(R.id.colorIconLeftSettings);
                itemColorIconLeftView.setImageResource(((ColorItem) itemList.get(i)).getColorIconLeft());

                TextView itemColorNameView = convertView.findViewById(R.id.colorNameSettings);
                itemColorNameView.setText(((ColorItem) itemList.get(i)).getTitle());

                Button itemButtonIconRightView = convertView.findViewById(R.id.colorIconRightSettings);
                itemButtonIconRightView.setText(((ColorItem) itemList.get(i)).getColorIconRight());

                itemButtonIconRightView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick (View v) {
                        ColorChooserDialog dialog = new ColorChooserDialog(SettingsActivity.this);
                        dialog.setTitle("Couleurs du thème");
                        dialog.setColorListener(new ColorListener() {
                            @Override
                            public void OnColorClick (View v, int color) {
                                ShapeDrawable d = new ShapeDrawable(new OvalShape());
                                d.getPaint().setStyle(Paint.Style.FILL);
                                itemButtonIconRightView.setBackground(d);

                                Intent intent = new Intent(SettingsActivity.this, HomeActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(intent);
                                overridePendingTransition(R.anim.no_animation, R.anim.no_animation);
                                finish();
                            }
                        });

                        dialog.show();
                    }
                });


            } else {
                // if item
                convertView = inflater.inflate(R.layout.adapter_item, null);

                // Icon left
                ImageView itemIconView = convertView.findViewById(R.id.item_iconleft_settings);
                itemIconView.setImageResource(((EntryItem) itemList.get(i)).getIconleft());

                // Title
                TextView itemNameView = convertView.findViewById(R.id.item_name_settings);
                itemNameView.setText(((EntryItem) itemList.get(i)).getTitle());

            }


            return convertView;
        }


    }


    @Override
    public void onBackPressed () {
        super.onBackPressed();
        startActivity(new Intent(getApplicationContext(), HomeActivity.class));
        finish();
    }


    @Override
    public boolean onSupportNavigateUp () {
        onBackPressed();
        finish();
        return super.onSupportNavigateUp();
    }


    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.no_animation,  R.anim.no_animation);
    }

}

0 个答案:

没有答案