listviewpage页面滚动

同样是从网络获取数据并用gson解析,导包及权限一个不能少

mainactivity

public class MainActivity extends AppCompatActivity {
    private ListView lv;
    String url="http://169.254.167.244:8080/Web_Data/GetPhones?pagesize=20&pageindex=";
    List<Map<String,String>> list;
    boolean isBottom=false;
    int index=1;
    MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView)findViewById(R.id.lv);
        list=new ArrayList<>();
        adapter=new MyAdapter(MainActivity.this,list);
        lv.setAdapter(adapter);
        getData();
        //listview的滚动监听事件
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {

            /**
             * //滚动状态改变的方法
             //1.AbsListView:ListView的父类
             //2.scrollState:滚动的状态
             *   第1次:scrollState = SCROLL_STATE_TOUCH_SCROLL(1):表示正在滚动。当屏幕滚动且用户使用的触碰或手指还在屏幕上时为1
             第2次:scrollState =SCROLL_STATE_FLING(2) :表示手指做了抛的动作(手指离开屏幕前,用力滑了一下,屏幕产生惯性滑动)。
             第3次:scrollState =SCROLL_STATE_IDLE(0) :表示屏幕已停止。屏幕停止滚动时为0。
             * @param view
             * @param scrollState
             */
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
               //如果屏幕到底,并且滚动已停止才加载新数据
                if(scrollState==SCROLL_STATE_IDLE && isBottom==true){
                    index++;//下一页
                    getData();

                }
            }
            //滚动时候执行的方法
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if(firstVisibleItem+visibleItemCount==totalItemCount){
                    isBottom=true;
                }else {
                    isBottom=false;
                }
            }
        });

    }

    public void getData(){
        new MyAsyncTask(new MyAsyncTask.CallBack() {
            @Override
            public void sendResult(String json) {
                Gson gson=new Gson();
                Phone phone = gson.fromJson(json, Phone.class);
                for (int i=0;i<phone.getPhones().size();i++){
                    Map<String,String> map=new HashMap<String, String>();
                    map.put("id",String.valueOf(phone.getPhones().get(i).getId()));
                    map.put("brand",phone.getPhones().get(i).getBrand());
                    list.add(map);
                }
                adapter.notifyDataSetChanged();
            }
        }).execute(url+index);
    }
}
适配器

public class MyAdapter extends BaseAdapter {

    Context context;
    List<Map<String,String>> list;
    public MyAdapter(Context context, List<Map<String,String>> list){
        this.context=context;
        this.list=list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Viewholder vh;
        if(convertView==null){
            convertView=View.inflate(context,R.layout.list_item,null);
            vh=new Viewholder();
            vh.tv_id=(TextView) convertView.findViewById(R.id.tv_id);
            vh.tv_brand=(TextView)convertView.findViewById(R.id.tv_brand);
            convertView.setTag(vh);
        }else {
            vh=(Viewholder)convertView.getTag();
        }
        vh.tv_id.setText(list.get(position).get("id"));
        vh.tv_brand.setText(list.get(position).get("brand"));
        return convertView;
    }

    class Viewholder{
        TextView tv_id;
        TextView tv_brand;
    }
}
异步加载

public class MyAsyncTask extends AsyncTask<String,Void,String> {
    //Context context;
    CallBack callBack;
    public MyAsyncTask(CallBack callBack){
        //this.context=context;
        this.callBack=callBack;
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            URL url=new URL(params[0]);
            HttpURLConnection huc=(HttpURLConnection) url.openConnection();
            huc.setConnectTimeout(3000);
            huc.connect();
            if(huc.getResponseCode()==200){
                InputStream inputStream = huc.getInputStream();
                byte[] b=new byte[1024];
                int len=0;
                StringBuilder sb=new StringBuilder();
                while ((len=inputStream.read(b))!=-1){
                    sb.append(new String(b,0,len));
                }
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        callBack.sendResult(s);
    }

    interface CallBack{
        void sendResult(String json);
    }
}
还有实体类,利用json快速生成即可



activity布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="org.frxm.day14_listviewpage.MainActivity">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

list_item布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_id"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:gravity="center"/>
    <TextView
        android:id="@+id/tv_brand"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_weight="1"
        android:gravity="center"/>
</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值