2012-10-17 4 views
8

मैं सूची तत्वों के बीच विभिन्न डिवाइडरों के साथ एक सूची चाहता हूं। मैं ऊंचाई 1 के साथ एक सफेद विभक्त परिभाषित करने के लिए इस कोड का इस्तेमाल किया है:सूची दृश्य में प्रत्येक तत्व को अलग विभक्त रंग सेट करना

_listView.setDivider(new ColorDrawable(Color.WHITE)); 
_listView.setDividerHeight(1); 

हालांकि यह सब तत्व के लिए विभक्त सेट सफेद होने का, और मैं केवल उनमें से कुछ चाहते हैं सफेद और विभिन्न अन्य होने के लिए रंग।

मैं यह कैसे कर सकता हूं?

+1

आप वैकल्पिक रंग की कोशिश कर रहे हैं, या परिभाषित पर अलग-अलग रंगों विभाजक विभिन्न अंक? – nickhar

+0

मैं सामग्री के आधार पर विभिन्न तत्वों के लिए विभिन्न विभक्त रंग परिभाषित करने की कोशिश कर रहा हूं। जब मैं सूची बना रहा हूं, तो मैं इसे प्रोग्रामिक रूप से करने की कोशिश कर रहा हूं, लेकिन जब मैं अलग-अलग रंग को परिभाषित करता हूं तो यह सभी डिवाइडर को इस रंग में बदल देता है न केवल विशिष्ट तत्व के विभाजक को बदलना चाहता था। – MrBug

उत्तर

9

विभाजक को 0 तक ऊंचाई पर सेट करें और 1 की ऊंचाई के साथ अपने आइटम लेआउट में एक दृश्य को कार्यान्वित करें और प्रत्येक बार दृश्य निर्मित होने पर सूची आइटम के आधार पर अपना रंग बदलें।

यहाँ एक एक्सएमएल लेआउट नमूना है:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/text" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

    <View 
     android:id="@+id/divider" 
    android:layout_width="fill_parent" 
    android:layout_height="1dp" 
    android:background="#FFFFFFFF" /> 

</LinearLayout> 

और यह कैसे आप एडाप्टर में रंग बदल रहा है:

public class MyAdapter extends BaseAdapter { 
    /** List of items to show */ 
    private ArrayList<String> items = new ArrayList<String>(); 
    private Context context; 
    private int color[]; 

    public OffersAdapter(Context c, ArrayList<String> items, int color[]) 
    { 
     super(); 
     this.context = c; 
     this.items = items; 
     this.color = color; 
    } 

    public int getCount() { 
     return items.size(); 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

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

    if(null == convertView) 
    { 
     LayoutInflater inflater = LayoutInflater.from(context); 
     convertView = inflater.inflate(R.layout.list_item, parent, false); 

     viewHolder.text = (TextView) convertView.findViewById(R.id.text); 
     viewHolder.divider = (View) convertView.findViewById(R.id.divider); 

     convertView.setTag(viewHolder); 
    } else { 
     //Retrieve the current view 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 

    //This is where you chance the divider color based on the item 
    viewHolder.divider.setBackgroundColor(color[position]); 

    viewHolder.text.setText(items.get(position)); 

    return convertView; 
} 

//Holds the current view 
private static class ViewHolder { 
     public TextView text; 
     public View divider; 
    } 
} 

कहाँ int color[] रंग आप उपयोग करना चाहते की एक सूची है।

ViewHolder read here के बारे में अधिक जानकारी।

+0

मैंने कोशिश की है। लेकिन फिर तत्व पर दबाव डालने और आपको पकड़ने पर पृष्ठभूमि रंग सही स्थिति में नहीं दिखाई देगा, यह मेरे द्वारा बनाए गए दृश्य को कवर करेगा। – MrBug

+0

अपना कोड पोस्ट करें। क्या आपने ऊंचाई तय की? – slybloty

+0

हाँ मैंने किया ... _listView.setDivider (नया रंग हटाने योग्य (रंग। हाइट)); _listView.setDividerHeight (1); – MrBug

1
  1. अलग अपनी सूची आइटम और xml में अपनी पूरी सूची
  2. अपनी सूची आइटम के लिए एक कस्टम एडाप्टर बनाएँ और अपने सूची
  3. का दृष्टांत मापदंड आप प्रत्येक नए आइटम के लिए विभक्त रंग बदलने चाहते हैं पर निर्भर करता है यदि आप सूची में एडाप्टर से जोड़ने

उदाहरण:

listitem.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/sometext" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

    <View 
     android:id="@+id/customdivider" 
    android:layout_width="fill_parent" 
    android:layout_height="1dp" 
      /> 

</LinearLayout> 

list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    > 

    <ListView 
     android:id="@+id/totallynewlist" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     /> 

</LinearLayout> 

CustomAdapter.java

public class CustomAdapter extends BaseAdapter { 

    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private static LayoutInflater inflater = null; 

    public SubjectListAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data = d; 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 

    public int getCount() { 
     return data.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 



     public View getView(int position, View convertView, ViewGroup parent) { 
      View vi = convertView; 
      if (convertView == null) 
       vi = inflater.inflate(R.layout.listitem, null); 

     //Find the divider here and depending on your criteria change the colour - if required take data from main activity if you need it to change the colour 
     View divider= (View) vi.findViewById(R.id.customdivider); 
     if(your criteria) 
      divider.setBackgroundColor(R.color.white); //assuming white is defined in    colors 
     else 
     set to whatever color 
     return vi; 
     } 
} 

आपकी गतिविधि में

ListView list = (ListView) findViewById(R.id.totallynewlist); 

    // creating new HashMap 
    HashMap<String, String> map = new HashMap<String, String>(); 
    <//Pass whatever condition you want for your criteria here if you need to - optional> 
    ListPass.add(map); 

    adapter = new CustomAdapter(this, ListPass); 

    list.setAdapter(adapter); 
+0

लेकिन कौन सी विधि केवल विशिष्ट तत्व के लिए विभाजक को बदलती है? विधि सेटडिवाइडर सभी तत्वों के लिए विभक्त रंग बदलता है। – MrBug

+0

यह मूल रूप से सेट विभाजक का उपयोग नहीं करता है - यह 1 डीपी का दृश्य बनाता है, कि आप अपने मानदंडों के आधार पर प्रत्येक सूची आइटम के रंग को बदल सकते हैं। आइटम दृश्य विभक्त की पृष्ठभूमि को जो भी रंग आपको चाहिए – Slartibartfast

+0

हम अब सूची की प्रत्येक पंक्ति के लिए इसे बदल रहे हैं, इसलिए सूची विभाजक का उपयोग न करें - एक सूची विधि है और पूरी सूची – Slartibartfast

संबंधित मुद्दे