[英]gridview change item background by position in Android
I have Custom BaseAdapter
. 我有Custom BaseAdapter
。 Which i used in my GridView
. 我在GridView
。 I trying to change Item background color by position(also i change Textview's text color) this is a my baseadapter
source 我试图通过位置更改项目背景颜色(我也更改了Textview的文本颜色),这是我的基本baseadapter
源
public class ServiciesGridAdapter extends BaseAdapter {
private Context context;
private List<ServicesListItem> itemList;
public ServiciesGridAdapter(Context context, List<ServicesListItem> mobileValues) {
this.context = context;
this.itemList = mobileValues;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
gridView = inflater.inflate(R.layout.view_servisies_gridview_items, null);
final LinearLayout mainLayout = (LinearLayout) gridView.findViewById(R.id.gridview_items_layout);
final TextView serviciesID = (TextView) gridView.findViewById(R.id.servicies_grid_id);
final TextView serviciesName = (TextView) gridView.findViewById(R.id.servicies_grid_name);
serviciesID.setText(itemList.get(position).getName());
serviciesName.setText(itemList.get(position).getIdentifier() + " EUR");
mainLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mainLayout.setBackgroundResource(R.drawable.rounded_corners_blue);
serviciesID.setTextColor(Color.parseColor("#ffffff"));
serviciesName.setTextColor(Color.parseColor("#ffffff"));
}
});
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
when i run my app in item click i can change background color, but when i checked another item,first item's background is a same. 当我在项目中运行我的应用程序时,我可以更改背景颜色,但是当我检查另一个项目时,第一个项目的背景是相同的。 Simply, i want to change only one items background each click. 简而言之,我每次单击仅更改一个项目背景。
How i can solve my problem? 我该如何解决我的问题?
you have to reset all other view text color and background, so make a loop and do it.. try below code 您必须重置所有其他视图文本的颜色和背景,因此进行循环并执行此操作..尝试以下代码
mainLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<parent.getChildCount();i++){
View c = parent.getChildAt(i);
c.findViewById(R.id.gridview_items_layout).setBackgroundResource(defaultResource);
(TextView) c.findViewById(R.id.servicies_grid_id).setTextColor(defaultColor);
((TextView) c.findViewById(R.id.servicies_grid_name)).setTextColor(defaultColor);
}
mainLayout.setBackgroundResource(R.drawable.rounded_corners_blue);
serviciesID.setTextColor(Color.parseColor("#ffffff"));
serviciesName.setTextColor(Color.parseColor("#ffffff"));
}
});
use of listSelector for change item background by position in gridview .. 使用listSelector通过gridview ..中的位置更改项目背景。
add color you color.xml
为color.xml
添加颜色
<resources>
<color name="onClickColor">#FFFFFF</color>
</resources>
add selecter.xml
to drawable
folder 将selecter.xml
添加到drawable
文件夹
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@color/onClickColor">
</item>
</selector>
add listSelector
to your GridView
将listSelector
添加到您的GridView
<GridView
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="@drawable/selecter">
</GridView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.