如何动态更改所有颜色textview

时间:2016-02-16 06:28:18

标签: android listview

我有代码,如果点击一个,我的textview颜色被更改,但它只更改1行..我想更改所有列表项颜色 enter image description here

这是我的剧本 这是我的代码 我不知道这段代码有什么问题,

PS:它甚至不按我的按钮,有时候如果我滚动列表视图,颜色会自动改变

    public void colorToggle(View view) {
    int[] attrs = {android.R.attr.popupBackground};
    TypedArray ta = obtainStyledAttributes(R.style.MyApp_PopupMenu, attrs);
    final LinearLayout propLayout = (LinearLayout) findViewById(R.id.leot);
    ListView listView = (ListView) findViewById(android.R.id.list);
    TextView textView = (TextView) findViewById(R.id.wilayah);
    switch (view.getId()) {
        case R.id.blueButton: {
            int holoBlue = getResources().getColor(R.color.holo_blue_light);
            mFab.setColor(holoBlue);
            getActionBar().setBackgroundDrawable(new ColorDrawable(holoBlue));
            mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
            int popupBackground = ta.getColor(0, R.color.holo_blue_light);
            Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
            propLayout.setVisibility(View.GONE);
            listView.setDivider(new ColorDrawable(holoBlue));
            listView.setDividerHeight(1);
            textView.setTextColor(holoBlue);
            break;
        }
        case R.id.purpleButton: {
            int holoPurple = getResources().getColor(R.color.holo_purple);
            mFab.setColor(holoPurple);
            getActionBar().setBackgroundDrawable(new ColorDrawable(holoPurple));
            mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
            int popupBackground = ta.getColor(0, R.color.holo_purple);
            Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
            propLayout.setVisibility(View.GONE);
            listView.setDivider(new ColorDrawable(holoPurple));
            listView.setDividerHeight(1);
            textView.setTextColor(holoPurple);
            break;
        }
        case R.id.greenButton: {
            int holoGreen = getResources().getColor(R.color.holo_green_light);
            mFab.setColor(holoGreen);
            getActionBar().setBackgroundDrawable(new ColorDrawable(holoGreen));
            mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
            int popupBackground = ta.getColor(0, R.color.holo_green_light);
            Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
            propLayout.setVisibility(View.GONE);
            listView.setDivider(new ColorDrawable(holoGreen));
            listView.setDividerHeight(1);
            textView.setTextColor(holoGreen);
            break;
        }
        case R.id.orangeButton: {
            int holoOrange = getResources().getColor(R.color.holo_orange_light);
            mFab.setColor(holoOrange);
            getActionBar().setBackgroundDrawable(new ColorDrawable(holoOrange));
            mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
            int popupBackground = ta.getColor(0, R.color.holo_orange_light);
            Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
            propLayout.setVisibility(View.GONE);
            listView.setDivider(new ColorDrawable(holoOrange));
            listView.setDividerHeight(1);
            textView.setTextColor(holoOrange);
            break;
        }
        case R.id.redButton: {
            int holoRed = getResources().getColor(R.color.holo_red_light);
            mFab.setColor(holoRed);
            getActionBar().setBackgroundDrawable(new ColorDrawable(holoRed));
            mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
            int popupBackground = ta.getColor(0, R.color.holo_red_light);
            Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
            propLayout.setVisibility(View.GONE);
            listView.setDivider(new ColorDrawable(holoRed));
            listView.setDividerHeight(1);
            textView.setTextColor(holoRed);
            break;
        }
    }
    ta.recycle();    
}

3 个答案:

答案 0 :(得分:0)

换色后尝试拨打adapter's notifyDataSetChanged()。它应该为每个项目重新绘制整个listview的新颜色。

答案 1 :(得分:0)

  

如果我滚动ListView,颜色会自动变化

滚动时,隐藏的ListView中的项目将被回收或重复使用。

有关详细说明,请参阅this

您使用ListView的方式似乎不正确。我建议您使用带模型的适配器并将此适配器分配给ListView

请参阅this以使用自定义适配器实现ListView。

  

我想更改所有列表项颜色

首先实现自定义适配器。当颜色改变时,只需循环浏览模型列表并更改每个项目的颜色。然后致电适配器' notifyDataSetChanged()

希望这可以帮助您解决问题。

答案 2 :(得分:0)

希望它能帮助您实现输出!

enter image description here

  

ThirdActivity.java(包括适配器类)

public class ThirdActivity extends Activity {

    View view_red, view_blue;

    ListView lst_data;

    int mySelectedColor;

    MyListAdapter myListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_3_list);

        lst_data = (ListView) findViewById(R.id.lst_data);

        // Statically taken two views for color selection
        view_blue = findViewById(R.id.view_blue);
        view_red = findViewById(R.id.view_red);

        mySelectedColor = ContextCompat.getColor(this, android.R.color.holo_red_dark);

        myListAdapter = new MyListAdapter(this, mySelectedColor);
        lst_data.setAdapter(myListAdapter);

        view_blue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (myListAdapter == null) {

                    myListAdapter = new MyListAdapter(ThirdActivity.this, ContextCompat.getColor(ThirdActivity.this, android.R.color.holo_blue_dark));
                    lst_data.setAdapter(myListAdapter);

                    return;
                }

                mySelectedColor = ContextCompat.getColor(ThirdActivity.this, android.R.color.holo_blue_dark);

                myListAdapter.notifyDataSetChanged();

            }
        });

        view_red.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (myListAdapter == null) {

                    myListAdapter = new MyListAdapter(ThirdActivity.this, ContextCompat.getColor(ThirdActivity.this, android.R.color.holo_red_dark));
                    lst_data.setAdapter(myListAdapter);

                    return;
                }

                mySelectedColor = ContextCompat.getColor(ThirdActivity.this, android.R.color.holo_red_dark);

                myListAdapter.notifyDataSetChanged();

            }
        });


    }


    public class MyListAdapter extends BaseAdapter {


        private int selectedMyColor;
        private Context context;
        private LayoutInflater mLayoutInflater;

        public MyListAdapter(Context contetx, int mySelectedColor) {

            this.context = contetx;

            mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            this.selectedMyColor = mySelectedColor;

        }

        @Override
        public int getCount() {
            return 20;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

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

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            ViewHolder holder = null;

            if (view == null) {

                //The view is not a recycled one: we have to inflate
                view = mLayoutInflater.inflate(R.layout.demo_3_row_layout, parent, false);

                holder = new ViewHolder();

                holder.txt_title = (TextView) view.findViewById(R.id.txt_title);


                view.setTag(holder);

            } else {

                holder = (ViewHolder) view.getTag();
            }

            holder.txt_title.setTextColor(mySelectedColor);

            return view;
        }


        class ViewHolder {

            TextView txt_title;

        }
    }

}