2012-08-25 144 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