Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an XML tag that is equivalent to `ListView.addHeaderView'?

Is there an XML tag which I can use in a layout file that is equivalent to ListView.addHeaderView()?

like image 301
Code-Apprentice Avatar asked Oct 30 '12 23:10

Code-Apprentice


People also ask

What does setAdapter do?

A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.

How to display the data from XML file to listview?

This method has the ability to display the data from XML File to the ListView. This program is so simple and you can make it in no time. Please follow the instructions below to see how it works. Go to solution explorer and right click the file name. After that select “ Add ” and hit “ New Item ”.

How to add a header view to a listview?

There isn't an easy way like listview.addHeaderView () but you can achieve this by adding a type to your adapter for header.

How to add a listview to a form?

Add a ListView inside the Form just like shown below. Put the XML File in the resources folder by dragging it. Press F7 to open the code editor. In the code editor, create a method to get the data in the XML File and display it into the ListView . Write the following code to execute the method that you have created in the first load of the form.

How do I initialize a listviewitem with tag and text properties?

The following code example demonstrates how to initialize a ListViewItem and set the Tag and Text properties. To run this example, place the following code in a form that contains a ListView named ListView1, and call InitializeListViewItems from the form's constructor or Load event-handling method.


1 Answers

I wrote a simple ListView like your requirement.

  1. Declare custom attribute in attrs.xml in value folder:

    <resources>
        <declare-styleable name="HeaderListViewFromXML"> 
            <attr name="headerView" format="reference"/>
        </declare-styleable>
    </resources>
    
  2. Create HeaderListViewFromXML class extended 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. Declare custom HeaderListViewFromXML in 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. In Activity, use like normal 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);
        }
    }
    
like image 103
Trung Nguyen Avatar answered Oct 20 '22 04:10

Trung Nguyen