嘿伙计们,我在过去的4天里因为这个问题而苦苦挣扎,没有提供完整解决方案,所以我最终完成了这个问题并希望与您分享代码。
答案 0 :(得分: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。
答案 1 :(得分:1)
这就是我解决这个问题的方法看到伙计我给出了这个问题的完整代码让我知道你们中是否有人还有这个问题....
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;
}