2017-01-02 120 views
0

GridView有一個Image和灰色​​的文本選擇任何項目我需要改變文本和圖像的灰色的顏色爲其他顏色(橙色) ,並選擇其他網格項目我需要改變以前選定的項目默認灰色,並選擇一個橙色..GirdView項目顏色變化選擇和以前選擇的項目應顯示正常的顏色

我試過一些解決方案,但沒有得到我的正確輸出..請幫我解決這個問題 this是我試過的:

private int previousSelectedPosition = -1; 

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
      ImageView selectedImageView = (ImageView) gridView.findViewById(R.id.groupbyImage); 
      TextView selectedTextView= (TextView) gridView.findViewById(R.id.groupbyHeader); 
      if (previousSelectedPosition != position && previousSelectedPosition!=-1) { 
       selectedImageView.setSelected(false); 
       // Set the last selected View background color as deselected item 
       selectedImageView.setColorFilter(getResources().getColor(R.color.gridsepration)); 
       // Set the last selected View text color as deselected item 
       selectedTextView.setTextColor(getResources().getColor(R.color.gridsepration)); 
      } else { 

       selectedTextView.setTextColor(getResources().getColor(R.color.violet)); 
       selectedImageView.setColorFilter(getResources().getColor(R.color.violet)); 
      } 
      // Set the current selected view position as previousSelectedPosition 
      previousSelectedPosition = position; 

     } 
    }); 

終於我得到了解決方案..找到下面的解決方案。

+0

發佈您的代碼.. – rafsanahmad007

回答

0

我得到了解決方案,感謝所有回覆的人。

這是我做的。

在我adpater

public HashMap<Integer, Boolean> hashMapSelected; 

在我的適配器構造

 hashMapSelected = new HashMap<>(); 
    for (int i = 0; i < headers.size(); i++) { 
     hashMapSelected.put(i, false); 
    } 

在我的適配器getView

if (hashMapSelected.get(position) == true) { 
     viewHolder.imageView.setColorFilter(ContextCompat.getColor(mContext, R.color.violet)); 
     viewHolder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.violet)); 
    } else { 
     viewHolder.imageView.setColorFilter(ContextCompat.getColor(mContext, R.color.gridsepration)); 
     viewHolder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.gridsepration)); 
    } 

一個額外的方法來清除適配器其他項目

public void makeAllUnselect(int position) { 
    hashMapSelected.put(position, true); 
    for (int i = 0; i < hashMapSelected.size(); i++) { 
     if (i != position) 
      hashMapSelected.put(i, false); 
    } 
} 

終於我的網格視圖setOnItemClickListener

adapter.makeAllUnselect(position); 
adapter.notifyDataSetChanged(); 

最終我的適配器看起來像這樣

public class DataAdapter extends BaseAdapter { 
    private Context mContext; 
    private TypedArray images; 
    private List<String> headers; 
    public HashMap<Integer, Boolean> hashMapSelected; 

public DataAdapter(Context context, TypedArray images, List<String> headers) { 
    this.mContext = context; 
    this.images = images; 
    this.headers = headers; 
    hashMapSelected = new HashMap<>(); 
    for (int i = 0; i < headers.size(); i++) { 
     hashMapSelected.put(i, false); 
    } 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return images.length(); 
} 

@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

public void makeAllUnselect(int position) { 
    hashMapSelected.put(position, true); 
    for (int i = 0; i < hashMapSelected.size(); i++) { 
     if (i != position) 
      hashMapSelected.put(i, false); 
    } 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    final ViewHolder viewHolder; 

    if (convertView == null) { 
     convertView = LayoutInflater.from(mContext).inflate(R.layout.groupby_grid_item, parent, false); 
     viewHolder = new ViewHolder(convertView); 
     convertView.setTag(viewHolder); 

    } else 
     viewHolder = (ViewHolder) convertView.getTag(); 
    viewHolder.textView.setText(headers.get(position)); 
    viewHolder.imageView.setImageResource(images.getResourceId(position, -1)); 


    if (hashMapSelected.get(position) == true) { 
     viewHolder.imageView.setColorFilter(ContextCompat.getColor(mContext, R.color.violet)); 
     viewHolder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.violet)); 
    } else { 
     viewHolder.imageView.setColorFilter(ContextCompat.getColor(mContext, R.color.gridsepration)); 
     viewHolder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.gridsepration)); 
    } 
    return convertView; 
} 


