转载请注明出处:自定义ListView ,适配器(BaseAdapter)_Mr_Leixiansheng的博客-CSDN博客
步骤同自定义ListView
区别:是用的是 BaseAdapter 作为适配器
代码如下:
1、新建News类
package com.example.leixiansheng.news;
/**
* Created by Leixiansheng on 2017/1/11.
*/
public class News {
String title;
String content;
String conmment;
int image;
public News(String conmment, String content, int image, String title) {
this.conmment = conmment;
this.content = content;
this.image = image;
this.title = title;
}
public String getConmment() {
return conmment;
}
public void setConmment(String conmment) {
this.conmment = conmment;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
2、ListView显示布局制定
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_view"
android:layout_centerVertical="true"
android:layout_width="80dp"
android:layout_height="70dp" />
<TextView
android:id="@+id/title"
android:textSize="20dp"
android:layout_toRightOf="@id/image_view"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/content"
android:textSize="15dp"
android:layout_width="match_parent"
android:layout_below="@+id/title"
android:layout_toRightOf="@id/image_view"
android:lines="2"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/comment"
android:text="dsfjas"
android:textSize="15dp"
android:textColor="@color/colorAccent"
android:layout_below="@id/content"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
3、加载ListView
<?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">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
4、使用BaseAdapter作为适配器
package com.example.leixiansheng.news;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<News> newsList = new ArrayList<News>();;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
ListView listView = (ListView) findViewById(R.id.list_view);
//使用BaseAdapter作为适配器
listView.setAdapter(new BaseAdapter() {
//获取条目个数
@Override
public int getCount() {
return newsList.size();
}
@Override
public Object getItem(int i) {
return newsList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
//区别
News news = newsList.get(i);
View view1 = null;
ViewHolder viewHolder = null;
if (view == null) {
view1 = View.inflate(MainActivity.this, R.layout.item_listview, null);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) view1.findViewById(R.id.image_view);
viewHolder.title = (TextView) view1.findViewById(R.id.title);
viewHolder.content = (TextView) view1.findViewById(R.id.content);
viewHolder.comment = (TextView) view1.findViewById(R.id.comment);
view1.setTag(viewHolder);
} else {
view1 = view;
viewHolder = (ViewHolder) view1.getTag();
}
viewHolder.imageView.setImageResource(news.getImage());
viewHolder.title.setText(news.getTitle());
viewHolder.content.setText(news.getContent());
viewHolder.comment.setText(news.getConmment());
return view1;
}
});
}
private void init() {
int image = R.mipmap.ic_launcher;
for (int i = 1; i < 51; i++) {
News news = new News(String.valueOf(i),String.valueOf(i+""+i),image,String.valueOf(i+""+i+""+i));
newsList.add(news);
}
}
private class ViewHolder {
ImageView imageView;
TextView title;
TextView content;
TextView comment;
}
}