一、listview控件添加标题:
1、新建一个项目,ListViewDemo,MainActivity.java代码如下:
public class MainActivity extends Activity {
String[] nameList ={"banbana","apple","orange","Watermelon","Pear","Grape"};
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview =(ListView)findViewById(R.id.listview);//就算是在碎片中也是可以这样写
addString();
}
private void addString(){
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,nameList);
listview.setAdapter(adapter);
}
}
这里面对listiew添加的适配器是用系统的ArrayAdapter构造函数,
ArrayAdapter (Context context, int resource, T[] objects)
第一个参数是context,第二个就是用哪种显示方式,这里simple_list_item_1表示是单行显示,第三个就是object数组.
2、再看一下xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
3、补充:
如果想去掉listview的横线:
listview.setDividerHeight(0);//屏蔽掉listview的横线
listview.setDivider(null);
二、listview添加一张图片和文字
1、修改MainActivity.java代码如下:
你要先拷贝一些图片到res/mipmap下面,图片名字分别是
sliding_personal_info.png,
sliding_health_news.png,
sliding_run.png,
sliding_device_manage.png,
sliding_setting.png,
sliding_about.png
public class MainActivity extends Activity {
private ListView listview;
String [] lefttitles={"个人信息","健康周报","跑步","设备管理","设置","关于"};
int [] leftImageList={R.mipmap.sliding_personal_info,R.mipmap.sliding_health_news,R.mipmap.sliding_run,
R.mipmap.sliding_device_manage,R.mipmap.sliding_setting,R.mipmap.sliding_about};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview =(ListView)findViewById(R.id.listview);
addImage();
}
private void addImage(){
listview.setAdapter(new SlidingListviewAdapter(this,lefttitles,leftImageList));
}
这里listview使用的适配器是自定议的适配器:SlidingListciewAdapter,它的代码是:
public class SlidingListviewAdapter extends BaseAdapter {
View [] itemViews; //每一条就是一个视图
LayoutInflater la; //布局
public SlidingListviewAdapter(Context c, String [] itemTitles, int [] itemImageRes){
itemViews = new View[itemTitles.length];
la=LayoutInflater.from(c);
for (int i=0; i<itemViews.length; ++i){
itemViews[i] = makeItemView(itemTitles[i],itemImageRes[i]); //自定义函数。用文字和图片制作出一个视图
}
}
private View makeItemView(String strTitle, int resId) {
// 使用View的对象itemView与R.layout.item关联
View itemView = la.inflate(R.layout.left_main_listview_items, null);
// 通过findViewById()方法实例R.layout.item内各组件
TextView title = (TextView)itemView.findViewById(R.id.itemTitle);
title.setText(strTitle);
ImageView image = (ImageView)itemView.findViewById(R.id.itemImage);
image.setImageResource(resId);
return itemView;
}
@Override
public int getCount() {
return itemViews.length;
}
@Override
public Object getItem(int position) {
return itemViews[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
return itemViews[position];
return convertView;
}
}
getCount()方法(在此适配器中所代表的数据集中的条目数),
getItem(int position) (获取数据集中与指定索引对应的数据项),
getItemId(int position)(取在列表中与指定索引对应的行id),每个子项被滚动到屏幕内的时候会调用getView()方法,效果如下: