【问题标题】:GridView Retain the TextColor on ClickGridView 在单击时保留 TextColor
【发布时间】: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;
        }
    }
}

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

提前致谢

【问题讨论】:

  • 请分享您的适配器代码
  • 更新了我的问题请检查适配器代码
  • 不清楚您在寻找什么?你能分享一下预期输出和当前输出的截图吗?
  • 我只想在点击时更改网格视图中文本视图项的颜色!

标签: android gridview textview


【解决方案1】:

从您的 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();
            }
        });

试试这个

【讨论】:

    【解决方案2】:
    1. 在 CategoryModel 中添加一个名为“clicked”的字段
    2. 点击时将此字段设置为真
    3. 调用 notifydatasetchanged() 方法
    4. 在您的适配器 getview() 方法中

    final CategoryModel myItem = mItems.get(position);

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

    【讨论】:

      【解决方案3】:

      在您的适配器代码中

          @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;
          }
      

      【讨论】:

      • 它不起作用,因为加载新数据时也会改变颜色。我试过了。。
      猜你喜欢
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多