如何在我的列表视图 android 工作室中进行某种延迟加载

How to do some kind of lazy load in my listview android studio

我创建了一个片段,我正在使用 Retrofit 填充其中的列表视图以进行网络连接并获取请求,我创建了自定义适配器来填充列表视图:

public class TransactionsAdapter extends BaseAdapter {
ArrayList<Transactions> transactions;


public TransactionsAdapter(ArrayList<Transactions> transactions) {
    this.transactions=transactions;
}

@Override
public int getCount() {
    if (transactions.size()<20) {
        return transactions.size();
    }else return 20;

}

@Override
public Object getItem(int position) {
    return transactions.get(position);
}
@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view=null;
    ViewHolder viewHolder = null;

    if(convertView == null)
    {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.izvjestaji_item,parent,false);
        viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);

    }
    else{
        view = convertView;
        viewHolder= (ViewHolder) view.getTag();

    }

    Transactions transactions = (Transactions) getItem(position);

    if(transactions != null) {

        viewHolder.datum.setText(transactions.getShortDate());
        viewHolder.partner.setText(transactions.getMerchantName());
        String doublestring=Double.toString(transactions.getTransactionPointsAmount());
        String doublestring1=Double.toString(transactions.getSalesAmount());
        viewHolder.iznos.setText(doublestring1);
        viewHolder.brojbodova.setText(doublestring);
    }

    return view;
}


private class ViewHolder{
    TextView datum;
    TextView partner;
    TextView iznos;
    TextView brojbodova;

    public ViewHolder(View view) {
        this.datum = (TextView) view.findViewById(R.id.izvjestaji_datum);
        this.partner = (TextView) view.findViewById(R.id.izvjestaji_partner);
        this.iznos = (TextView) view.findViewById(R.id.izvjestaji_iznos);
        this.brojbodova=(TextView)view.findViewById(R.id.izvjestaji_brojbodova);
    }
}
}

我已经为项目和列表视图创建了 2 个布局,并且一切正常。现在我需要添加某种延迟加载或更简单的东西,因为服务器有可能 return 大量数据,我想以 20 x 20 的比例显示它们以提高网络效率。 这是我的片段代码:

    public class Izvjestaji extends Fragment {
   // The onCreateView method is called when Fragment should create its View   
   object hierarchy,
 // either dynamically or via XML layout inflation.
 ListView list;
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle   
 savedInstanceState) {
    // Defines the xml file for the fragment


    return inflater.inflate(R.layout.izvjestaji, parent, false);

 }

// This event is triggered soon after onCreateView().
// Any view setup should occur here.  E.g., view lookups and attaching view          
listeners.
@Override
 public void onViewCreated(View view, Bundle savedInstanceState)  {
    list=(ListView)view.findViewById(R.id.izvjestaji_list);
    showList();
    // Setup any handles to view objects here
    // EditText etFoo = (EditText) view.findViewById(R.id.etFoo);

 }
 public void showList(){

    NetworkSDK.getInstance().getTransactions(new     
    Callback<List<Transactions>>() {
        @Override
        public void onResponse(Call<List<Transactions>> call,        
        Response<List<Transactions>> response) {
            if(response.isSuccess()){
                Log.d("Data", String.valueOf(response.isSuccess()));
                TransactionsAdapter transactionsAdapter=new 
        TransactionsAdapter((ArrayList<Transactions>)response.body());
                list.setAdapter(transactionsAdapter);


            }
        }

        @Override
        public void onFailure(Call<List<Transactions>> call, Throwable t) {
            Log.d("Error","Def error");

        }
    });
 }
 }

这是我在改造时从 Network SDK 调用的

    public void getTransactions(Callback<List<Transactions>>callback){
    Call<List<Transactions>>call =                                                                                                           
    BaseClient.getService().getTransactions(4,2016);
            call.enqueue(callback);
}

通过所有这些,我在我的片段中得到了一个列表视图,它显示了我想显示的所有可用交易,但我需要将其限制在大约 20 个,当用户滑动底部时,它需要显示另外 20 个,依此类推。我有一些想法,比如在用户滑动或类似操作时发送一些索引号,但我确信有一些更简单的方法,因为我对 android.

不是很好

我也尝试过 endless adapter 之类的东西,但我不允许使用不太流行的库,比如 retrofit 或 volley,还有这个: https://github.com/commonsguy/cwac-endless 不再提供支持,我什至不确定它是否仍然有效

使用OnScrollListener检测列表是否向下滚动。然后加载数据。

  mListView.setOnScrollListener(new OnScrollListener() {

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

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (mListView.getAdapter() == null)
            return ;

        if (mListView.getAdapter().getCount() == 0)
            return ;

        int l = visibleItemCount + firstVisibleItem;
        if (l >= totalItemCount && !isLoading) {
            // It is time to add new data. We call the listener
            isLoading = true;
            loadYourData();
        }
    }
});

加载数据后不要忘记在适配器中刷新列表。

public void updateInUi() {

    // send your request and get list of updated items
    List<> newItems = getYourUpdatedListHere()
    yourDataList.addAll(newItems);
    adapter.notifyDataSetChanged();
}

另一种检测是否已到达列表末尾的方法是在 AdaptergetView 方法内部:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (position == getCount() - 1) { // scrolled to last item
        if (mRepo.hasMore()) { // if there are more items than we are displaying
            new MyAsyncLoaderTask().execute();
        }
    }
    // usual getView implementation follows...
    return view;
}