首先,我们要建一个layout,里面放一个ListView,长宽皆适应屏幕即可。
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv1"
android:scrollbars="none">
</ListView>
然后,我们要在这个ListView里显示东西,显示什么样的东西,什么样的布局呢?所以,我们要再建一个layout,这里存放我们要显示的布局,此处以一个新闻软件的界面为例。
由该界面可见,此处用了一个ListView来显示这些新闻,而每一个新闻,则由Title标题,Image图片,pubDate发布日期,goodCount点赞量以及newsFrom新闻来源构成。
所以,我们这个布局可以这样写:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="标题"
android:id="@+id/title"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:layout_marginTop="6dp">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_weight="1"
android:id="@+id/img1"
android:src="@mipmap/ic_launcher"
android:scaleType="centerCrop"/>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_weight="1"
android:id="@+id/img2"
android:src="@mipmap/ic_launcher"
android:scaleType="centerCrop"
android:layout_marginLeft="10dp"/>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_weight="1"
android:id="@+id/img3"
android:src="@mipmap/ic_launcher"
android:scaleType="centerCrop"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:weightSum="1"
android:layout_marginTop="6dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:text="来源"
android:id="@+id/from"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="日期"
android:id="@+id/pubDate"/>
<ImageButton
android:layout_width="25dp"
android:layout_height="20dp"
android:src="@mipmap/left_menu_kycg_normal"
android:id="@+id/ib1" />
</LinearLayout>
</LinearLayout>
好了,布局建好了,接下来我们就要加载数据到布局中,然后用适配器,让布局在ListView中规则地显示出来。
我们先写一个适配器,建一个新的类,类名自定,但要让其继承BaseAdapter
public class MyListAdapter2 extends BaseAdapter {
private List<News2> myData;
private Context context;
public MyListAdapter2(List<News2> myData,Context context){
this.myData = myData;
this.context = context;
}
@Override
public int getCount() {
return myData.size();
}
@Override
public Object getItem(int position) {
return myData.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//----------------------------
// View view = LayoutInflater.from(context).inflate(R.layout.list_layout_base1,null);
// TextView title = (TextView) view.findViewById(R.id.title);
// TextView pubDate = (TextView) view.findViewById(R.id.pubDate);
// TextView from = (TextView) view.findViewById(R.id.from);
// ImageView img1 = (ImageView) view.findViewById(R.id.img1);
// ImageView img2 = (ImageView) view.findViewById(R.id.img2);
// ImageView img3 = (ImageView) view.findViewById(R.id.img3);
// News2 news2 = myData.get(position);
// title.setText(news2.getTitle());
// pubDate.setText(news2.getPubDate());
// from.setText(news2.getFrom());
// img1.setImageResource(news2.getImg1());
// img2.setImageResource(news2.getImg2());
// img3.setImageResource(news2.getImg3());
// return view;
//--------------下面的方法更加节省资源----------------
//引用用了setTag()方法,保存了控件,复用了资源。
final ViewHolder vh;
if(convertView==null){
vh = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list_layout_base1,null);
vh.title = (TextView) convertView.findViewById(R.id.title);
vh.pubDate = (TextView) convertView.findViewById(R.id.pubDate);
vh.from = (TextView) convertView.findViewById(R.id.from);
vh.img1 = (ImageView) convertView.findViewById(R.id.img1);
vh.img2 = (ImageView) convertView.findViewById(R.id.img2);
vh.img3 = (ImageView) convertView.findViewById(R.id.img3);
vh.ib1 = (ImageButton) convertView.findViewById(R.id.ib1);
<pre style="font-family: Consolas; background-color: rgb(255, 255, 255);"><pre style="font-family: Consolas; background-color: rgb(255, 255, 255);"><span style="font-size:12px;"><span style="color: rgb(128, 128, 128); "><em>//</em></span><span style="color: rgb(128, 128, 128); font-family: 宋体;"><em>因为</em></span><span style="color: rgb(128, 128, 128); "><em>setTag</em></span><span style="color: rgb(128, 128, 128); font-family: 宋体;"><em>只能包含一个参数,但我们要保存所有的</em></span><span style="color: rgb(128, 128, 128); "><em>view</em></span><span style="color: rgb(128, 128, 128); font-family: 宋体;"><em>,所以我们新建一个类,让它包含其所有,然后一起放进去</em></span></span>
convertView.setTag(vh); }else{ vh = (ViewHolder) convertView.getTag(); } final News2 news2 = myData.get(position); vh.img1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context,"点击了新闻"+position+"的图1",Toast.LENGTH_SHORT).show(); } }); vh.ib1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int i = Integer.parseInt(vh.pubDate.getText().toString())+1; vh.pubDate.setText(String.valueOf(i)); //将赞的数量保存 news2.setCount(i);// vh.pubDate.setTextColor(0xffcc0000);颜色改变,但是没保存,滑动过程中会发生错位 } }); vh.title.setText(news2.getTitle()); //将保存的赞的数量再赋值给pubDate vh.pubDate.setText(news2.getCount()+""); vh.from.setText(news2.getFrom()); vh.img1.setImageResource(news2.getImg1()); vh.img2.setImageResource(news2.getImg2()); vh.img3.setImageResource(news2.getImg3()); return convertView; } public class ViewHolder { TextView title; TextView pubDate; TextView from; ImageView img1; ImageView img2; ImageView img3; ImageButton ib1; }}
适配器也弄好了,万事俱备,只欠东风!我们要在Activity里写一些数据了。由于前面Adapter适配器里我们传入的参数是个List,所以我们要把数据都放在一个ArrayList里,可是我们每次想要传入的是一套数据啊,包括标题、发布时间、来源等属性。所以,我们建一个类,每次传一个它的对象进去,其中包括了这些属性。
News类如下:
public class News2 {
private String title;
private String pubDate;
private String newsID;
private int img1;
private int img2;
private int img3;
private String from;
private int count;
public News2() {
}
public News2(String title, String pubDate, String newsID, int img1, int img2, int img3, String from) {
this.title = title;
this.pubDate = pubDate;
this.newsID = newsID;
this.img1 = img1;
this.img2 = img2;
this.img3 = img3;
this.from = from;
}
public News2(String title, String pubDate, String newsID, int img1, int img2, int img3, String from,int count) {
this.count = count;
this.title = title;
this.pubDate = pubDate;
this.newsID = newsID;
this.img1 = img1;
this.img2 = img2;
this.img3 = img3;
this.from = from;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
public String getNewsID() {
return newsID;
}
public void setNewsID(String newsID) {
this.newsID = newsID;
}
public int getImg1() {
return img1;
}
public void setImg1(int img1) {
this.img1 = img1;
}
public int getImg2() {
return img2;
}
public void setImg2(int img2) {
this.img2 = img2;
}
public int getImg3() {
return img3;
}
public void setImg3(int img3) {
this.img3 = img3;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
最后,就是我们的Activity代码了!
public class List3Activity extends AppCompatActivity {
private ListView lv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list3);
lv1 = (ListView) findViewById(R.id.lv1);
List<News2> list = new ArrayList();
News2 ns = new News2("新闻1","1","n004",R.mipmap.mei01,R.mipmap.mei02,R.mipmap.mei01,"新浪",0);
list.add(ns);
ns = new News2("新闻2","1","n003",R.mipmap.mei02,R.mipmap.mei01,R.mipmap.mei02,"博客园",0);
list.add(ns);
ns = new News2("新闻3","3","n002",R.mipmap.mei01,R.mipmap.mei02,R.mipmap.mei01,"CSDN",0);
list.add(ns);
ns = new News2("新闻4","4","n001",R.mipmap.mei02,R.mipmap.mei01,R.mipmap.mei02,"知乎",0);
list.add(ns);
ns = new News2("新闻1","5","n004",R.mipmap.mei01,R.mipmap.mei02,R.mipmap.mei01,"新浪",0);
list.add(ns);
ns = new News2("新闻2","2","n003",R.mipmap.mei02,R.mipmap.mei01,R.mipmap.mei02,"博客园",0);
list.add(ns);
ns = new News2("新闻3","6","n002",R.mipmap.mei01,R.mipmap.mei02,R.mipmap.mei01,"CSDN",0);
list.add(ns);
ns = new News2("新闻4","7","n001",R.mipmap.mei02,R.mipmap.mei01,R.mipmap.mei02,"知乎",0);
list.add(ns);
ns = new News2("新闻1","8","n004",R.mipmap.mei01,R.mipmap.mei02,R.mipmap.mei01,"新浪",0);
list.add(ns);
ns = new News2("新闻2","9","n003",R.mipmap.mei02,R.mipmap.mei01,R.mipmap.mei02,"博客园",0);
list.add(ns);
ns = new News2("新闻3","1","n002",R.mipmap.mei01,R.mipmap.mei02,R.mipmap.mei01,"CSDN",0);
list.add(ns);
ns = new News2("新闻4","2","n001",R.mipmap.mei02,R.mipmap.mei01,R.mipmap.mei02,"知乎",0);
list.add(ns);
ns = new News2("新闻1","3","n004",R.mipmap.mei01,R.mipmap.mei02,R.mipmap.mei01,"新浪",0);
list.add(ns);
ns = new News2("新闻2","4","n003",R.mipmap.mei02,R.mipmap.mei01,R.mipmap.mei02,"博客园");
list.add(ns);
ns = new News2("新闻3","5","n002",R.mipmap.mei01,R.mipmap.mei02,R.mipmap.mei01,"CSDN");
list.add(ns);
ns = new News2("新闻4","6","n001",R.mipmap.mei02,R.mipmap.mei01,R.mipmap.mei02,"知乎");
list.add(ns);
lv1.setAdapter(new MyListAdapter2(list,this));
}
}