private class ViewHolder { 
    TextView textView; 
    ImageView imageView; 

    public ViewHolder(View view) { 
     textView = (TextView) view.findViewById(R.id.groupbyHeader); 
     imageView = (ImageView) view.findViewById(R.id.groupbyImage); 
    } 
} 
} 

而且我的GridView OnItemClick

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
      selectedGroupBy = getHeaders().get(position); 
      adapter.makeAllUnselect(position); 
      adapter.notifyDataSetChanged(); 
     } 
    }); 
0

試試這個代碼它工作它是一個示例工作代碼,在這個選擇和網格視圖中的項目背景顏色與文本顏色一起改變。並且前一個選定的項目也被取消選擇,並且在此之前,我也使用了一個textview。

以下是xml網格視圖的代碼。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/rl" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="10dp" 
    tools:context=".MainActivity" 
    android:background="#e8594c" 
    > 
    <TextView 
     android:id="@+id/tv_message" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#f85d4f" 
     android:layout_marginBottom="15dp" 
     android:padding="10dp" 
     /> 
    <GridView 
     android:id="@+id/gv" 
     android:layout_width="650dp" 
     android:layout_height="wrap_content" 
     android:padding="2dp" 
     android:numColumns="3" 
     android:background="#d84521" 
     android:verticalSpacing="2dp" 
     android:horizontalSpacing="2dp" 
     android:stretchMode="columnWidth" 
     android:gravity="left" 
     android:layout_below="@id/tv_message" 
     > 
    </GridView> 
</RelativeLayout> 

現在的java文件代碼如下:

import android.graphics.Color; 
import android.graphics.Typeface; 
import android.os.Bundle; 
import android.app.Activity; 
import android.util.TypedValue; 
import android.view.Gravity; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.GridView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import android.widget.RelativeLayout.LayoutParams; 

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

import android.content.res.Resources; 


public class MainActivity extends Activity { 
    private int previousSelectedPosition = -1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Get the widgets reference from XML layout 
     final GridView gv = (GridView) findViewById(R.id.gv); 
     final TextView tv_message = (TextView) findViewById(R.id.tv_message); 

     // Initializing a new String Array 
     String[] plants = new String[]{ 
       "Catalina ironwood", 
       "Cabinet cherry", 
       "Pale corydalis", 
       "Pink corydalis", 
       "Belle Isle cress", 
       "Land cress", 
       "Orange coneflower", 
       "Coast polypody", 
       "Water fern" 
     }; 

     // Populate a List from Array elements 
     final List<String> plantsList = new ArrayList<String>(Arrays.asList(plants)); 

