簡體   English   中英

ListView-滾動加載更多項目

[英]ListView - Load more items with scroll

我需要片段中使用的Listview的幫助。 該應用程序從API讀取數據,我的代碼運行正常。 最初我加載15個項目,下次每次加載10個項目。 但是,如果對API的請求返回的內容少於15個,則該請求不起作用。 此外,當項目數不是15或25或35的倍數時,應用程序也會失敗。這是因為我在初始設置后加載了10個項目。

我需要修改我的代碼才能使其與任何數量的列表項一起使用。

我的代碼如下:

(ListaFragment.java)->這是ListFragment

    public class ListaFragment extends ListFragment implements OnScrollListener {
    public ListaFragment(){}
    View rootView;

    ListAdapter customAdapter = null;
    ListaLugares listaLugares;    

    // values to pagination
    private View mFooterView;
    private final int AUTOLOAD_THRESHOLD = 4;
    private int MAXIMUM_ITEMS;
    private Handler mHandler;
    private boolean mIsLoading = false;
    private boolean mMoreDataAvailable = true;
    private boolean mWasLoading = false;

    public int ventana = 0;
    public int nInitial;

    private Runnable mAddItemsRunnable = new Runnable() {
        @Override
        public void run() {
            customAdapter.addMoreItems(10);
            mIsLoading = false;

        }
    };


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_lista, container, false);

        if (container != null) {
            container.removeAllViews();
        }

        ((MainActivity) getActivity()).setBar();


        // read number of places of a category
        listaLugares.readNumberOfPlaces();
        // set
        setNumbersOfItems();
        // read the places a insert in a List
        listaLugares.readPlaces(15, ventana, listaLugares.idCategoria);
        //ventana = ventana + 25;

        return rootView;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listaLugares = ((ListaLugares)getActivity().getApplicationContext());
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final Context context = getActivity();
        mHandler = new Handler();
        customAdapter = new ListAdapter(context, listaLugares.lista);

        mFooterView = LayoutInflater.from(context).inflate(R.layout.loading_view, null);
        getListView().addFooterView(mFooterView, null, false);
        setListAdapter(customAdapter);
        getListView().setOnScrollListener(this);
    }


    public void setNumbersOfItems() {
        if (listaLugares.totalItems > 100) {
            MAXIMUM_ITEMS = 100;
            nInitial = 25;
        } else {
            MAXIMUM_ITEMS = listaLugares.totalItems;
            nInitial = listaLugares.totalItems;
        }
        Log.v("NUMBER OF ITEMS", "Number: " + MAXIMUM_ITEMS + "-"+ nInitial);
    }



    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        //Intent myIntent = new Intent(getActivity(), DetalleActivity.class);
        //myIntent.putExtra("param_id", appState.lista.get(position).id);
        //getActivity().startActivity(myIntent); 
    }


    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {

         if (!mIsLoading && mMoreDataAvailable) {
             if (totalItemCount >= MAXIMUM_ITEMS) {
                 mMoreDataAvailable = false;
                 getListView().removeFooterView(mFooterView);
             } else if (totalItemCount - AUTOLOAD_THRESHOLD <= firstVisibleItem + visibleItemCount) {
                 ventana = ventana + 10;
                 listaLugares.readPlaces(10, ventana, listaLugares.idCategoria);
                 mIsLoading = true;
                     mHandler.postDelayed(mAddItemsRunnable, 1000);
             }
         }
    }


    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // Ignore
    }


    @Override
    public void onStart() {
        super.onStart();
        if (mWasLoading) {
            mWasLoading = false;
            mIsLoading = true;
            mHandler.postDelayed(mAddItemsRunnable, 1000);
        }
    }


    @Override
    public void onStop() {
        super.onStop();
        mHandler.removeCallbacks(mAddItemsRunnable);
        mWasLoading = mIsLoading;
        mIsLoading = false;
        ventana = ventana;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

(ListAdapter.java)->適配器類中的方法以添加更多項。

    public class ListAdapter extends ArrayAdapter<Lugar> {
Context context;
List<Lugar> values;

ListaLugares listaLugares;

private int mCount = 20;


public ListAdapter(Context context, List<Lugar> values) {
    super(context, R.layout.row, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row = inflater.inflate(R.layout.row, parent, false);
    TextView firstText = (TextView) row.findViewById(R.id.titulo);
    TextView secondText = (TextView) row.findViewById(R.id.categoria);

    firstText.setText(values.get(position).nombre);
    secondText.setText(values.get(position).categoriaNombre);

    return row;
}


public void addMoreItems(int count) {
    mCount += count;
    notifyDataSetChanged();
}

@Override
public int getCount() {
    return mCount;
}


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

}

我加入了先前的編輯版本以查看一些代碼。

第一次調用是有效的,因為您要在mCount上以15 initiaItems並在ListAdapter構造函數的mCount上進行ListAdapter

public ListAdapter(Context context, List<Lugar> values, int count) {
    super(context, R.layout.row, values);
    this.context = context;
    this.values = values;
    this.mCount = count;
}

因此,當android渲染列表視圖時,它會訪問函數ListAdapter.getCount()並返回mCount (15)。

但是當您滾動時,它可以調用

public void addMoreItems(int count, int idCategoria) {
    appState = ((ListaLugares)getContext().getApplicationContext());
    appState.readPlaces(15, mCount, idCategoria);
    mCount += count;
    notifyDataSetChanged();
}

如果未知由appState.readPlaces返回的項目數,為什么要在appState.readPlaces中添加一個數字mCount += count;

如果呈現列表視圖時appState.readPlaces返回14且count為15,則當15 + 14時將假定有15 + 15項,因此最后一項將崩潰。

mCount應該從保存API調用數據的對象獲取長度,在您的情況下,我認為它將是appState.lista

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM