Add things in ListView head and tail

Directly on the code

 1 import android.support.v7.app.AppCompatActivity;
 2 import android.os.Bundle;
 3 import android.view.LayoutInflater;
 4 import android.view.View;
 5 import android.widget.ArrayAdapter;
 6 import android.widget.Button;
 7 import android.widget.ListView;
 8 
 9 import java.util.ArrayList;
10 import java.util.List;
11 
12 public class MainActivity extends AppCompatActivity {
13     private ListView lv;
14     private List<String>list;
15     private ArrayAdapter<String>adapter;
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20         //初始化控件
21         lv=(ListView)findViewById(R.id.lv);
22         //初始化数据
23         list=new ArrayList<String>();
24         for (int0 = I; I <20 is; I ++ ) {
 25              List.add ( "display content" + I);
 26 is          }
 27          // initialize adapter 
28          Adapter = new new the ArrayAdapter <String> ( the this , android.R.layout.simple_expandable_list_item_1 , List);
 29          // the xml file into View objects 
30          View image LayoutInflater.from = ( the this ) .inflate (R.layout.image_item, null );
 31 is          // put into ListView picture header 
32          lv.addHeaderView (Image);
 33 is          the Button BTN = new new the Button ( the this );
 34 is         btn.setText ( "Load more" );
 35          // put the buttons ListView tail 
36          lv.addFooterView (BTN);
 37 [          // set the adapter 
38 is          lv.setAdapter (Adapter);
 39      }
 40 }

Newly learned is to use two methods is addHeaderView addFooterView

There are two xml files

Main Events

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     tools:context="com.example.aaaaa.myapplication.MainActivity">
 7 
 8     <ListView
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:id="@+id/lv">
12 
13     </ListView>
14 </RelativeLayout>

Picture XML file

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="200dp">
 5     <ImageView
 6         android:layout_width="match_parent"
 7         android:layout_height="200dp"
 8         android:src="@drawable/jj"
 9         android:scaleType="fitXY"/>
10 </LinearLayout>

 

 

Guess you like

Origin www.cnblogs.com/z-cg/p/12329384.html