     // Data bind GridView with ArrayAdapter (String Array elements) 
     gv.setAdapter(new ArrayAdapter<String>(
       this, android.R.layout.simple_list_item_1, plantsList){ 
      public View getView(int position, View convertView, ViewGroup parent) { 

       // Return the GridView current item as a View 
       View view = super.getView(position,convertView,parent); 

       // Convert the view as a TextView widget 
       TextView tv = (TextView) view; 

       // set the TextView text color (GridView item color) 
       tv.setTextColor(Color.DKGRAY); 

       // Set the layout parameters for TextView widget 
       RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
         LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT 
       ); 
       tv.setLayoutParams(lp); 

       // Get the TextView LayoutParams 
       RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)tv.getLayoutParams(); 

       // Set the width of TextView widget (item of GridView) 
       params.width = getPixelsFromDPs(MainActivity.this,168); 

       // Set the TextView layout parameters 
       tv.setLayoutParams(params); 

       // Display TextView text in center position 
       tv.setGravity(Gravity.CENTER); 

       // Set the TextView text font family and text size 
       tv.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL); 
       tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12); 

       // Set the TextView text (GridView item text) 
       tv.setText(plantsList.get(position)); 

       // Set the TextView background color 
       tv.setBackgroundColor(Color.parseColor("#FFFF4F25")); 

       // Return the TextView widget as GridView item 
       return tv; 
      } 
     }); 

     gv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       // Get the selected item text 
       String selectedItem = parent.getItemAtPosition(position).toString(); 

       // Display the selected item text to app interface 
       tv_message.setText("Selected item : " + selectedItem); 

       // Get the current selected view as a TextView 
       TextView tv = (TextView) view; 

       // Set the current selected item background color 
       tv.setBackgroundColor(Color.parseColor("#FF9AD082")); 

       // Set the current selected item text color 
       tv.setTextColor(Color.BLUE); 

       // Get the last selected View from GridView 
       TextView previousSelectedView = (TextView) gv.getChildAt(previousSelectedPosition); 

       // If there is a previous selected view exists 
       if (previousSelectedPosition != -1) 
       { 
        // Set the last selected View to deselect 
        previousSelectedView.setSelected(false); 

        // Set the last selected View background color as deselected item 
        previousSelectedView.setBackgroundColor(Color.parseColor("#FFFF4F25")); 

        // Set the last selected View text color as deselected item 
        previousSelectedView.setTextColor(Color.DKGRAY); 
       } 

       // Set the current selected view position as previousSelectedPosition 
       previousSelectedPosition = position; 
      } 
     }); 
} 

    // Method for converting DP value to pixels 
    public static int getPixelsFromDPs(Activity activity, int dps){ 
     Resources r = activity.getResources(); 
     int px = (int) (TypedValue.applyDimension(
       TypedValue.COMPLEX_UNIT_DIP, dps, r.getDisplayMetrics())); 
     return px; 
    } 
} 

這段代碼的輸出是:

之前

enter image description here

enter image description here

希望它能幫助你!

+0

NO這個人是選擇整個背景視圖爲藍色和其他trasperent ..我的需求是選定的項目應該改變它顏色和其他項目應該是默認顏色。 – Amith

+0

好的,讓我編輯我的代碼,併爲您提供替代文本顏色更改的解決方案。 –

+0

不僅文本我需要改變我的圖像顏色也。這就是我所做的 – Amith

0

我你的模型(網格視圖對象):

  1. 你應該把一個布爾變量來知道哪一個是選擇與否。
  2. 當你選擇其中一個你應該設置真布爾變量,如果你想另一個卡會忽略你應該設置錯誤的網格視圖的布爾變量。
  3. 進行任何更改後(如選擇的網格視圖中的一個),你必須調用mAdapter.notifyDataSetChanged();
1

您需要添加在你的模型類的選擇多了一個場。 所以你可以從你的適配器的getView()方法中設置選擇。在您的適配器中使用viewHolder。

public View getView(final int position, View convertView, ViewGroup parent) { 
    final ViewHolder holder; 

    if (convertView == null) { 

     convertView = LayoutInflater.from(context).inflate(
       R.layout.your_row_file, null); 

     holder = new ViewHolder(); 

     holder.selectedImageView = (ImageView) convertView 
       .findViewById(R.id.groupbyImage); 
     holder.selectedTextView = (TextView) convertView 
       .findViewById(R.id.groupbyHeader); 

     convertView.setTag(holder); 

    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    if (yourArrayList.get(position).getSelected()) { 
      selectedTextView.setTextColor(getResources().getColor(R.color.violet)); 
      selectedImageView.setColorFilter(getResources().getColor(R.color.violet)); 
     } else { 
    // Set the last selected View background color as deselected item 
      selectedImageView.setColorFilter(getResources().getColor(R.color.gridsepration)); 
      // Set the last selected View text color as deselected item 
      selectedTextView.setTextColor(getResources().getColor(R.color.gridsepration)); 
    } 


    convertView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     // =========== Update IsSelected Value in Day Array====== 

      boolean isSelected=yourArrayList.get(position).getSelected(); 
      yourArrayList.get(position).setSelected(!isSelected); 

      notifyDataSetChanged(); 
     } 
    }); 
    return convertView; 
} 
+0

謝謝@Bhoomika Patel ..我明白了,我也和你一樣在 – Amith

+0

@Amith很高興聽到你有解決方案。接受我的回答,這對其他人會有所幫助。 –