package com.example.user.android2lesson_03_baseadapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import bean.News;
public class MainActivity extends AppCompatActivity {
// BaseAdapter使用
//第一步.创建一个布局文件,对Item控件进行布局
//第二步.在对应的Activity中添加ListView控件
//第三步.声明属性并进行初始化
//第四步.创建一个类,继承自BaseAdapter
//第五步.创建Adapter并进行绑定
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView= (ListView) findViewById(R.id.listView);
MyAdapter adapter=new MyAdapter(this,getData());
mListView.setAdapter(adapter);
}
private List getData(){
List list=new ArrayList<>();
News new1=new News(R.drawable.image,"逗逼","大狗");
list.add(new1);
News new2=new News(R.drawable.image1,"逗逼","二狗");
list.add(new2);
News new3=new News(R.drawable.image2,"逗逼","狗狗");
list.add(new3);
return list;
}
}
package com.example.user.android2lesson_03_baseadapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import bean.News;
/**
* Created by user on 2016/7/6.
*/
public class MyAdapter extends BaseAdapter{
//声明属性,保存数据和上下文对象
private List list;
private Context context;
public MyAdapter(Context context,List list){
this.context=context;
this.list=list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) { return list.get(i);
}
@Override
//获取Item对应的ID
public long getItemId(int i) {
return i;
}
@Override
//获取i对应位置的视图
public View getView(int i, View view, ViewGroup viewGroup) {
/**优化前
//去获取指定的layout文件中指定id的控件
View v=LayoutInflater.from(context).inflate(R.layout.item_layout,null);
ImageView image= (ImageView) v.findViewById(R.id.image);
TextView titleView= (TextView) v.findViewById(R.id.text1);
TextView infoView= (TextView) v.findViewById(R.id.text2);
//拿到数据
News news= (News) getItem(i);
//根据数据对界面进行赋值
image.setBackgroundResource(news.getImg());
titleView.setText(news.getTitle().toString());
infoView.setText(news.getInfo().toString());
return v;
*/
//优化后
//利用缓存
ViewHolder Holder;
if (view==null){
//将指定的的布局文件转换成视图文件
//这里是缓存的界面控件
view= View.inflate(context,R.layout.item_layout,null);
Holder=new ViewHolder();
Holder.imageView= (ImageView) view.findViewById(R.id.image);
Holder.titleView= (TextView) view.findViewById(R.id.text1);
Holder.infoView= (TextView) view.findViewById(R.id.text2);
view.setTag(Holder);
}else {
Holder= (ViewHolder) view.getTag();
}
News news= (News) getItem(i);
Holder.imageView.setBackgroundResource(news.getImg());
Holder.titleView.setText(news.getTitle().toString());
Holder.infoView.setText(news.getInfo().toString());
return view;
}
}
package com.example.user.android2lesson_03_baseadapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by user on 2016/7/6.
*/
public class ViewHolder {
//这里罗列的是所有的控件
public ImageView imageView;
public TextView titleView;
public TextView infoView;
}
package bean;
/**
* Created by user on 2016/7/6.
*/
public class News {
//存放图片,资源都是采用id的形式
private int img;
private String title;
private String info;
//初始化方法
public News(int img, String title, String info){
this.img=img;
this.title=title;
this.info=info;
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.user.android2lesson_03_baseadapter.MainActivity">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+idstView">
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:id="@+id/text1"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是一条消息"
android:id="@+id/text2"/>