2012-10-30 13 views
13

Czy istnieje znacznik XML, którego można użyć w pliku układu, który jest równoważny z ListView.addHeaderView()?Czy istnieje znacznik XML, który jest odpowiednikiem `ListView.addHeaderView '?

+0

myślę, to nie istnieją w oryginalnym widoku listy. Jednym z możliwych rozwiązań jest rozszerzenie 'ListView' i utworzenie niestandardowego atrybutu do ustawienia widoku nagłówka. –

+0

@Iul Czy mogę utworzyć niestandardowe atrybuty, które można wykorzystać w pliku układu XML? Będę musiał zajrzeć do tego dalej ... –

+1

[** tutaj **] (http://kevindion.com/2011/01/custom-xml-attributes-for-androidwidgets/) kumpel –

Odpowiedz

18

Napisałem prosty ListView jak twoje wymaganie.

  1. Declare niestandardowy atrybut w attrs.xml w value folderu:

    <resources> 
        <declare-styleable name="HeaderListViewFromXML"> 
         <attr name="headerView" format="reference"/> 
        </declare-styleable> 
    </resources> 
    
  2. Tworzenie HeaderListViewFromXML klasa rozszerzony ListView

    public class HeaderListViewFromXML extends ListView { 
        private int headerId; 
    
        public HeaderListViewFromXML(Context context) { 
         this(context, null); 
        } 
    
        public HeaderListViewFromXML(Context context, AttributeSet attrs) { 
         this(context, attrs, 0); 
        } 
    
        public HeaderListViewFromXML(Context context, AttributeSet attrs, int defStyle) { 
         super(context, attrs, defStyle); 
         TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.HeaderListViewFromXML, defStyle, defStyle); 
    
         try { 
          headerId = a.getResourceId(R.styleable.HeaderListViewFromXML_headerView, View.NO_ID); 
          if (headerId != View.NO_ID) { 
           LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
           View header = inflater.inflate(headerId, null); 
           addHeaderView(header); 
          } 
         } finally { 
          a.recycle(); 
         } 
        } 
    } 
    
  3. stwierdzenie zwyczaj HeaderListViewFromXML w layout.xml

    <RelativeLayout 
         xmlns:android="http://schemas.android.com/apk/res/android" 
         xmlns:app="http://schemas.android.com/apk/res-auto" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent"> 
        <com.example.rewidget.HeaderListViewFromXML 
          android:id="@+id/listWithHeader" 
          android:layout_width="fill_parent" 
          android:layout_height="150dp" 
          android:layout_marginTop="60dp" 
          android:background="#00FF00" 
          // custom attribute. Point to layout in header1.xml 
          app:headerView="@layout/header1" /> 
    </RelativeLayout> 
    
  4. W działalności, jak używać normalnego ListView

    public class MainActivity extends Activity { 
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState); 
         setContentView(R.layout.activity_main); 
         ListView list = (ListView) findViewById(R.id.listWithHeader); 
    
         String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" }; 
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values); 
    
         list.setAdapter(adapter); 
        } 
    } 
    
+0

Nifty! Dzięki za post. –

Powiązane problemy