Add the header layout and the footer layout for ListView.
Add header layout and footer layout in ListView
I posted a Demo when I was learning about the Himalayas for your reference;
If our current page has multiple interfaces and multiple la s, our general choice is nothing more than 1. Multiple la s; 2. ScrollView (good low) for various complex sliding la S ); 3. Head layout. Sometimes we cannot implement it well with multiple la s, so the head layout is our best choice. After learning ListView, the principle is very simple and there is nothing to understand, directly paste the Code:
:
Text layout:
Fragment_classify.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"> <ListView android:id="@+id/teach_classify_listview" android:layout_width="match_parent" android:layout_height="match_parent" android:listSelector="#00000000"/></LinearLayout>
Classify_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" android:orientation="vertical"> <View android:id="@+id/teach_classify_item_divider" android:background="#f3fdeeee" android:layout_width="match_parent" android:layout_height="10dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <RelativeLayout android:id="@+id/teach_classify_left" android:layout_width="0dp" android:background="@drawable/item_pressed" android:layout_height="match_parent" android:layout_marginLeft="15dp" android:layout_weight="1"> <ImageView android:id="@+id/teach_classify_item_iamge01" android:layout_width="30dp" android:layout_height="30dp" android:layout_centerVertical="true" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/teach_classify_item_text01" android:layout_width="match_parent" android:layout_height="45dp" android:layout_centerVertical="true" android:layout_marginLeft="60dp" android:gravity="center_vertical" android:text="@string/app_name" /> </RelativeLayout> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#efe6e6" /> <RelativeLayout android:id="@+id/teach_classify_right" android:layout_width="0dp" android:background="@drawable/item_pressed" android:layout_height="match_parent" android:layout_marginLeft="15dp" android:layout_weight="1"> <ImageView android:id="@+id/teach_classify_item_iamge02" android:layout_width="30dp" android:layout_height="30dp" android:layout_centerVertical="true" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/teach_classify_item_text02" android:layout_width="match_parent" android:layout_height="45dp" android:layout_centerVertical="true" android:layout_marginLeft="60dp" android:gravity="center_vertical" android:text="@string/app_name" /> </RelativeLayout> </LinearLayout></LinearLayout>
Header layout:
Fragment_classify_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"> <ImageView android:id="@+id/teach_classify_lv_header" android:src="@mipmap/ic_launcher" android:layout_width="match_parent" android:layout_height="180dp" /></LinearLayout>
Foot layout:
Fragment_classify_bottom.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"> <ImageView android:id="@+id/teach_classify_bottom" android:src="@mipmap/ic_launcher" android:layout_width="match_parent" android:layout_height="160dp" /></LinearLayout>
Home page:
Public class ClassifyFragment extends BaseFragment implements ClassifyAdapter. onClickItemListener {public static final String TAG = ClassifyFragment. class. getSimpleName (); private ListView mListView; private ClassifyAdapter adapter; private ImageView mHeaderImage; private ImageView metadata; @ Nullable @ Override public View onCreateView (LayoutInflater inflater, @ Nullable ViewGroup container, @ Nu Llable Bundle savedInstanceState) {layout = inflater. inflate (R. layout. fragment_classify, container, false); return layout ;}@ Override public void onActivityCreated (@ Nullable Bundle savedInstanceState) {super. onActivityCreated (savedInstanceState); initView (); setupView ();}/*** it is best to add the Header before setAdapter, before Android4.4, the Header must be placed before the Adapter is set */private void initView () {mListView = (ListView) Layout. findViewById (R. id. teach_classify_listview); // header View headerView = LayoutInflater. from (getActivity ()). inflate (R. layout. fragment_classify_header, null); mHeaderImage = (ImageView) headerView. findViewById (R. id. teach_classify_lv_header); // you can add multiple HeaderView mListView. addHeaderView (headerView); // bottom View bottomView = LayoutInflater. from (getActivity ()). inflate (R. layout. fragment_classify _ Bottom, null); mBottomImage = (ImageView) bottomView. findViewById (R. id. teach_classify_bottom); mListView. addFooterView (bottomView); adapter = new ClassifyAdapter (getActivity (), null); mListView. setAdapter (adapter);}/*** network Request */private void setupView () {HttpUtil. getStringAsync (HttpConstant. CLASSIFY_URL, new HttpUtil. requestCallBack () {@ Override public void onFailure () {Log. e (TAG, "onFailur E: ") ;}@ Override public void onSuccess (String result) {Log. e (TAG, "onSuccess:" + result); Gson gson = new Gson (); ClassifyList classifyList = gson. fromJson (result, ClassifyList. class); List <Classify> list = classifyList. getList (); // update the adapter. updateRes (list); // update the Header ImageLoader. display (mHeaderImage, list. get (0 ). getCoverPath () ;}@ Override public void onFinish () {Log. e (TAG, "onFinish: ") ;}}); String URL_BOTTOM =" http://adse.ximalaya.com/ting? Device = android & name = cata_index_banner & network = wifi & operator = 0 & version = 4.3.98 "; HttpUtil. getStringAsync (URL_BOTTOM, new HttpUtil. requestCallBack () {@ Override public void onFailure () {}@ Override public void onSuccess (String result) {Gson gson = new Gson (); ClassifyBottomList classifyBottomList = gson. fromJson (result, ClassifyBottomList. class); ImageLoader. display (mBottomImage, classifyBottomList. getData (). get (0 ). getCover () ;}@ Override public void onFinish () {}}) ;}@ Override public void onOnclickItem (int position) {Log. e (TAG, "onclickitem: -------------" + position );}}
Adapter:
Public class ClassifyAdapter extends BaseAdapter implements View. onClickListener {private static final String TAG = ClassifyAdapter. class. getSimpleName (); private List <Classify> data; private LayoutInflater inflater; private OnClickItemListener listener; // hold interface public void setListener (OnClickItemListener listener) {this. listener = listener;} public ClassifyAdapter (Context context, List <Classify> data) {Inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); if (data! = Null) {this. data = data;} else {this. data = new ArrayList <> () ;}} public void updateRes (List <Classify> data) {if (data! = Null) {this. data. clear (); this. data. addAll (data); notifyDataSetChanged () ;}@ Override public int getCount () {int count = 0; if (data! = Null) {count = (data. size ()-1)/2;} return count;} @ Override public Classify getItem (int position) {return data. get (position) ;}@ Override public long getItemId (int position) {return position ;}@ Override public View getView (int position, View convertView, ViewGroup parent) {ViewHolder holder = null; if (convertView = null) {convertView = inflater. inflate (R. layout. classify_item, parent, false); holde R = new ViewHolder (); holder. itemIamge01 = (ImageView) convertView. findViewById (R. id. teach_classify_item_iamge01); holder. itemImage02 = (ImageView) convertView. findViewById (R. id. teach_classify_item_iamge02); holder. itemText01 = (TextView) convertView. findViewById (R. id. teach_classify_item_text01); holder. itemText02 = (TextView) convertView. findViewById (R. id. teach_classify_item_text02); holder. topDivider = ConvertView. findViewById (R. id. teach_classify_item_divider); holder. leftItem = convertView. findViewById (R. id. teach_classify_left); holder. rightItem = convertView. findViewById (R. id. teach_classify_right); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag () ;}// determine whether to display the split line if (position % 3 = 0 & position! = 0) {holder. topDivider. setVisibility (View. VISIBLE);} else {holder. topDivider. setVisibility (View. GONE);} // load the data holder. itemText01.setText (data. get (position * 2 + 1 ). getTitle (); holder. itemText02.setText (data. get (position * 2 + 2 ). getTitle (); // sets the listener holder. leftItem. setOnClickListener (this); holder. rightItem. setOnClickListener (this); // sets the flag holder. leftItem. setTag (position * 2 + 1); holder. rightItem. setTag (po Sition * 2 + 2); // load the image ImageLoader. display (holder. itemIamge01, data. get (position * 2 + 1 ). getCoverPath (); ImageLoader. display (holder. itemImage02, data. get (position * 2 + 2 ). getCoverPath (); return convertView;} @ Override public void onClick (View v) {Integer position = (Integer) v. getTag (); Log. e (TAG, "onClick:" + position); if (listener! = Null) {listener. onOnclickItem (position) ;}} private static class ViewHolder {// ImageView itemIamge01 on the left; // ImageView itemImage02 on the right; // TextView itemText01 on the right; TextView itemText02 on the right; // split line View topDivider; // left and right Layout View leftItem, rightItem;} public interface OnClickItemListener {void onclickitem (int position );}}
Model class:
public class Classify { private String title; private String coverPath; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getCoverPath() { return coverPath; } public void setCoverPath(String coverPath) { this.coverPath = coverPath; }}
public class ClassifyList { private List<Classify> list; public List<Classify> getList() { return list; } public void setList(List<Classify> list) { this.list = list; }}
public class ClassifyBottom { private String cover; public String getCover() { return cover; } public void setCover(String cover) { this.cover = cover; }}
public class ClassifyBottomList { private List<ClassifyBottom> data; public List<ClassifyBottom> getData() { return data; } public void setData(List<ClassifyBottom> data) { this.data = data; }}