2016-02-16 41 views
0

我有一些代碼,其中一個,如果點擊,我TextView的顏色發生變化,但它只能改變1線..我想改變這一切列表項的顏色 enter image description here如何改變所有顏色的TextView動態

這裏是我的screenshoot 這是我的代碼 我不知道什麼是錯與此代碼,

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();  
} 

回答

0

嘗試在顏色更改後致電adapter'snotifyDataSetChanged()。它應該重新繪製整個listview與每個項目的新顏色。

0

如果我滾動的ListView,本身

滾動時,在ListView這是隱藏的項目將被回收或再使用的顏色變化。

查看this的詳細解釋。

您使用ListView的方式似乎不正確。我建議你使用帶有型號的適配器並將此適配器分配給ListView

參見this用於實現具有自定義適配器的ListView。

我想改變所有列表項的顏色

首先實現自定義適配器。當顏色改變時,只需循環瀏覽模型列表並更改每個項目的顏色。然後調用適配器的notifyDataSetChanged()

希望這會幫助你解決你的問題。

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; 

     } 
    } 

}