Android Studio : ListView With Header And Footer
(有頁首與頁尾的ListView)
有頁首與頁尾的ListView,看起來比較完整,只要使用addHeaderView與addFooterView,就可以達到這個效果,來實作吧!
相關資訊圖片:
![[程式] Android Studio : ListView With Header And Footer ListView With Header And Footer View1 (activity_main.xml)畫面](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi78WKiFlqwjMXMjq6Ci2hx8w-D8azgWFxvz-hp_Kjo-KfDm9LDUx8jwtMnrZCVaWnCMgAlQZ73guqRdRJhHokRDBOU4Pc3COl_-4r5vERgsGh4ah0g7p1-n1XayqEjU0M89VMiW_Yx4YzB/s640/ListViewWithHeaderAndFooter_01.jpg) |
ListView With Header And Footer View1 (activity_main.xml)畫面 |
![[程式] Android Studio : ListView With Header And Footer ListView With Header And Footer View2 (list_item.xml)畫面](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUzJB2omsG-1RJrTzINQN64OPoXQ41LFIxGnB__nExraRVlvhjjqsZNYEBaZqWfgmM3WYi5uhL4zjH3QS0Ny6ICAkWkNSclEvkcodSFOtQXCrnYV5jcLutnaHZUl-WAlhig9jgBHh9tETC/s640/ListViewWithHeaderAndFooter_02.jpg) |
ListView With Header And Footer View2 (list_item.xml)畫面 |
![[程式] Android Studio : ListView With Header And Footer ListView With Header And Footer View3 (list_header.xml)畫面](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJiAnux3YkBL7Pidzg9Ph-VM_tVFcF6i5ditxzmgCWfY7hMILaxFr9oN21TQ0X22kCVu6BxicteVQ5XZF7Ix78U6VMx_nTK7KcNGNuJ3RBBct5GJfOqNRjaa5gN42Kxz-X-KEbQjhUekuk/s640/ListViewWithHeaderAndFooter_03.jpg) |
ListView With Header And Footer View3 (list_header.xml)畫面 |
![[程式] Android Studio : ListView With Header And Footer ListView With Header And Footer View4 (list_header1.xml)畫面](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDxDeRWyG0KgzkLXVIDfBgEickD5cXa49-eznMPy66iW17lBfpgbRyo2KDYDjWJ8jCmQcTTfbGEIVP2XJuQclAmpbs2tQVGVrb288E7Qsgu3f2_OlmHheE05msDSggyhgP5ScUjZvZoPG8/s640/ListViewWithHeaderAndFooter_04.jpg) |
ListView With Header And Footer View4 (list_header1.xml)畫面 |
![[程式] Android Studio : ListView With Header And Footer ListView With Header And Footer View5 (list_footer.xml)畫面](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1E5yK8r8rvgGZUC_HgXpP7halUA6VJxLZGIld6CqaMTldCHZhTb_WFm4ClwMB_PO_HFo_-Gn_JWHCvtygUpoI93bd9QLlBmHQT1yqMdGULG4o2AwWZTtAhxhcAR9vQ6PUMMc2vo4ntYuM/s640/ListViewWithHeaderAndFooter_05.jpg) |
ListView With Header And Footer View5 (list_footer.xml)畫面 |
![[程式] Android Studio : ListView With Header And Footer 初始畫面](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZbbjrw6qXe14Jmrr1jMpibvO5s2dskS1BTlML34YRmpjF0dgWgLwEI4nir3L9iLWZrdi24CuQ6qeFjzcuuMWqijExRKzuR0YZEXdzUOSc_HEQREueWdtU-No0W8a85i_i3V22q9F4uyQP/s400/ListViewWithHeaderAndFooter_06.jpg) |
初始畫面 |
![[程式] Android Studio : ListView With Header And Footer 點擊畫面(1個Header)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj8qgrbaJTqsJhwAzO_ye5Nd7FmiRplCKYABc4xuw3_KidNsBYRGw1rhc4m6yCgmKRGl8xlCby6haU6qfE4S8jRDQ2NRS9p1Zds0xw89uNKEWRnbM4RuCPrX5LTQ9LGjfRa0ZKkYOaKxFOl/s400/ListViewWithHeaderAndFooter_07.jpg) |
點擊畫面(1個Header) |
![[程式] Android Studio : ListView With Header And Footer 點擊畫面(2個Header)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFIimAnXat0vRMvt9_psjpbm9rVA2B7lsrTEsTQVrzTlBmBlyMwevWFS6seyeDz9IRW_rb5E-pHds_B-oiYANB_D70hoogg9BG1aPs8n0_ugWQ7scBW4JztJ_5veCjeKIIjbGSPsQmt-Sg/s400/ListViewWithHeaderAndFooter_08.jpg) |
點擊畫面(2個Header) |
Source Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
list_item.xml
<?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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="24dp"
android:text="TextView" />
</LinearLayout>
list_header.xml
<?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">
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:background="#666666"
android:text="Header" />
</LinearLayout>
list_header1.xml
<?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">
<TextView
android:id="@+id/header1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:background="#999999"
android:text="Header" />
</LinearLayout>
list_footer.xml
<?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">
<TextView
android:id="@+id/footer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp"
android:background="#666666"
android:text="Footer" />
</LinearLayout>
MainActivity.java
package tw.idv.wenyen.listviewwithheaderandfooter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ListView listView;
View header, header1, footer;
private static final String TAG = "MainActivity";
static String[] lessons = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
header = (View)getLayoutInflater().inflate(R.layout.list_header, null);
header1 = (View)getLayoutInflater().inflate(R.layout.list_header1, null);
footer = (View)getLayoutInflater().inflate(R.layout.list_footer, null);
// Header Footer必須在setAdapter()之前加入,否則會產生異常錯誤
listView.addHeaderView(header, null, false);
// listView.addHeaderView(header, null, false);
// listView.addHeaderView(header1, null, false);
listView.addFooterView(footer, null, false);
listView.setAdapter(new BaseAdapter() {
@Override
public int getCount() {
return lessons.length;
}
@Override
public Object getItem(int position) {
return lessons[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.list_item, null);
TextView textView = view.findViewById(R.id.textView);
textView.setText(lessons[position]);
return view;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
Log.d(TAG, "ItemPosition=" + position + " ,Header Count=" + listView.getHeaderViewsCount());
// lessons[position] 跟 listView.getAdapter().getItem(position).toString() 的 position不同
// 若要使用 lessons[position] 須扣掉 listView.getHeaderViewsCount(),才能得到真正的值
// Toast.makeText(MainActivity.this, lessons[position], Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, listView.getAdapter().getItem(position).toString()
+ " Position=" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
實作心得:
比較要注意的部分有兩個,一個部份是addHeaderView與addFooterView必須在setAdapter之前加入,一個部份是setAdapter與setOnClickListener的position值不一樣,因為Header也是一個View,由圖片 點擊畫面(1個Header) 與點擊畫面(2個Header) 中的position值就可以知道。
參考資料:
0 意見:
張貼留言