ListView的用法
Posted 837634902why
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ListView的用法相关的知识,希望对你有一定的参考价值。
一个可向下滑动的列表
main.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"
>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
main.java
其中使用可适配器
package com.example.listviewtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private String[] data={"梨","苹果","沙果","海棠","野樱莓","山楂","梨","杏","樱桃","桃","李子","梅子","草莓","菠萝","橘子","橙子","柠檬","金桔","哈密瓜","香瓜","香蕉"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data);
ListView listView=(ListView)findViewById(R.id.listView);
listView.setAdapter(adapter);
}
}
ListView添加图片和优化代码
1定义实体类Fruit
public class Fruit {
private String name;
private int imageId;
public Fruit(String name,int imageId){
this.name=name;
this.imageId=imageId;
}
public String getName(){
return name;
}
public int getImageId(){
return imageId;
}
}
2添加fruit_item.xml布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/fruit_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>
3定义适配器
package com.example.listviewtest;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by lenovo on 2019/1/25.
*/
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceid;
//用于储存控件的实例
class ViewHolder{
ImageView fruitImage;
TextView fruitName;
}
//textViewRecourceId为子布局id
public FruitAdapter(Context context, int textViewRecourceId, List<Fruit> objects){
super(context,textViewRecourceId,objects);
resourceid=textViewRecourceId;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit=getItem(position);
View view;
ViewHolder viewHolder;
if(convertView==null) {
view = LayoutInflater.from(getContext()).inflate(resourceid, parent, false);
viewHolder=new ViewHolder();
viewHolder.fruitImage=(ImageView)view.findViewById(R.id.fruit_image);
viewHolder.fruitName=(TextView)view.findViewById(R.id.fruit_name);
view.setTag(viewHolder);//将viewHolder放入到view中
}else
{
view=convertView;
viewHolder=(ViewHolder)view.getTag();//将viewHolder取出,此步骤避免了每次都要用find获取空间实例的问题
}
viewHolder.fruitImage.setImageResource(fruit.getImageId());
viewHolder.fruitName.setText(fruit.getName());
return view;
}
}
4main.java
package com.example.listviewtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Fruit> fruitList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();
FruitAdapter adapter=new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
ListView listView=(ListView)findViewById(R.id.listView);
listView.setAdapter(adapter);
}
private void initFruits(){
for(int i=0;i<2;i++) {
Fruit f1 = new Fruit("梨", R.drawable.p1);
fruitList.add(f1);
Fruit f2 = new Fruit("苹果", R.drawable.p2);
fruitList.add(f2);
Fruit f3 = new Fruit("沙果", R.drawable.p3);
fruitList.add(f3);
Fruit f4 = new Fruit("海棠", R.drawable.p4);
fruitList.add(f4);
Fruit f5 = new Fruit("野樱莓", R.drawable.p5);
fruitList.add(f5);
Fruit f6 = new Fruit("山楂", R.drawable.p6);
fruitList.add(f6);
Fruit f7 = new Fruit("杏", R.drawable.p7);
fruitList.add(f7);
Fruit f8 = new Fruit("樱桃", R.drawable.p8);
fruitList.add(f8);
Fruit f9 = new Fruit("李子", R.drawable.p9);
fruitList.add(f9);
Fruit f10 = new Fruit("草莓", R.drawable.p10);
fruitList.add(f10);
}
}
}
添加点击事件
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();
FruitAdapter adapter=new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
ListView listView=(ListView)findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.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();
}
});
}
以上是关于ListView的用法的主要内容,如果未能解决你的问题,请参考以下文章