本文主要是介绍ListView的进阶-跳转 下拉 上拉 多布局重用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
珍惜作者劳动成果 转载请注明出处
实现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);
事件处理
//实现下拉刷新@Overridepublic 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;}@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {}@Overridepublic 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);}}}
如何进行多布局重用
我们需要重写两个方法
@Overridepublic 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;}}//多少个布局@Overridepublic int getViewTypeCount() {return 2;}
这篇关于ListView的进阶-跳转 下拉 上拉 多布局重用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!