实现item监听跳转到指定页面
监听注册
listView.setOnItemClickListener(this);
回调方法
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getAdapter().getItem(position);
if (item instanceof Entry) {
Entry entry = (Entry) item;
Toast.makeText(MainActivity.this, entry.getText(), Toast.LENGTH_SHORT).show();
}
实现下拉刷新, 下拉加载功能
监听注册
//实现下拉刷新, 上拉加载
listView.setOnTouchListener(this);
listView.setOnScrollListener(this);
事件处理
//实现下拉刷新
@Override
public boolean onTouch(View v, MotionEvent event) {
View childAt = ((ViewGroup) v).getChildAt(0);
if (childAt == null) {
return false;
}
//必须第一个完全可见才可以
if (firstVisibleItem == 0 && childAt.getTop() == v.getPaddingTop()) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downY = event.getY();
break;
case MotionEvent.ACTION_UP:
if (event.getY() > downY) {
Toast.makeText(MainActivity.this, "正在刷新...", Toast.LENGTH_SHORT).show();
//刷新数据
isRefreshing =true;
ResultServer server = Tools.getInsances(ResultServer.class);
page = 0;
//别忘了清空一下
adapter.clear();
server.getResult(page,10).execute(this);
}
break;
}
}
return false;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.firstVisibleItem = firstVisibleItem;
//实现上拉加载:
if(totalItemCount - (firstVisibleItem + visibleItemCount) < 5){
// Toast.makeText(MainActivity.this, "需要加载了", Toast.LENGTH_SHORT).show();
if (!isRefreshing) {
isRefreshing= true;
ResultServer server = Tools.getInsances(ResultServer.class);
page += 10;
server.getResult(page, 10).execute(this);
}
}
}
如何进行多布局重用
我们需要重写两个方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
if (getItemViewType(position) == 0) {
convertView = inflater.inflate(R.layout.item_single, parent, false);
} else {
convertView = inflater.inflate(R.layout.item_muti, parent, false);
}
convertView.setTag(new ViewHolder(convertView));
}
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.title.setText(list.get(position).getTitle());
holder.source.setText(list.get(position).getSource());
holder.time.setText(list.get(position).getTime());
ImageUtil.loadImage(holder.image, list.get(position).getImage());
if (getItemViewType(position) == 1){
ImageUtil.loadImage(holder.image1, list.get(position).getImgExtra().get(0).get("imgsrc"));
ImageUtil.loadImage(holder.image2, list.get(position).getImgExtra().get(1).get("imgsrc"));
}
return convertView;
}
private static class ViewHolder {
private TextView title;
private TextView source;
private TextView time;
private ImageView image;
private ImageView image1;
private ImageView image2;
public ViewHolder(View itemView) {
title = (TextView) itemView.findViewById(R.id.title);
source = (TextView) itemView.findViewById(R.id.source);
time = (TextView) itemView.findViewById(R.id.time);
image = (ImageView) itemView.findViewById(R.id.image);
image1 = ((ImageView) itemView.findViewById(R.id.image1));
image2 = ((ImageView) itemView.findViewById(R.id.image2));
}
}
//多布局复用
@Override //控件的位置
public int getItemViewType(int position) {
if (list.get(position).getImgExtra() == null) {
return 0;
} else {
return 1;
}
}
//多少个布局
@Override
public int getViewTypeCount() {
return 2;
}