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);

事件处理

 //实现下拉刷新
 @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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值