一、ListView的简单使用
MainActivity.java
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends AppCompatActivity { String[] data ={"a","b","c","d","a","b","c","d","a","b","c","d","a","b","c","d","a","b","c","d","a","b","c","d","a","b","c","d","a","b","c","d"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter adapter = new ArrayAdapter(MainActivity.this , android.R.layout.simple_list_item_1 , data); /*创建适配器 ; *其中ArrayAdapter构造方法的参数分别为当前的Context,List子项布局的id,以及要适配的数据; *android.R.layout.simple_list_item_1为内置子布局 */
ListView listView = (ListView)findViewById(R.id.listview); listView.setAdapter(adapter); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" > <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
显示效果
二、自定义ListView(向每一栏中插入图片和文字)
MainActivity.java
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<Fruit> fruits = 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,fruits); //此次改变,主要对适配器、ListView的子布局、以及集合对象作出了调整 ListView listView = (ListView)findViewById(R.id.list_item); listView.setAdapter(adapter); } private void initfruits(){ for (int i=0;i<15;i++){ Fruit apple = new Fruit("apple",R.drawable.apple); fruits.add(apple); Fruit grape = new Fruit("grape",R.drawable.grapes); fruits.add(grape); } } }
Fruit.java
public class Fruit { private String name_Text; private int image_id; public Fruit(String name_Text,int image_id){ this.name_Text = name_Text; this.image_id = image_id; } public String getName_Text(){ return name_Text; } public int getImage_id(){ return image_id; } }
FruitAdapter.java
import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.List; public class FruitAdapter extends ArrayAdapter<Fruit> { private int Resourse; public FruitAdapter(Context context, int textViewResourceId, List<Fruit> object){ super(context,textViewResourceId,object); Resourse = textViewResourceId; } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { //getView方法,当每个子项滚到屏幕内部时候调用 Fruit fruit = getItem(position); //获得当前Fruit的实例,其中position表示在列表中的具体位置的索引 View view = LayoutInflater.from(getContext()).inflate(Resourse,parent,false); /*LayoutInflater使用情况:当有一个XML布局文件需要解析成View的时候,如:fragment中的onCreate,ListView中的view,等; * *第一个传入的参数resourse是想要加载的布局资源; *第二个传入的参数是指当前载入的视图要将要放入在层级结构中的根视图; *如果传入了第三个参数attachToRoot,那么它会决定视图在载入完成后是否附加到传入的根视图中去; */ ImageView imageView = (ImageView)view.findViewById(R.id.image); TextView textView = (TextView)view.findViewById(R.id.name); imageView.setImageResource(fruit.getImage_id()); textView.setText(fruit.getName_Text()); return view; } }
activity_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/list_item" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
fruit_item.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="wrap_content"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_marginLeft="10dp" /> </LinearLayout>
显示效果
三、ListView的优化
由于getView()在每一次调用时都会重新加载控件,可以使用ListView的视图缓冲机制去提高效率。
修改getView()方法中代码
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { //本次利用到了convert参数,这个参数用于将之前加载好的数据进行缓存 Fruit fruit = getItem(position); View view; ViewHolder viewHolder = new ViewHolder(); if(convertView == null){ view = LayoutInflater.from(getContext()).inflate(Resourse, parent, false); viewHolder.imageView = (ImageView) view.findViewById(R.id.image); viewHolder.textView = (TextView) view.findViewById(R.id.name); view.setTag(viewHolder); }else { view = convertView; viewHolder = (ViewHolder)view.getTag(); } viewHolder.imageView.setImageResource(fruit.getImage_id()); viewHolder.textView.setText(fruit.getName_Text()); return view; } class ViewHolder{ //利用内部类ViewHodler对控件实例进行缓存 ImageView imageView; TextView textView; } }
原文地址:https://www.cnblogs.com/qianbin/p/8432920.html
时间: 2024-11-01 16:12:27