ListView允许用户通过手指上下滑动的方式将屏幕外的数据滚动到屏幕内,同时屏幕上原有的数据则会滚动出屏幕。
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv1"
/>
public class MainActivity extends AppCompatActivity {
private String[]data={
"Apple","Pig","Red",
"Banana","Chicken","Yellow",
"Orange","Cow","Pink",
"Cherry","Sheep","Purple",
"Pear","Cat","Green",
"Snow","Rain","Sun"
};
@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.lv1);
listView.setAdapter(adapter);
}
}
package com.example.practice;
public class Goods {
private String name;
private int imageId;
public Goods(String name,int imageId)
{
this.name=name;
this.imageId=imageId;
}
public String getName()
{
return name;
}
public int getImageId()
{
return imageId;
}
}
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/goods_image"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/goods_name"
android:layout_gravity="center_vertical"
android:layout_marginLeft="110dp"
/>
package com.example.practice;
import android.content.Context;
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 androidx.recyclerview.widget.RecyclerView;
import java.util.List;
//自定义适配器
public class GoodsAdapter extends ArrayAdapter<Goods> {
private int resourceId;
public GoodsAdapter(Context context, int textViewResourceId, List<Goods> objects)
{
super(context,textViewResourceId,objects);
resourceId=textViewResourceId;
}
@Override
//getView方法是每个子项被滚动到屏幕内的时候都会被调用
public View getView(int position, View convertView, ViewGroup parent)
{
Goods good=getItem(position);//获取当前good的实例
// View view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
View view;
if(convertView == null)
{
view=LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
}
else
{
view = convertView;
}
ImageView goodImage=(ImageView) view.findViewById(R.id.goods_image);
TextView goodName=(TextView) view.findViewById(R.id.goods_name);
goodImage.setImageResource(good.getImageId());
goodName.setText(good.getName());
return view;
}
}
package com.example.practice;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Goods>goodsList=new ArrayList<>();
private void initGood()
{
for(int i=0;i<2;i++)
{
Goods good1=new Goods("apple",R.drawable.pic1);
goodsList.add(good1);
Goods good2=new Goods("banana",R.drawable.pic2);
goodsList.add(good2);
Goods good3=new Goods("pear",R.drawable.pic3);
goodsList.add(good3);
Goods good4=new Goods("orange",R.drawable.pic4);
goodsList.add(good4);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initGood();
GoodsAdapter adapter=new GoodsAdapter(MainActivity.this,R.layout.activity_main,goodsList);
ListView listView=(ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
}
initGood();
GoodsAdapter adapter=new GoodsAdapter(MainActivity.this,R.layout.activity_main,goodsList);
ListView listView=(ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Goods good=goodsList.get(position);
Toast.makeText(MainActivity.this,good.getName(),Toast.LENGTH_SHORT).show();
}
});
(上述学习内容来自《第一行代码 Android》第二版 郭霖著)