Android ListView में कस्टम फुटर व्यू जोड़ना
ListView के अंत में एक फुटर व्यू (जैसे "सूची समाप्त" संदेश) जोड़ने का तरीका सीखें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project से प्रोजेक्ट बनाएं।
चरण 2: मेन लेआउट में ListView
res/layout/activity_main.xml:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
चरण 3: फुटर लेआउट फ़ाइल बनाएं
res/layout/listview_footer.xml (नई फ़ाइल):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Footer: End of the ListView!"
android:textAppearance="?android:attr/textAppearanceLarge"
android:padding="16sp" />
</LinearLayout>
चरण 4: MainActivity.java में फुटर इनफ्लेट करें
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView listView;
String[] country = {"India", "Australia", "South Africa", "North America", "South America", "Iceland", "GreenLand"};
List<String> listString;
LayoutInflater layoutInflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
listString = new ArrayList<>(Arrays.asList(country));
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listString);
layoutInflater = getLayoutInflater();
ViewGroup footer = (ViewGroup) layoutInflater.inflate(R.layout.listview_footer, listView, false);
listView.addFooterView(footer);
listView.setAdapter(adapter);
}
}
चरण 5: AndroidManifest.xml
मानक मैनिफेस्ट कॉन्फ़िगरेशन।
ऐप चलाएं
डिवाइस पर चलाएं। सूची के अंत में "Footer: End of the ListView!" दिखाई देगा।