• Android_listview_scrollListener


    layout.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <ListView 
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ></ListView>
    
    </RelativeLayout>

    listview_item_name.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" >
        <TextView 
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"/>
    
    </LinearLayout>

    main.java

    package com.example.day09_listview_scrolllistener;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AbsListView;
    import android.widget.AbsListView.OnScrollListener;
    import android.widget.BaseAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
        protected static final String TAG = "MainActivity";
        private ListView listView;
        private List<String> data;
        private boolean isBottom = false;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView = (ListView) findViewById(R.id.listView);
            data = getData();
            MyAdapter myAdapter = new MyAdapter();
            //设置适配器
            listView.setAdapter(myAdapter);
            //设置滚动监听
            listView.setOnScrollListener(new OnScrollListener() {
                /**
                 * 当滚动状态改变的时候调用
                 * AbsListView view, listView的父类 
                 * int scrollState 滚动状态
                 */
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
    //                SCROLL_STATE_FLING 表示惯性滑动--2
    //                SCROLL_STATE_IDLE   表示停止滑动--0
    //                SCROLL_STATE_TOUCH_SCROLL 表示正在滑动--1
    //                Log.i(TAG, "onScrollStateChanged----scrollState"+scrollState);
                    //1.滑动到屏幕最底部2.停止滚动
                    if(scrollState == OnScrollListener.SCROLL_STATE_IDLE && isBottom ==true){
                        Log.i(TAG, isBottom+"");
                        
                    }
                }
                /**
                 * 当listView滚动的时候调用,第一次加载时也会调用
                 * int firstVisibleItem, 表示当前屏幕显示的第一条数据索引
                 * int visibleItemCount, 表示当前屏幕显示的总数量
                 *    int totalItemCount 表示的是数据的总数量
                 */
                @Override
                public void onScroll(AbsListView view, int firstVisibleItem,
                        int visibleItemCount, int totalItemCount) {
                    Log.i(TAG, "onScroll----firstVisibleItem-"+firstVisibleItem+"---visibleItemCount-"+visibleItemCount+"---totalItemCount"+totalItemCount);
                    isBottom = (firstVisibleItem + visibleItemCount == totalItemCount);
                }
            });
        }
        private List<String> getData(){
            List<String> list = new ArrayList<String>();
            for (int i = 0; i < 100; i++) {
                list.add("杭州----"+i);
            }
            return list;
            
        }
        class MyAdapter extends BaseAdapter{
    
            @Override
            public int getCount() {
                return data.size();
            }
    
            @Override
            public Object getItem(int position) {
                return data.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = convertView;
                ViewHolder viewHolder;
                if(view == null){
                    view = View.inflate(MainActivity.this, R.layout.listview_item_name,null);            
                    viewHolder = new ViewHolder();
                    viewHolder.name = (TextView)view.findViewById(R.id.tv_name);
                    view.setTag(viewHolder);
                }else{
                    viewHolder = (ViewHolder) view.getTag();
                }
                viewHolder.name.setText(data.get(position));
            return view;
            }
        
        }
        class ViewHolder{
            private TextView name;
        }
    
    }

  • 相关阅读:
    kafka源码学习笔记
    spring学习笔记
    logback pattern配置详解
    hive笔记
    hbase笔记
    打监控的坑
    Consul1 在window7安装
    springboot1.4下hystrix dashboard Unable to connect to Command Metric Stream解决办法
    com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: fallback method wasn't found: serviceError([class java.lang.String]) 异常
    Keepalived 集群在Linux下的搭建
  • 原文地址:https://www.cnblogs.com/fangg/p/5743524.html
Copyright © 2020-2023  润新知