使用 Cursoradapter 和 bindView 更改 ListView 的行颜色
Change row color of a ListView with Cursoradapter and bindView
我遇到了一些麻烦,当我单击我的列表视图时,我想更改所选行的颜色,但是有几行更改了颜色,而不是我想要的颜色。
如果我点击另一行,我希望之前点击的行保持他的颜色。
还有一个小问题,因为我每次都调用 setAdapter,所以每次单击时列表都会向上滚动
Fragment中onclicklistener的代码:
private AdapterView.OnItemClickListener ajouter_joueur_liste_listener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View v, int position, long id ) {
//I take all player in my database
Cursor cursor = joueurDab.getAll();
CursorListJoueur cl = new CursorListJoueur(context, cursor, position, true);
liste_joueurs.setAdapter(cl);
}
}
游标适配器
public class CursorListJoueur extends CursorAdapter {
int poscolor;
boolean selection;
private CategorieDAO categorieDab;
public CursorListJoueur(Context pContext, Cursor c, int poscolor, boolean selection) {
super(pContext, c, 0);
this.poscolor = poscolor;
this.selection = selection;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
categorieDab = new CategorieDAO(context);
return LayoutInflater.from(context).inflate(R.layout.row_player, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
int pos = cursor.getPosition();
if ( poscolor != -1 ){
if ( cursor.getPosition() == poscolor && selection ) {
view.setBackgroundColor( Color.GRAY );
}else if ( cursor.getPosition() == poscolor && !selection ){
view.setBackgroundColor( Color.WHITE );
}
}
在 CursorListJoueur(context, cursor, int poscolor, boolean selection);
参数 poscolor 对应于我要着色的行,布尔值指示我是要设置灰色还是白色,您无需关心 if (poscolor != -1)
我看到了一些其他主题,但我不想对颜色行使用 XML 方法(因为我想在另一个 onclicklistener 中删除行颜色),如果可能的话,我想保留我的绑定视图() 并且不要使用 getView()
谢谢
您可以使用 SparseBooleanArray 来跟踪当前正在选择的项目,而不是在每次您的列表被点击时创建一个新的 Adapter
(这会导致列表滚动回顶部)。
在您的适配器中:
public class YourAdapter extends CursorAdapter {
// Initialize the array
SparseBooleanArray selectionArray = new SparseBooleanArray();
...
// Method to mark items in selection
public void setSelected(int position, boolean isSelected) {
selectionArray.put(position, isSelected);
}
...
@Override
public void bindView(View view, Context context, Cursor cursor) {
int position = cursor.getPosition();
boolean isSelected = selectionArray.get(position);
if (isSelected ) {
view.setBackgroundColor( Color.GRAY );
} else if (!isSelected){
view.setBackgroundColor( Color.WHITE );
}
}
}
然后在单击项目时,您可以同样切换选择:
@Override
public void onItemClick(AdapterView parent, View v, int position, long id ) {
YourAdapter adapter = (YourAdapter) parent.getAdapter();
adapter.setSelected(position, true);
adapter.notifyDataSetChanged();
}
希望这对您有所帮助。 :)
我遇到了一些麻烦,当我单击我的列表视图时,我想更改所选行的颜色,但是有几行更改了颜色,而不是我想要的颜色。
如果我点击另一行,我希望之前点击的行保持他的颜色。
还有一个小问题,因为我每次都调用 setAdapter,所以每次单击时列表都会向上滚动
Fragment中onclicklistener的代码:
private AdapterView.OnItemClickListener ajouter_joueur_liste_listener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View v, int position, long id ) {
//I take all player in my database
Cursor cursor = joueurDab.getAll();
CursorListJoueur cl = new CursorListJoueur(context, cursor, position, true);
liste_joueurs.setAdapter(cl);
}
}
游标适配器
public class CursorListJoueur extends CursorAdapter {
int poscolor;
boolean selection;
private CategorieDAO categorieDab;
public CursorListJoueur(Context pContext, Cursor c, int poscolor, boolean selection) {
super(pContext, c, 0);
this.poscolor = poscolor;
this.selection = selection;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
categorieDab = new CategorieDAO(context);
return LayoutInflater.from(context).inflate(R.layout.row_player, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
int pos = cursor.getPosition();
if ( poscolor != -1 ){
if ( cursor.getPosition() == poscolor && selection ) {
view.setBackgroundColor( Color.GRAY );
}else if ( cursor.getPosition() == poscolor && !selection ){
view.setBackgroundColor( Color.WHITE );
}
}
在 CursorListJoueur(context, cursor, int poscolor, boolean selection);
参数 poscolor 对应于我要着色的行,布尔值指示我是要设置灰色还是白色,您无需关心 if (poscolor != -1)
我看到了一些其他主题,但我不想对颜色行使用 XML 方法(因为我想在另一个 onclicklistener 中删除行颜色),如果可能的话,我想保留我的绑定视图() 并且不要使用 getView()
谢谢
您可以使用 SparseBooleanArray 来跟踪当前正在选择的项目,而不是在每次您的列表被点击时创建一个新的 Adapter
(这会导致列表滚动回顶部)。
在您的适配器中:
public class YourAdapter extends CursorAdapter {
// Initialize the array
SparseBooleanArray selectionArray = new SparseBooleanArray();
...
// Method to mark items in selection
public void setSelected(int position, boolean isSelected) {
selectionArray.put(position, isSelected);
}
...
@Override
public void bindView(View view, Context context, Cursor cursor) {
int position = cursor.getPosition();
boolean isSelected = selectionArray.get(position);
if (isSelected ) {
view.setBackgroundColor( Color.GRAY );
} else if (!isSelected){
view.setBackgroundColor( Color.WHITE );
}
}
}
然后在单击项目时,您可以同样切换选择:
@Override
public void onItemClick(AdapterView parent, View v, int position, long id ) {
YourAdapter adapter = (YourAdapter) parent.getAdapter();
adapter.setSelected(position, true);
adapter.notifyDataSetChanged();
}
希望这对您有所帮助。 :)