2012-08-25 147 views
2

我在Android application.In工作我的主要活動變化的列表視圖字段屬性我要實現以下一個list.The是我的網頁的樣本形狀從主要活動

|----------------------| \ 
| |Button|   | \ 
|----------------------| \     
|listview row1   | \ \ 
|listview row1   | \ \---------Screen 
|listview row1   |/--/----- ListView 
|      |/ /
|      |/
|      |/
|______________________|/ 

按鈕是在我活動頁面和列表視圖行在baseadapter.List創建包含textview.Now我必須更改Textviews背景顏色,當我點擊從活動按鈕,下次我點擊按鈕textviews的顏色將保留舊的顏色。How can我做朋友嗎?我在getview()方法中聲明瞭textview。

+0

是否要更改所有textview背景顏色? –

+0

@ChiragRaval ...是 – sarath

回答

0

最後我得到了solution.It可能有助於others.But我不看好我的代碼的質量。

步驟1)

我瞭解創建變量在我的活動

static int hidestate=0; 

而在上點擊的方法的隱藏按鈕我寫這

hide_btn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      if (hidestate==0) { 

       hidestate=1; 

       sMSConversationAdapter.notifyDataSetChanged(); 
       hide_btn.setText("Show All"); 

      }else { 
       hidestate=0; 

       sMSConversationAdapter.notifyDataSetChanged(); 
       hide_btn.setText("Hide All"); 
      } 

     } 
    }); 

步驟2) 以下是我BaseAdapter類getView()

public View getView(final int position, View convertView, ViewGroup parent) { 

    View vi=convertView; 
    final TextView message; 

    if(convertView==null) 
     vi = inflater.inflate(R.layout.smsconversation_row, null); 

    RelativeLayout nws=(RelativeLayout)vi.findViewById(R.id.all); 
    message=(TextView)vi.findViewById(R.id.snt_txt); 

    if (SMSConversationHome.hidestate==1) { 
     message.setVisibility(View.INVISIBLE); 

    } 
    else{ 

     message.setVisibility(View.VISIBLE); 

    } 
} 

謝謝朋友爲你的幫助。

2

可能還有其他方法,但是我會在按鈕的OnClick方法中遍歷列表行。喜歡的東西:

在您的活動字段定義:

static final int colourA=Color.argb(255,255,0,0); 
    static final int colourB=Color.argb(255,0,255,0); 
    int currentColour=colourA; 

在您的活動的OnCreate:

 Button myButton = (Button) findViewById(R.id.myButton); 
     final ListView myListView = (ListView) findViewByID(R.id.myListView); 
     //change myButton to your button id, and myListView to your ListView id 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       //This is the code to toggle the colours, you can do pretty much whatever you want here though 
       if (currentColour==colourA){ 
        currentColour=colourB; 
       } else { 
        currentColour=colourA; 
       } 

       //This cycles through all the root views in the ListView. If you want to change the 
       //colour of only one view in the row layout, in the for loop use 
       //rowView.findViewById(R.id.myViewInRow).setBackgroundColor(currentColour); 
       //instead, to get the relevant view in the row 
       View rowView; 
       for (int i=0;i<myListView.getChildCount();i++){ 
        rowView=myListView.getChildAt(i); 
        rowView.setBackgroundColor(currentColour); 
       } 
      } 
     }); 
+0

@ skyrift ..我正在使用自定義的基礎適配器。 – sarath

+0

如果您只想更改行的背景顏色,則無論使用的是哪種適配器 - 它都在該級別以上運行,並使用ListView本身,它都可以工作。儘管從下面發佈的解決方案看來,您似乎希望隱藏佈局的一部分,而不是更改它的顏色。 – skyrift