嘿,伙计们,我在过去 4 天里一直在为这个问题而苦苦挣扎,但没有给出完整的解决方案,所以最后我完成了这个问题,并想与你分享代码。
问问题
983 次
2 回答
1
你在寻找这样的东西吗?
LinearLayout newRow = new LinearLayout(getBaseContext());
newRow.setOrientation(LinearLayout.HORIZONTAL);
if(i % 2 != 0) {
newRow.setBackgroundColor(res.getColor(/* your color here: e.g., R.color.rowcols*/));
}
在增量器所在的循环中使用它i
,您可以将添加的每一行更改为不同的颜色。我使用了LinearLayouts并向它们添加了一些东西(然后将LinearLayouts添加到可滚动的父LinearLayout中),但是对于ListViews应该可以完成类似的方法。
于 2012-06-04T13:37:03.157 回答
1
我就是这样解决这个问题的
*这就是我的 Activity 看起来的样子--> * ****
public class MessageActivity extends Activity implements Observer,
OnClickListener, OnItemClickListener,
{
//My adapter where my selected position will be hold
private MySimpleAdapter adapter;
ListView listViewMsgs = null;
protected Context activity;
protected int position = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.message);
}
//从服务器获取数据后初始化你的游标适配器。
public void update(Observable arg0, Object arg1) {
String[] from = new String[] { "sender_number", "display_name",
"access_number", "timestamp", "message_type" };
int[] to = new int[] { R.id.photomsg, R.id.display_name,
R.id.access_number, R.id.timestamp1, R.id.message_type };
adapter = new MySimpleAdapter(this, this.model,
R.layout.messagelist, mymsglist, from, to);
listViewMsgs = (ListView) findViewById(R.id.listViewMsgs);
listViewMsgs.setAdapter(adapter);
listViewMsgs.setOnItemClickListener((OnItemClickListener) this);
listViewMsgs.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);
}
//您自己的适配器,我们将在其中设置选定的位置和所有。
public class MySimpleAdapter extends SimpleAdapter {
public MySimpleAdapter(Context context, MessageModel model, int layout,
ArrayList<HashMap<String, String>> mymsglist, String[] from,
int[] to) {
super(context, mymsglist, layout, from, to);
this.context = context;
this.mymsglist = mymsglist;
this.model = model;
}
**//Add this code inside your own CursorAdapter.....
//set the selected position of the row over here.
private int selectedPos = 0;//-1 // init value for not-selected
public void setSelectedPosition(int pos){
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
public int getSelectedPosition(){
return selectedPos;
}
public void setSelected(int position) {
selectedPosition = position;
}**
//One thing which most of the android developer i think dont know is when getView is called
//when you are making a ListView.....the answer is it will get called to draw each row of the ListView.
public View getView(int position, View convertView, ViewGroup parent) {
//The get view is called everytime a row is drawn and that is how the color of each row is drawn.
if(selectedPos == position){
row.setBackgroundColor(Color.LTGRAY);
}else{
row.setBackgroundColor(Color.WHITE);
}
}
return row;
}
}
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//on selection of a row basically we are setting the position now when getView will draw this this
//this row it will change the selected row color.
adapter.setSelectedPosition(position);
MessageActivity.this.position = position;
}
于 2012-06-07T05:54:22.107 回答