Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android列表加載更多數據,並實現點贊

Android列表加載更多數據,並實現點贊

編輯:關於Android編程

對我上一篇文章進行了修改
可以點擊加載更多條數據
並且實現點擊功能
主要修改代碼:

MainActivity

package com.gz.test_listview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity implements AbsListView.OnScrollListener,View.OnClickListener{
    private int visibleLastIndex = 0;   //最後的可視項索引
    private int visibleItemCount;       // 當前窗口可見項總數
    private Button load_more;
    private View footView;
    MainActivityAdapter adapter;

    private ListView listview;
    //測試數據
    private String[] title = {"商用","家用","測試","多聯","空氣能","商用","家用","測試","多聯","空氣能"};
    private String[] praise = {"3","1","0","0","0","3","1","0","0","0"};
    private String[] step = {"0","0","0","1","1","0","0","0","1","0"};

    //將數據傳回adapter中
    private List> map = new ArrayList>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //加載更多控件
        footView = getLayoutInflater().inflate(R.layout.footview,null);
        load_more = (Button) footView.findViewById(R.id.load_more);
        //初始化Adapter
        initAdapter();
        listview.addFooterView(footView);
        //加載更多的點擊事件
        load_more.setOnClickListener(this);
        listview.setOnScrollListener(this);
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this,DetailActivity.class);
                startActivity(intent);
            }
        });
    }

    //初始化Adapter,並為初始化的十條數據賦值
    private void initAdapter() {
//        Log.e("gz/initAdapter","進入initAdapter()方法");
        listview = (ListView) findViewById(R.id.listview);

        for(int i=0;i<10;i++){
            Map item = new HashMap();
            item.put("title",title[i]);
            item.put("praise",praise[i]);
            item.put("step",step[i]);
            map.add(item);
        }
        adapter = new MainActivityAdapter(this,map);
    }

    //加載更多數據每次增加十條數據
    private void loadData() {
//        Log.e("gz/loadData","進入loadData()方法");
        int count = adapter.getCount();
        for(int i=count;i item = new HashMap();
            item.put("title",title[i-count]);
            item.put("praise",praise[i-count]);
            item.put("step",step[i-count]);
            adapter.addItem(item);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
            load_more.setText("加載中");
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    loadData();
                    adapter.notifyDataSetChanged();
                    listview.setSelection(visibleLastIndex);
                    load_more.setText("加載更多");
                }
            },2000);
    }

    //滑動事件
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        int itemsLastIndex = adapter.getCount() - 1;    //數據集最後一項的索引
        int lastIndex = itemsLastIndex + 1;             //加上底部的loadMoreView項
        if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {
            //如果是自動加載,可以在這裡放置異步加載數據的代碼
//            Log.e("gz/onScrollStateChanged", "loading...");
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        this.visibleItemCount = visibleItemCount;
        visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
//        Log.e("gz/onScroll","進入onScroll()");
    }
}

MainAdapter

package com.gz.test_listview;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 390778 on 2016/11/9.
 */
public class MainActivityAdapter extends BaseAdapter{
    private ViewHolder holder;
    private Context context;
    private List praise_step_num = new ArrayList();

    private LayoutInflater inflater;
    List> listItems;
    /*
    根據position,保存點擊的是哪一行數據,默認為false,點擊後改為true
    */
    private Map isExist = new HashMap();

    public MainActivityAdapter(Context context,List> listItems){
        this.context = context;
        this.listItems = listItems;
        inflater = LayoutInflater.from(context);
        Log.i("listItem",listItems.toString());
        init();
    }

    private void init() {
        /*
        為相應的數據賦值,所有的數據存儲在listItems中
        position一一對應每一行數據
         */
        for (int i = 0;i
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved