2017-02-07 25 views
0

您好,我是Android開發新手! 我正在學習它我自己,我只是有一個網格視圖中有多個文本視圖。我想更改我點擊的文本視圖的文本顏色! 文本視圖應該改變我點擊的顏色。GridView在點擊時保留TextColor

我做它喜歡: 這是我的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; 
     } 
    } 
} 

它確實改變文字的顏色,但只有當我點擊它我的意思是我想保留文本視圖T的藍色分機上,我點擊,

在此先感謝

+0

請分享您的適配器代碼 –

+0

更新了我的疑問,請查看適配器代碼 –

+0

你在找什麼看不清?你可以分享什麼樣的期望輸出的截圖,以及當前的輸出是什麼? – Rachit

回答

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

試試這個

0
  1. 在您的CategoryModel中添加一個稱爲'點擊'
  2. 集這個領域真正的時,其點擊
  3. 調用notifydatasetchanged()方法
  4. 在適配器getview()方法

最終CategoryModel myItem = mItems.get(位置);

if(myItem.clicked){ 
     yourTextView.settextcolor("your color"); 
}else{ 
     yourTextView.settextcolor("default color"); 
} 
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; 
    }