Android listview 的应用
生活随笔
收集整理的這篇文章主要介紹了
Android listview 的应用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ListView作為Android最常用但是卻最難用的控件之一,有很多神奇的用法.我之前也有寫過一個例子,稍微不那么簡單了一點.
[Android原生item的伸縮效果]:http://www.cnblogs.com/stareblankly/p/4958062.html
- 簡單的ListView的應用.
- 定制ListView界面
1.我們新建一個furit_item.xml作為我們自定義listview的item.
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;}@Overridepublic 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的點擊事件
轉載于:https://www.cnblogs.com/stareblankly/p/5057340.html
總結
以上是生活随笔為你收集整理的Android listview 的应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mybatis返回Mysql表的自增主键
- 下一篇: tomcat(2)--集群