2013-07-19 7 views
12

क्या एंड्रॉइड विचारों में सीएसएस क्लास चयनकर्ताओं के बराबर कुछ है? आरआईड की तरह कुछ लेकिन कई विचारों के लिए प्रयोग योग्य? मैं लेआउट पेड़ में अपनी स्थिति से स्वतंत्र विचारों के कुछ समूह को छिपाना चाहता हूं।एंड्रॉइड विचारों के लिए सीएसएस क्लास चयनकर्ताओं के बराबर?

उत्तर

4

मुझे लगता है कि आपको अपने लेआउट में सभी विचारों के माध्यम से पुन: प्रयास करना होगा, एंड्रॉइड की तलाश में: आईडी जिसे आप चाहते हैं। फिर दृश्यता को बदलने के लिए आप दृश्य सेट दृश्यता() का उपयोग कर सकते हैं। आप उन दृश्यों को चिह्नित करने के लिए एंड्रॉइड: आईडी के बजाय व्यू सेटट()/getTag() का उपयोग भी कर सकते हैं, जिन्हें आप संभालना चाहते हैं। उदाहरण के लिए, निम्नलिखित कोड एक सामान्य प्रयोजन विधि का उपयोग करता लेआउट पार करने के लिए:

// Get the top view in the layout. 
final View root = getWindow().getDecorView().findViewById(android.R.id.content); 

// Create a "view handler" that will hide a given view. 
final ViewHandler setViewGone = new ViewHandler() { 
    public void process(View v) { 
     // Log.d("ViewHandler.process", v.getClass().toString()); 
     v.setVisibility(View.GONE); 
    } 
}; 

// Hide any view in the layout whose Id equals R.id.textView1. 
findViewsById(root, R.id.textView1, setViewGone); 


/** 
* Simple "view handler" interface that we can pass into a Java method. 
*/ 
public interface ViewHandler { 
    public void process(View v); 
} 

/** 
* Recursively descends the layout hierarchy starting at the specified view. The viewHandler's 
* process() method is invoked on any view that matches the specified Id. 
*/ 
public static void findViewsById(View v, int id, ViewHandler viewHandler) { 
    if (v.getId() == id) { 
     viewHandler.process(v); 
    } 
    if (v instanceof ViewGroup) { 
     final ViewGroup vg = (ViewGroup) v; 
     for (int i = 0; i < vg.getChildCount(); i++) { 
      findViewsById(vg.getChildAt(i), id, viewHandler); 
     } 
    } 
} 
3

आप इस तरह के सभी दृश्यों के लिए एक ही टैग सेट कर सकते हैं और फिर आप सब इस तरह एक साधारण समारोह के साथ कि टैग होने दृश्य प्राप्त कर सकते हैं:

private static ArrayList<View> getViewsByTag(ViewGroup root, String tag){ 
    ArrayList<View> views = new ArrayList<View>(); 
    final int childCount = root.getChildCount(); 
    for (int i = 0; i < childCount; i++) { 
     final View child = root.getChildAt(i); 
     if (child instanceof ViewGroup) { 
      views.addAll(getViewsByTag((ViewGroup) child, tag)); 
     } 

     final Object tagObj = child.getTag(); 
     if (tagObj != null && tagObj.equals(tag)) { 
      views.add(child); 
     } 

    } 
    return views; 
} 

जैसा कि श्लोमी श्वार्टज़ answer में बताया गया है। स्पष्ट रूप से यह सीएसएस कक्षाओं के रूप में उपयोगी नहीं है। लेकिन यह आपके विचारों को बार-बार फिर से शुरू करने के लिए लेखन कोड की तुलना में थोड़ा उपयोगी हो सकता है।

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