GridView在Click上保留TextColor

时间:2017-02-07 06:03:55

标签: android gridview textview

您好,我是Android开发新手! 我正在学习它,我只有一个网格视图,里面有多个文本视图。我想更改我单击的文本视图的文本颜色! 文本视图应该更改我单击的颜色。

我这样做: 这是我的GridView Item 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;
        }
    }
}

它确实改变了文本颜色,但只有当我点击它时,我的意思是我想保留我点击的文本视图文本的蓝色,

提前致谢

3 个答案:

答案 0 :(得分:0)

从XML中删除它:

android:background="@drawable/bg_tv_categories"

并在您的适配器中,确实喜欢

view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //text.setBackgroundColor(ctx.getResources().getColor(R.color.color_77));
                text.setTextColor(ContextCompat.getColor(mContext, R.color.blue_light));
                categories = myItem.id;
                //Toast.makeText(parent.getContext(), "" + myItem.id, Toast.LENGTH_SHORT).show();
            }
        });

试试这个

答案 1 :(得分:0)

  1. 在您的CategoryModel中添加一个名为“clicked”的字段
  2. 单击
  3. 时将此字段设置为true
  4. 调用notifydatasetchanged()方法
  5. 在你的适配器getview()方法
  6. final CategoryModel myItem = mItems.get(position);

    if(myItem.clicked){
          yourTextView.settextcolor("your color");
    }else{
          yourTextView.settextcolor("default color");
    }
    

答案 2 :(得分:0)

在适配器代码中

    @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) {
                //change the color
                text.setTextColor(getResources().getColor(R.color.my_color));
                categories = myItem.id;
                //Toast.makeText(parent.getContext(), "" + myItem.id, Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }