Sunday, 20 November 2016

Android ListView में header footer Add करना

Android ListView में  header footer Add करना

अक्सर हमें list view में कुछ data उसके top पर show करना होता है . इसके लिए 

हमारे पास header  option उपलब्ध है जिसे बहुत ही आसानी से apply किया जा

सकता है इसके लिए हमें निम्न कदम उठाने होंगे

Android ListView में  header Add करना
Step 1: अपनी आवश्यकता के हिसाब से एक XML फाइल बनाबी होगी जिसे header.xml से save करेंगे.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#554455"
    android:textColor="#ffffff"
    android:gravity="center_horizontal"
    android:text="Faculty Personal Information" />

</LinearLayout>

अब इस व्यू को हम inflate  करेंगे और अपने listview वाले code के साथ add करेंगे.
list=(ListView)findViewById(R.id.facrofilelistView1);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header, list, false);
listView.addHeaderView(header, null, false);
ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.facprofile,fval1);
              list.setAdapter(adapter);

Android ListView में  Footer  Add करना
Step 1: अपनी आवश्यकता के हिसाब से एक XML फाइल बनाबी होगी जिसे footer.xml से save करेंगे.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#554455"
    android:textColor="#ffffff"
    android:gravity="center_horizontal"
    android:text="Thanks " />

</LinearLayout>

अब इस व्यू को हम inflate  करेंगे और अपने listview वाले code के साथ add करेंगे.

list=(ListView)findViewById(R.id.facrofilelistView1);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header, list,false);
ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer, list,false);
listView.addHeaderView(header, null, false);
ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.facprofile,fval1);
list.setAdapter(adapter);



Complete Code

public class MainActivity extends Activity {
    ListView listView;

    static final String[] numbers = new String[] { "one", "two", "three",
            "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
            "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
            "seventeen", "eighteen", "nineteen", "twenty", "twenty one",
            "twenty two" };
    View header;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list=(ListView)findViewById(R.id.facrofilelistView1);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header, list,
         false);
ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer, list,
                false);
listView.addHeaderView(header, null, false);
ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.facprofile,fval1);
              list.setAdapter(adapter);

    }

}

No comments:

Post a Comment