Android listview 的应用
ListView作为Android最常用但是却最难用的控件之一,有很多神奇的用法.我之前也有写过一个例子,稍微不那么简单了一点.
[Android原生item的伸缩效果]:http://www.cnblogs.com/stareblankly/p/4958062.html
- 简单的ListView的应用.
private Integer[] data={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
ArrayAdapter<Integer> adapter=new ArrayAdapter<Integer>(MainActivity.this, android.R.layout.simple_list_item_1,data);
((ListView)findViewById(R.id.lv)).setAdapter(adapter);
}
- 定制ListView界面
1.我们新建一个furit_item.xml作为我们自定义listview的item.
<?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" >
<ImageView
android:id="@+id/furit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp" />
</LinearLayout>
2.然后我们新建一个Furit.class作为数据适配的实体类.
public class Fruit {
private String name;
private int imageId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public Fruit(String name, int imageId) {
super();
this.name = name;
this.imageId = imageId;
}
}
3.新建一个FruitAdapter.class适配器用来适配listview.
public class FruitAdapter extends ArrayAdapter<Fruit>{
private int resourceid;
public FruitAdapter(Context context, int textViewResourceId,
List<Fruit> objects) {
super(context,textViewResourceId, objects);
resourceid=textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit=getItem(position);
View view=LayoutInflater.from(getContext()).inflate(resourceid, null);
TextView tv=(TextView) view.findViewById(R.id.fruit_name);
ImageView iv=(ImageView) view.findViewById(R.id.furit_image);
iv.setImageResource(fruit.getImageId());
tv.setText(fruit.getName());
return view;
}
}
4.然后初始化我们要显示的数据.
private List<Fruit> fruitList=new ArrayList<Fruit>();
Fruit fruit=new Fruit("1", R.drawable.ic_launcher);
fruitList.add(fruit);
fruit=new Fruit("2", R.drawable.ic_launcher);
fruitList.add(fruit);
fruit=new Fruit("3", R.drawable.ic_launcher);
fruitList.add(fruit);
......
5.最后就只剩下使用了.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
FruitAdapter adapter=new FruitAdapter(this, R.layout.fruit_item, fruitList);
((ListView)findViewById(R.id.lv)).setAdapter(adapter);
}
- ListView的item的点击事件
((ListView)findViewById(R.id.lv)).setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Fruit fruit=fruitList.get(position);
Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();
}
});
stareblankly.cn
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步