activity_my_adapter.xml里的my_listview.xml
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview"
/>
listview_item.xml
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
>
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/photo_ib"
android:layout_alignParentLeft="true"
/>
android:layout_width="wrap_content"
android:layout_height="24dp"
android:gravity="center"
android:id="@+id/username_tv"
android:layout_toRightOf="@id/photo_ib"
android:text="@string/app_name"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:textSize="12sp"
/>
android:layout_width="24dp"
android:layout_height="24dp"
android:id="@+id/vip_ib"
android:layout_toRightOf="@id/username_tv"
/>
android:layout_width="24dp"
android:layout_height="24dp"
android:id="@+id/pop_menu_ib"
android:layout_alignParentRight="true"
/>
android:layout_width="wrap_content"
android:layout_height="16dp"
android:gravity="center"
android:id="@+id/month_tv"
android:layout_below="@id/username_tv"
android:layout_toRightOf="@id/photo_ib"
android:text="1-1"
android:textSize="12sp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
/>
android:layout_width="wrap_content"
android:layout_height="16dp"
android:gravity="center"
android:id="@+id/comefrom_tv"
android:layout_below="@id/username_tv"
android:layout_toRightOf="@id/month_tv"
android:text="来自 "
android:textSize="12sp"
android:paddingRight="4dp"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/content_tv"
android:text="@string/content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textSize="16sp"
android:layout_below="@id/photo_ib"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:orientation="horizontal"
android:layout_below="@id/content_tv"
>
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/forward_tv"
android:gravity="center"
android:textSize="10sp"
android:text="1"
/>
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/common_tv"
android:gravity="center"
android:textSize="10sp"
android:text="评论"
/>
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/praise_tv"
android:gravity="center"
android:textSize="10sp"
android:text="赞"
/>
strings.xml
name="content">少的可怜发上来的积分来的是来的是更厉害萨拉合法的三国的塞给了受到法律的塞给了返回搞,士大夫的森林里商大会搞
加载中...
User.java
public class User {
private int mPhoto;
private int mVip;
private int mPopMenu;
private int mContent;
private String mUsername;
private String mMonth;
private String mComefrom;
public int getPhoto() {
return mPhoto;
}
public void setPhoto(int photo) {
mPhoto = photo;
}
public int getVip() {
return mVip;
}
public void setVip(int vip) {
mVip = vip;
}
public int getPopMenu() {
return mPopMenu;
}
public void setPopMenu(int popMenu) {
mPopMenu = popMenu;
}
public String getUsername() {
return mUsername;
}
public void setUsername(String username) {
mUsername = username;
}
public String getMonth() {
return mMonth;
}
public void setMonth(String month) {
mMonth = month;
}
public String getComefrom() {
return mComefrom;
}
public void setComefrom(String comefrom) {
mComefrom = comefrom;
}
public int getContent() {
return mContent;
}
public void setContent(int content) {
mContent = content;
}
}
MyAdapterActivity.java
public class MyAdapterActivity extends AppCompatActivity implements AbsListView.OnScrollListener {
private static final String TAG = "MyAdapterActivity";
private ListView mListView;
private TextView mLoadingTxt;
private MyAdapter mMyAdapter;
private List<User> mDatas;
private int mLastVisibleIndex ; // 列表最后可见的项的索引
private int mModCount; // 记录getDatas调用的次数
private SimpleDateFormat mDateFormat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_adapter);
mLoadingTxt = new TextView(this);
mLoadingTxt.setText(R.string.loading_text);
mLoadingTxt.setGravity(Gravity.CENTER);
mLoadingTxt.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); // 字体加粗
// 初始化日期格式
mDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
// 初始化listview
mListView = (ListView) findViewById(R.id.listview);
// 实例化LinkedList
mDatas = new LinkedList<>();
mMyAdapter = new MyAdapter(this, getDatas(mDatas));// 自定义adapter
mListView.addFooterView(mLoadingTxt);
mListView.setAdapter(mMyAdapter);
mListView.removeFooterView(mLoadingTxt);
}
private List<User> getDatas(List<User> datas) {
int i;
// start i = 1 11 21 31 41
// end i = 10 20 30 40 50
if (mModCount == 0) {
i = 1;
} else {
i = mModCount * 10 + 1;
}
for (;i <= mModCount *10+10; i++) {
User user = new User();
user.setPhoto(R.mipmap.ic_launcher);
user.setUsername("等风的草" + i + "号");
user.setVip(R.drawable.vip);
user.setPopMenu(R.drawable.pop_pic);
user.setMonth(mDateFormat.format(new Date()));
user.setComefrom("微博weibo" + i);
user.setContent(R.string.content);
datas.add(user);
}
mModCount++;
return datas;
}
}
在getDatas方法中使用model来封装数据.
MyAdapter.java
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<User> mDatas;
public MyAdapter(Context context, List<User> datas) {
mContext = context;
mDatas = datas;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
通过convertview的setTag方法和getTag方法来将我们要显示的数据来绑定在convertview 上。
如果convertview 是第一次展示,就创建新的Holder对象与之绑定,并在最后通过return convertview 返回来显示新数据;
如果convertview 是回收来的那么就不必创建新的holder对象,只需要把原来的绑定的holder取出加上新的数据就行了。
*/
View v = convertView;
ViewHolder viewHolder;
if (v == null) {
v = LayoutInflater.from(mContext).inflate(R.layout.listview_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.photo = (ImageButton) v.findViewById(R.id.photo_ib);
viewHolder.username = (TextView) v.findViewById(R.id.username_tv);
viewHolder.vip = (ImageButton) v.findViewById(R.id.vip_ib);
viewHolder.popmenu = (ImageButton) v.findViewById(R.id.pop_menu_ib);
viewHolder.month = (TextView) v.findViewById(R.id.month_tv);
viewHolder.comefrom = (TextView) v.findViewById(R.id.comefrom_tv);
viewHolder.content = (TextView) v.findViewById(R.id.content_tv);
v.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) v.getTag();
}
User user = (User) getItem(position);
viewHolder.photo.setImageResource(user.getPhoto());
viewHolder.vip.setImageResource(user.getVip());
viewHolder.popmenu.setImageResource(user.getPopMenu());
viewHolder.username.setText(user.getUsername());
viewHolder.month.setText(user.getMonth());
viewHolder.comefrom.setText(user.getComefrom());
viewHolder.content.setText(user.getContent());
return v;
}
/**
* 静态内部类,只实例化一次
*/
static class ViewHolder {
ImageButton photo;
TextView username;
ImageButton vip;
ImageButton popmenu;
TextView month;
TextView comefrom;
TextView content;
}
}
该类使用一个ViewHolder静态内部类来优化listview的显示.
在onCreate方法中添加,设置监听listview滚动状态
mListView.setOnScrollListener(this);
实现bsListView.OnScrollListener接口的两个方法
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// 当当前可见索引是列表最后一个并且不滚动时,加载数据
if (mLastVisibleIndex == mMyAdapter.getCount() - 1 && scrollState == SCROLL_STATE_IDLE) {
mListView.addFooterView(mLoadingTxt); // 添加页脚(放在ListView最后)
// simple adapter实现分页加载
// 另起一个线程加载
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
getDatas(mDatas);
runOnUiThread(new Runnable() {
@Override
public void run() {
mMyAdapter.notifyDataSetChanged();
if (mListView.getFooterViewsCount() > 0)
mListView.removeFooterView(mLoadingTxt); // 加载完数据后移除页脚
}
});
Log.d(TAG, "onScrollStateChanged: 模拟网络数据加载。。。");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
mLastVisibleIndex = firstVisibleItem + visibleItemCount - 1;
}
最后贴上activity的完整清单
public class MyAdapterActivity extends AppCompatActivity implements AbsListView.OnScrollListener {
private static final String TAG = "MyAdapterActivity";
private ListView mListView;
private TextView mLoadingTxt;
private MyAdapter mMyAdapter;
private List<User> mDatas;
private int mLastVisibleIndex ; // 列表最后可见的项的索引
private int mModCount; // 记录getDatas调用的次数
private SimpleDateFormat mDateFormat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_adapter);
mLoadingTxt = new TextView(this);
mLoadingTxt.setText(R.string.loading_text);
mLoadingTxt.setGravity(Gravity.CENTER);
mLoadingTxt.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); // 字体加粗
// 初始化日期格式
mDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
// 初始化listview
mListView = (ListView) findViewById(R.id.listview);
// 实例化LinkedList
mDatas = new LinkedList<>();
mMyAdapter = new MyAdapter(this, getDatas(mDatas));
mListView.addFooterView(mLoadingTxt);
mListView.setAdapter(mMyAdapter);
mListView.removeFooterView(mLoadingTxt);
mListView.setOnScrollListener(this);
}
private List<User> getDatas(List<User> datas) {
int i;
// start i = 1 11 21 31 41
// end i = 10 20 30 40 50
if (mModCount == 0) {
i = 1;
} else {
i = mModCount * 10 + 1;
}
for (;i <= mModCount *10+10; i++) {
User user = new User();
user.setPhoto(R.mipmap.ic_launcher);
user.setUsername("等风的草" + i + "号");
user.setVip(R.drawable.vip);
user.setPopMenu(R.drawable.pop_pic);
user.setMonth(mDateFormat.format(new Date()));
user.setComefrom("微博weibo" + i);
user.setContent(R.string.content);
datas.add(user);
}
mModCount++;
return datas;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// 当当前可见索引是列表最后一个并且不滚动时,加载数据
if (mLastVisibleIndex == mMyAdapter.getCount() - 1 && scrollState == SCROLL_STATE_IDLE) {
mListView.addFooterView(mLoadingTxt); // 添加页脚(放在ListView最后)
// simple adapter实现分页加载
// 另起一个线程加载
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
getDatas(mDatas);
runOnUiThread(new Runnable() {
@Override
public void run() {
mMyAdapter.notifyDataSetChanged();
if (mListView.getFooterViewsCount() > 0)
mListView.removeFooterView(mLoadingTxt); // 加载完数据后移除页脚
}
});
Log.d(TAG, "onScrollStateChanged: 模拟网络数据加载。。。");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
mLastVisibleIndex = firstVisibleItem + visibleItemCount - 1;
}
}
如有疑问请指出交流,谢谢!