思维导图如下:
代码片段如下:
public class listactivity extends AppCompatActivity {
private Listlist=new ArrayList<>();
private ListView listView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listactivity);
addshuju();
listView= (ListView) findViewById(R.id.list);
listView.setAdapter(new frute(this,R.layout.buju,list));
}
private void addshuju() {
list.add(new com.example.croppersample.frute("苹果",R.drawable.loan_photo_x2));
list.add(new com.example.croppersample.frute("苹果",R.drawable.loan_photo_x2));
list.add(new com.example.croppersample.frute("苹果",R.drawable.loan_photo_x2));
list.add(new com.example.croppersample.frute("苹果",R.drawable.loan_photo_x2));
list.add(new com.example.croppersample.frute("苹果",R.drawable.loan_photo_x2));
}
class frute extends ArrayAdapter{
cheng c;
int h;
public frute(Context context, int resource, List objects) {
super(context, resource, objects);
h=resource;
}
@Override
public int getCount() {
return list.size();
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=LayoutInflater.from(getContext()).inflate(h,parent,false);
com.example.croppersample.frute f=getItem(position);
c=new cheng();
c.tt= (TextView) v.findViewById(R.id.textView);
c.ii= (ImageView) v.findViewById(R.id.imageView);
c.tt.setText(f.getName());
c.ii.setImageResource(f.getId());
return v;
}
class cheng{
TextView tt;
ImageView ii;
}
}
}
bean文件中
public class frute {
private String name;
private int id;
public frute(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
在buju.xml文件中
注意:可以通过如下代码修改listview的item的颜色和item之间的间隔
android:divider="#ffffff"
android:dividerHeight="10dp"
利用getView方法中的convertView参数,实现系统内存的优化, convertView参数当第一个item加载的时候它是null的,但是加载第二个item及以后的item的时候它就不是null了,因为第一个item创建好之后return回去的那个view就传到了getView的第二个参数那里了,但是要注意要复用的话记得用setTag绑定viewHolder,和用getTag得到viewHolder,我当时想的是多个item重复使用一个view和一个viewholder来实现复用,太年轻了,实现不了,只有通过getView的第二个参数来实现复用
public class XueHuaActivity extends AppCompatActivity {
private ListView listView;
private ShiPeiQi shiPeiQi;
private List list;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xuehua);
listView = (ListView) findViewById(R.id.list);
list = new ArrayList<>();
list.add("第一个");
list.add("第二个");
list.add("第三个");
list.add("第四个");
list.add("第五个");
list.add("第六个");
shiPeiQi = new ShiPeiQi(R.layout.hehe, list);
listView.setAdapter(shiPeiQi);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
Toast.makeText(XueHuaActivity.this, list.get(position), Toast.LENGTH_SHORT).show();
}
});
}
private class ShiPeiQi extends BaseAdapter {
private int layout;
private List list;
private ViewHolder viewHolder;
public ShiPeiQi(int layout, List list) {
this.layout = layout;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {//第一个item的convertView为空
convertView = LayoutInflater.from(getApplicationContext()).inflate(layout, null);//在这里设置了复用,以后的item将会复用第一个item,节省内存资源
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) convertView.findViewById(R.id.textView7);
convertView.setTag(viewHolder);
} else {
viewHolder= (ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(list.get(position));
return convertView;
}
/*将上面的getView中的方法体写成如下也行,上面的那个是包建强的写法,以下是郭霖的写法
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
view = LayoutInflater.from(getApplicationContext()).inflate(layout, null);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) view.findViewById(R.id.textView7);
view.setTag(viewHolder);//这里view调用了setTag方法,当最后return之后,view就传到了getView的第二个参数convert
} else {
view=convertView;
viewHolder= (ViewHolder) view.getTag();
}
viewHolder.textView.setText(list.get(position));
return view;
}
*/
class ViewHolder {
private TextView textView;
}
}
}
下面这个博客可以看看
https://blog.csdn.net/xulianboblog/article/details/51419039