Friday, April 24, 2020

How to add a header in a ListView in Android

Introduce ListView

in this section, we will continue to study the UI controls that have not been finished. Looking back, we introduced the concept of the Adapter, and then learned the use of the three simplest adapters:
ArrayAdapter, SimpleAdapter, and SimpleCursorAdapter. Explained is the first UI control that needs to be used with Adapter: ListView, but in the version was replaced by the new control RecyclerView!
As one of the most commonly used controls, lists still need to be studied well. This section learns ListView, the properties of ListView, and the simple definition of BaseAdapter from the perspective of a beginner. As for ListView to optimize these, we come step by step
activity_main.xml
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#b6cfb6"
    >
    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:isScrollContainer="false"
        android:background="#ff4c5f"
        />
</RelativeLayout>
listview_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="List of fruits."
        android:background="#ffe238"
        android:padding="10dp"
        />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Get reference of widgets from XML layout
        final ListView lv = (ListView) findViewById(R.id.lv);
        // Initializing a new String Array
        String[] fruits = new String[] {
                "Abiu",
                "Batuan",
                "Black Mulberry",
                "Cape Gooseberry",
                "Desert banana",
                "Eastern May Hawthorn",
                "Fibrous Satinash",
                "Gooseberry",
                "Hairless rambutan",
                "Illawarra Plum",
                "Jelly Palm"
        };
        // Create a List from String Array elements
        final List<String> fruits_list = new ArrayList<String>(Arrays.asList(fruits));
        // Create an ArrayAdapter from List
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
                (this, android.R.layout.simple_list_item_1, fruits_list);
        // Add a header to the ListView
        LayoutInflater inflater = getLayoutInflater();
        ViewGroup header = (ViewGroup)inflater.inflate(R.layout.listview_header,lv,false);
        lv.addHeaderView(header);
        // DataBind ListView with items from ArrayAdapter
        lv.setAdapter(arrayAdapter);
    }
}
Result:
How to add a header in a ListView in Android

No comments:

Post a Comment