我正在开发一个Android应用程序。在我的主要活动中,我必须实现一个列表。以下是我页面的示例形状
|----------------------| \
| |Button| | \
|----------------------| \
|listview row1 | \ \
|listview row1 | \ \---------Screen
|listview row1 | / --/----- ListView
| |/ /
| | /
| | /
|______________________|/
按钮位于我的活动页面中,listview行正在baseadapter中创建.Listview包含textview.Now我必须在单击活动按钮时更改Textviews背景颜色,下次单击按钮时textviews颜色将保留旧的颜色。我怎么能做朋友?我在getview()方法中声明了textview。
答案 0 :(得分: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);
}
}
});
答案 1 :(得分:0)
最后我得到了解决方案。它可能对其他人有帮助。但我不是关于代码的质量。
步骤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);
}
}
谢谢你们的朋友为你们提供帮助。