本人博客地址:
转载请标明原址:
老规矩 先上效果图
给listview 添加头部和尾部是因为项目中要用到所以在网上查了查
其实很简单 只要在 setAdatper()这个方法之前 执行
listview.addHeaderView(titleView);listview.addFooterView(footView);
这两个方法便可
好了废话不多说上代码:
MainActivity.java
import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.LayoutInflater;import android.widget.LinearLayout;import android.widget.ListView;public class MainActivity extends AppCompatActivity { private LayoutInflater inflater; private LinearLayout titleView; private LinearLayout bodyView; private LinearLayout footView; private ListView listview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) findViewById(R.id.lv); inflater = LayoutInflater.from(this); titleView = (LinearLayout) inflater.inflate(R.layout.item_title, null);//得到头部的布局 footView = (LinearLayout) inflater.inflate(R.layout.item_foot, null);//得到尾部的布局 listview.addHeaderView(titleView);//添加头部 listview.addFooterView(footView);//添加尾部 MainAdapter mainAdapter = new MainAdapter(this, 5); listview.setAdapter(mainAdapter); }}
MainAdapter.java
import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;/** * Created by admin on 2016/8/21. */public class MainAdapter extends BaseAdapter { private Context context; private LayoutInflater inflater; private int anInt; public MainAdapter(Context context, int anInt) { this.anInt = anInt; inflater = LayoutInflater.from(context); } @Override public int getCount() { return anInt; } @Override public Object getItem(int i) { return i; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder viewHolder; if (view == null) { viewHolder = new ViewHolder(); view = inflater.inflate(R.layout.item_body, null); viewHolder.item_body = (TextView) view.findViewById(R.id.tv_body); } return view; } class ViewHolder { TextView item_body; }}
布局什么的我就不网上贴了,源码中有
源码地址: