Android開發學習記錄--ListView使用
阿新 • • 發佈:2019-03-21
margin eap pan 器) ica tlist findview 實體類 image
1.ListView的簡單使用
首先建立一個新的項目,在xml文件中添加ListView控件,如下所示:
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
接下來修改MainActivity中的代碼:
public class MainActivity extends AppCompatActivity {
private String[] data={"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango","Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango"};@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.list_view);listView.setAdapter(adapter);
}
}
將數據存儲在數組中,借助ArrayAdapter適配器傳遞數據給ListView,最後調用setAdapter()方法,將適配器對象傳遞出去,關聯建立完成。
2.定制ListView的界面
定義一個新的實體類,作為ListView適配器的適配類型。
public class Fruit {
private String name;//水果名字
private int imageId;//水果對應圖片id
public Fruit(String name,int imageId){
this.name=name;this.imageId=imageId;
}
public int getImageId() {
return imageId;
}
public java.lang.String getName() {
return name;
}
}
指定一個自定義布局,代碼如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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_vertical"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
其中控件ImageView用來顯示水果圖片,TextView用來顯示水果名稱。
接下來自定義一個適配器,繼承自ArrayAdapter:
public class FruitAdapter extends ArrayAdapter<Fruit>{
private int resourceId;
public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects){
super(context,textViewResourceId,objects);
resourceId=textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
Fruit fruit=getItem(position);
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;
}
}
FruitAdapter重寫了父類的一組構造函數,用於將上下文、ListView子項布局的id和數據都傳遞進來。在重寫的getView()方法中,首先通過getItem()方法得到當前的Fruit實例,然後使用LayoutInflater來為這個子項加載我們傳入的數據。
接下來對MainActivity進行修改:
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.list_view);
listView.setAdapter(adapter);
}
private void initFruits(){
for(int i=0;i<2;i++)
{
Fruit apple=new Fruit("Apple",R.drawable.fruit_1);
fruitlist.add(apple);
Fruit banana=new Fruit("banana",R.drawable.fruit_2);
fruitlist.add(banana);
Fruit orange=new Fruit("orange",R.drawable.fruit_1);
fruitlist.add(orange);
Fruit watermelon=new Fruit("watermelon",R.drawable.fruit_1);
fruitlist.add(watermelon);
Fruit pear=new Fruit("pear",R.drawable.fruit_2);
fruitlist.add(pear);
}
}
}
運行程序,效果如下:
3.ListView的點擊事件
在MainActivity中添加如下代碼(為ListView註冊一個監聽器),點擊水果時可以顯示該水果名:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Fruit fruit=fruitlist.get(position);
Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
}
});
運行程序,效果如下:
Android開發學習記錄--ListView使用