2015-02-10 51 views
0

我在一個活動中有一個網格我需要排序我寫了排序邏輯這裏是我的代碼。 但問題是當我試圖點擊任何網格項目。該項目的背景並沒有改變,有沒有辦法來改變項目背景網格視圖項目單擊顏色變化

HashMap<String, Integer> map = new HashMap<String, Integer>(); 
    ValueComparator bvc = new ValueComparator(map); 
    TreeMap<String, Integer> sorted_map = new TreeMap<String, Integer>(bvc); 

    map.put("Windows", sharedPref.getInt("Windows", 0)); 
    map.put("iOS", sharedPref.getInt("iOS", 0)); 
    map.put("Android", sharedPref.getInt("Android", 0)); 
    map.put("Blackberry", sharedPref.getInt("Blackberry", 0)); 
    map.put("Java", sharedPref.getInt("Java", 0)); 
    map.put("JQuery", sharedPref.getInt("JQuery", 0)); 
    map.put("Phonegap", sharedPref.getInt("Phonegap", 0)); 
    map.put("SQLite", sharedPref.getInt("SQLite", 0)); 
    map.put("Thread", sharedPref.getInt("Thread", 0)); 
    map.put("Video", sharedPref.getInt("Video", 0)); 
    sorted_map.putAll(map); 

    iconList = new ArrayList<Integer>(); 
    Map<String, Integer> treeMap = new TreeMap<String, Integer>(sorted_map); 

    for (Map.Entry<String, Integer> entry : treeMap.entrySet()) { 
    System.out.println("Key : " + entry.getKey() 
             + " Value : " + entry.getValue()); 


    sortedList.add(entry.getKey()); 

    } 
    package info.payism.onlineservice; 

import android.content.Context; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class CustomGridAdapter extends BaseAdapter { 

private Context context; 
private final String[] gridValues; 
ImageView imageView; 

// Constructor to initialize values 
public CustomGridAdapter(Context context, String[] gridValues) { 
    this.context = context; 
    this.gridValues = gridValues; 
} 

@Override 
public int getCount() { 

    // Number of times getView method call depends upon gridValues.length 
    return gridValues.length; 
} 

@Override 
public Object getItem(int position) { 

    return null; 
} 

@Override 
public long getItemId(int position) { 

    return 0; 
} 

// Number of times getView method call depends upon gridValues.length 

public View getView(int position, View convertView, ViewGroup parent) { 

    // LayoutInflator to call external grid_item.xml file 

    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View gridView; 

    if (convertView == null) { 

     gridView = new View(context); 

     // get layout from grid_item.xml 
     gridView = inflater.inflate(R.layout.grid_item, null); 

     imageView= (ImageView) gridView.findViewById(R.id.grid_item_image); 
     imageView.setBackgroundColor(Color.parseColor("#ffffff")); 

     // set value into textview 
     Typeface font = Typeface.createFromAsset(context.getAssets(), 
       "fonts/gotham-book-1361523257.ttf"); 
     TextView textView = (TextView) gridView 
       .findViewById(R.id.grid_item_label); 
     textView.setBackgroundColor(Color.parseColor("#ffffff")); 
     textView.setTypeface(font); 
     textView.setText(gridValues[position]); 

     String icon_tag_name = gridValues[position]; 

     if (icon_tag_name.equals("Mobile")) { 

      imageView.setImageResource(R.drawable.vmobile_blue); 

     } else if (icon_tag_name.equals("Data Card")) { 

      imageView.setImageResource(R.drawable.vdatacard_blue); 

     } else if (icon_tag_name.equals("DTH")) { 

      imageView.setImageResource(R.drawable.vdth_blue); 

     } else if (icon_tag_name.equals("Postpaid/Landline")) { 

      imageView.setImageResource(R.drawable.vpostpaid_blue); 

     } else if (icon_tag_name.equals("Money Transfer")) { 

      imageView.setImageResource(R.drawable.vmoney_blue); 

     } else if (icon_tag_name.equals("Electricity")) { 

      imageView.setImageResource(R.drawable.velectricity_blue); 

     } else if (icon_tag_name.equals("Bus Ticket")) { 

      imageView.setImageResource(R.drawable.vbus_blue); 

     } else if (icon_tag_name.equals("Entertainment")) { 

      imageView.setImageResource(R.drawable.ventertainment_blue); 

     } else if (icon_tag_name.equals("GasBill")) { 

      imageView.setImageResource(R.drawable.vgas_blue); 

     } else if (icon_tag_name.equals("Waterbill")) { 

      imageView.setImageResource(R.drawable.vwater_blue); 

     } else if (icon_tag_name.equals("Lifeinsurence")) { 

      imageView.setImageResource(R.drawable.vinsurance_blue); 

     } else if (icon_tag_name.equals("GiftVoucher")) { 

      imageView.setImageResource(R.drawable.vgift_blue); 

     } else { 
      imageView.setImageResource(R.drawable.vflight_blue); 
     } 

    } else { 

     gridView = (View) convertView; 

    } 

    return gridView; 
} 

}

+0

遵循此鏈接:http://stackoverflow.com/questions/19292369/how-to-highlight-the-grid-查看項目選擇 – prakash 2015-02-10 10:54:05

回答

0
gridView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, 
       int position, long id) { 
      v.setBackgroundColor(Color.RED); 
     } 

    }); 
+0

對不起,它不是在我的情況下工作我需要爲我的網格選擇效果我不需要更改項目顏色,並感謝您的答案 – Kumar 2015-02-10 10:54:16

+0

什麼樣的影響? – GIGAMOLE 2015-02-10 10:55:04

0

做一個活動XML佈局grid_color_selector.xml並在自定義的GridView佈局Android上使用此佈局:background =「@ drawable/grid_color_selector」

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 

     <item android:drawable="@android:color/holo_blue_light" android:state_pressed="true"/> 
     <item android:drawable="@android:color/holo_blue_light" android:state_selected="true"/> 
     <item android:drawable="@color/white"/> 

    </selector> 
2

您可以將選擇器定義爲drawable並將其添加爲網格項的背景。

首先,創建一個包含你選擇一個新的可繪製資源文件:

<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:state_pressed="false" android:state_focused="true" android:color="@color/your_pressed_color" /> 
    <item android:state_pressed="true" android:color="@color/your_pressed_color" /> 
    <item android:color="@color/your_default_color" /> 
</selector> 

之後,這個選擇添加到您的網格項:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/your_selector" 
    android:orientation="vertical"> 

    <!--The grid item layout elements--> 

</LinearLayout> 
0

使用此

/res/drawable/grid_view_item.xml

<item android:state_pressed="true" > 
    <shape> 
     <solid android:color="@color/grid_item_pressed"/> 
    </shape> 
</item> 

/res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="grid_item_pressed">#ffaa66</color> 
</resources> 

現在把android:background="@drawable/grid_view_item"屬性,在xml含網格視圖