【发布时间】:2017-02-07 06:03:55
【问题描述】:
您好,我是 Android 开发新手! 我正在自己学习它,我只有一个网格视图,里面有多个文本视图。我想更改单击的文本视图的文本颜色! 文本视图应该改变我点击的颜色。
我这样做是这样的: 这是我的 GridView 项目 xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dp"
android:paddingTop="2dp">
<TextView
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="@drawable/bg_tv_categories"
android:ellipsize="end"
android:gravity="center"
android:lines="1"
android:padding="2dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/text_pressed"
android:textSize="15sp"></TextView>
</RelativeLayout>
我的选择器 xml 是:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/blue"/>
<item android:color="@android:color/holo_red_light"/>
</selector>
适配器是:
private static final class GridAdapter extends BaseAdapter {
final ArrayList<CategoryModel> mItems;
final int mCount;
final Context ctx;
TextView text = null;
View view;
/**
* Default constructor
*
* @param items to fill data to
*/
private GridAdapter(final ArrayList<CategoryModel> items, Context ctx) {
mCount = items.size();
//mItems = new ArrayList<String>();
mItems = items;
this.ctx=ctx;
}
@Override
public int getCount() {
return mCount;
}
@Override
public Object getItem(final int position) {
return mItems.get(position);
}
@Override
public long getItemId(final int position) {
return position;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
view = convertView;
if (view == null) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.gridview_item, parent, false);
text = (TextView) view.findViewById(R.id.item);
}
final CategoryModel myItem = mItems.get(position);
if (text != null)
text.setText(myItem.name);
//text.setText(mItems.get(position));
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//text.setBackgroundColor(ctx.getResources().getColor(R.color.color_77));
categories = myItem.id;
//Toast.makeText(parent.getContext(), "" + myItem.id, Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
}
它确实改变了文本颜色,但只有当我点击它时,我的意思是我想保留我点击的文本视图文本的蓝色,
提前致谢
【问题讨论】:
-
请分享您的适配器代码
-
更新了我的问题请检查适配器代码
-
不清楚您在寻找什么?你能分享一下预期输出和当前输出的截图吗?
-
我只想在点击时更改网格视图中文本视图项的颜色!