Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> [Android新手學習筆記23]-如何使用ListView展示列表數據

[Android新手學習筆記23]-如何使用ListView展示列表數據

編輯:關於Android編程

基本配置:

android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />

 

設置Adapter:

public class MainActivity extends AppCompatActivity {
private String[] data = {
"草莓",
"梨",
"芒果",
"蘋果",
"西瓜",
"香蕉",
"櫻桃",
};

@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
);
ListView listView = (ListView) findViewById(R.id.list_view);
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 this.name;
}

public int getImageId() {
return this.imageId;
}
}

 

 

2.自定義ListView布局

新建布局文件:fruit_item.xml:

xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

android:id="@+id/fruit_image"
android:layout_width="60dp"
android:layout_height="60dp" />

android:id="@+id/fruit_name"
android:textSize="30sp"
android:gravity="center_vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_height="60dp" />

 

3.Adapter

新建FruitAdapter類繼承ArrayAdapter

public class FruitAdapter extends ArrayAdapter {
private int resourceId;

public FruitAdapter(Context context, int textViewResourceId, List objects) {
super(context, textViewResourceId, objects);

resourceId = textViewResourceId;
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = getItem(position); // 獲取當前項的Fruit實例
View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());

return view;
}
}

 

 

4.使用

初始化數據,創建FruitAdapter,設置adapter:

public class MainActivity extends AppCompatActivity {
private List 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.list_view);
listView.setAdapter(adapter);
}

private void initFruits() {
Fruit strawberry = new Fruit("草莓", R.drawable.strawberry);
fruitList.add(strawberry);

Fruit pear = new Fruit("梨", R.drawable.pear);
fruitList.add(pear);

Fruit mango = new Fruit("芒果", R.drawable.mango);
fruitList.add(mango);

Fruit apple = new Fruit("蘋果", R.drawable.apple);
fruitList.add(apple);

Fruit watermelon = new Fruit("西瓜", R.drawable.watermelon);
fruitList.add(watermelon);

Fruit banana = new Fruit("香蕉", R.drawable.banana);
fruitList.add(banana);

Fruit cherry = new Fruit("櫻桃", R.drawable.cherry);
fruitList.add(cherry);
}
}

 

性能優化:



@Override
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = getItem(position); // 獲取當前項的Fruit實例

View view;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
} else {
view = convertView;
}

ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());

return view;
}

 

convertView參數,用於將之前加載好的布局進行緩存,這樣不會再重復去加載布局。

新增一個內部類ViewHolder,用於對控件的實例進行緩存。就不用每次都通過findViewById()方法來獲取實例了。

最終實現代碼如下:

public class FruitAdapter extends ArrayAdapter {
private int resourceId;

public FruitAdapter(Context context, int textViewResourceId, List objects) {
super(context, textViewResourceId, objects);

resourceId = textViewResourceId;
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = getItem(position); // 獲取當前項的Fruit實例

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
}

viewHolder.fruitImage.setImageResource(fruit.getImageId());
viewHolder.fruitName.setText(fruit.getName());

return view;
}

class ViewHolder {
ImageView fruitImage;
TextView fruitName;
}
}

 

 

增加ListView點擊事件:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterViewparent, View view, int position, long id) {
Fruit fruit = fruitList.get(position);
Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();
}
});

 


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved