如何使用切换按钮更改Recycler View中项目的文本颜色?

时间:2017-10-20 21:42:05

标签: android

我使用Switch来更改Activity的背景和文本颜色。但我不知道如何通过切换开关来更改RecyclerView项目的文本颜色 这是我的MainActivity.java

package com.example.gagandeep.test;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    SeekBar seekBar;
    RecyclerView recyclerView;
    ItemAdapter adapter;
    Toolbar toolbar;
    Switch switchToggle;
    TextView head;
    ConstraintLayout layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setSupportActionBar(toolbar);
        findViews();
        click();



    }

    private void findViews() {
        seekBar = findViewById(R.id.seekBar);
        recyclerView = findViewById(R.id.recycler);
        toolbar = findViewById(R.id.toolbar);
        layout = findViewById(R.id.constraint);
        head = findViewById(R.id.head);
    }

    private void click() {
        seekBar.setMax(20);
        seekBar.setProgress(10);
        timeTable(10);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress==0){
                    Toast.makeText(MainActivity.this, "Multiplying a number by 0 always gives zero", Toast.LENGTH_SHORT).show();
                    seekBar.setProgress(1);
                    timeTable(1);
                }
                else
                    timeTable(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private void timeTable(int progress) {
        ArrayList<Item> table = new ArrayList<>();
        for (int i=0; i<=20; i++){
            table.add(new Item(i + " * " + progress + " = " + i*progress ));
        }
        adapter = new ItemAdapter(MainActivity.this, table);
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(adapter);


    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);

        switchToggle = menu.findItem(R.id.myswitch)
                .getActionView().findViewById(R.id.switchAB);

        switchToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                if (isChecked) {
                    checked();
                }
                if (!isChecked){
                    unChecked();
                }
            }
        });
        return true;
    }

    private void unChecked() {
        layout.setBackgroundColor(getResources().getColor(android.R.color.white));
        toolbar.setTitleTextColor(getResources().getColor(android.R.color.black));
    }

    private void checked() {
        layout.setBackgroundColor(getResources().getColor(android.R.color.black));
        toolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.myswitch:

                break;

        }
        return true;
    }

}

这是我的ItemAdapter.java

package com.example.gagandeep.test;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {


    public ItemAdapter(Context context, List<Item> list) {
        this.context = context;
        this.list = list;
    }

    private Context context;
    private List<Item> list;
    @Override
    public ItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemAdapter.ViewHolder holder, int position) {
        Item currentItem = list.get(position);
        holder.head.setText(currentItem.getHead());

    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView head;

        public ViewHolder(View itemView) {
            super(itemView);
            head = itemView.findViewById(R.id.head);
        }
    }
}

我必须在哪里进行更改才能在运行时更改textColor?

0 个答案:

没有答案