简单的介绍一下滚动布局 ListView 的用法
发一下效果图 是可以滚动的 :
1,加载ListView布局,设置长和宽为match_parent
<?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/lv" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>
2,创建Fruit类,作为ListView 的适配器类型
public class Fruit { private String name ; private int imageId; public Fruit(int imageId, String name) { this.imageId = imageId; this.name = name; } public int getImageId() { return imageId; } public String getName() { return name; }
3,给ListView 的子项指定一个自定义的布局,在layout新建fruit_item.xml ,imageView指定图片,TextView指定名字
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/fruit_item" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/Tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="50dp"/> </LinearLayout>
4,创建一个自定义的适配器,这个类继承ArrayAdapter,并将泛型指定为Fruit类。适配器的构造器接收三个参数,不知道意思就看一下在MainActivity中实例化的时候传入的参数,就差不多明白了
public class FruitAdapter extends ArrayAdapter<Fruit> { private int resourceId; public FruitAdapter(Context context , int textViewResourceId, List<Fruit> objects){ super(context,textViewResourceId,objects); this.resourceId = textViewResourceId; } @NonNull @Override //每个子项被滚动到屏幕内会调用这个方法 //concertView 参数,用于将之前加载好的布局进行缓存,以便进行重用 public View getView(int position, View convertView, @NonNull ViewGroup parent) { Fruit fruit = getItem(position); View view ; ViewHolder viewHolder ; //如果convertView 为null,则加载布局,不为空则对convertView进行重用,避免了每次加载布局 //加入一个内部类ViewHolder 用于对控件的实例进行缓存,避免了每次都要加载控件的实例 //这样就提高了ListView 的运行效率 if(convertView == null){ view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false); viewHolder = new ViewHolder(); viewHolder.fruitImage=(ImageView)view.findViewById(R.id.fruit_item); viewHolder.fruitName=(TextView) view.findViewById(R.id.Tv); view.setTag(viewHolder); //将viewHolder 存储在View中 }else{ view = convertView; viewHolder = (ViewHolder) view.getTag(); } viewHolder.fruitImage.setImageResource(fruit.getImageId()); viewHolder.fruitName.setText(fruit.getName()); return view; } class ViewHolder{ ImageView fruitImage; TextView fruitName; } }
5,修改MainActivity 的代码,新建集合,初始化要显示的数据,实例化适配器,将适配器对象传给ListView就好了
public class MainActivity extends AppCompatActivity { List<Fruit> list ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView =(ListView)findViewById(R.id.lv); init(); FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item,list); listView.setAdapter(adapter); } private void init() { list = new ArrayList<>(); for (int i = 1 ;i<= 50 ;i++){ Fruit f = new Fruit(R.drawable.tab_2,"name"+i); list.add(f); } } }
这样ListView 的滚动界面就完成了!