标准的ListView使用(带缓存)

来源:互联网 发布:csgo顿卡优化教程 编辑:程序博客网 时间:2024/06/07 09:27

以笑话列表为例

1、新建Joke.java


/**
* Created by Jorble on 2016/3/1.
*/
public class Joke {
private String title;
private String content;
private String type;
private String updatatime;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getUpdatatime() {
return updatatime;
}

public void setUpdatatime(String updatatime) {
this.updatatime = updatatime;
}
}
2、新建布局文件item_list_joke.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:id="@+id/updatatime"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

</LinearLayout>
3、新建列表适配器JokeListAdapter类(带添加、删除功能)

public class JokeListAdapter extends BaseAdapter {

private LinkedList<Joke> mData;
private Context mContext;

public JokeListAdapter(LinkedList<Joke> mData, Context mContext) {
this.mData = mData;
this.mContext = mContext;
}

@Override
public int getCount() {
return mData.size();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_joke,parent,false);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.type = (TextView) convertView.findViewById(R.id.type);
holder.updatatime = (TextView) convertView.findViewById(R.id.updatatime);
holder.content = (TextView) convertView.findViewById(R.id.content);

convertView.setTag(holder); //Holder存储到convertView
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(mData.get(position).getTitle());
holder.type.setText(mData.get(position).getTitle());
holder.updatatime.setText(mData.get(position).getTitle());
holder.content.setText(mData.get(position).getTitle());
return convertView;
}

static class ViewHolder{
TextView title;
TextView type;
TextView updatatime;
TextView content;
}

/**
* 添加数据
* */
public void add(Joke data) {
if (mData == null) {
mData = new LinkedList<>();
}
mData.add(data);
notifyDataSetChanged();
}

/**
* 删除数据
* */
public void remove(int position) {
if(mData != null) {
mData.remove(position);
}
notifyDataSetChanged();
}
}
4、在MainActivity中使用

(1)activity_main.xml中添加listview控件

<ListView
android:id="@+id/joke_lv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
(2)在MainActivity的oncreat中添加

       
/**
* 使用列表
* */
ListView joke_lv = (ListView) view.findViewById(R.id.joke_lv);
List<Joke> mData = new LinkedList<Joke>();

Joke joke1=new Joke();
joke1.setTitle("狼来了");
joke1.setType("经典笑话");
joke1.setUpdatatime("2016.3.1");
joke1.setContent("骗你的,狼没来!哈哈哈!");
mData.add(joke1);

JokeListAdapter mAdapter = new JokeListAdapter((LinkedList<Joke>) mData, this);
joke_lv.setAdapter(mAdapter);


5、运行如图

0 0
原创粉丝点击