如何实现无限滚动列表视图

Posted

技术标签:

【中文标题】如何实现无限滚动列表视图【英文标题】:How to implement endless scrolling listview 【发布时间】:2015-09-13 03:56:18 【问题描述】:

我正在创建一个应用程序,我在其中使用 Json 解析来解析数据。我已经实现了 listview,在我的回复中我有 totalpagecurrent page 对象,

新用户将从第一页获取项目。当用户在列表视图中滚动时,列表会随着来自下一页(以及下一个和下一个等)的项目展开。

这是我的回应 http://pastie.org/10259792

以下是我试过的代码:

首页片段

public class HomeFragment extends Fragment 
// Listview Adapter
        ListViewAdapter adapters;

            // Connection detector
            ConnectionDetector cd;

            // Alert dialog manager
            AlertDialogManager alert = new AlertDialogManager();

            // Progress Dialog
            private ProgressDialog pDialog;

            // Creating JSON Parser object
            JSONparserr jsonParser = new JSONparserr();

            ArrayList<HashMap<String, String>> WebsiteList;

            // albums JSONArray
            JSONArray data = null;
            String link_url;

            ArrayList<HashMap<String, String>> arrayTemplist;

            private AutoCompleteTextView inputSearch;

            private ListView lv;

            private static final String URL_ALBUMS = "";

            private static final String WEBSITE_MENU_ID ="Brand_name";

            private static final String TAG_MATCH="data";
            //private static final String TAG_NAME="Brand_name";
            private static final String TAG_PROFILE="id";
            private static final String TAG_CAST="Company";

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

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

    lv = (ListView)rootView.findViewById(R.id.listpehlu);


    cd = new ConnectionDetector(getActivity());


    WebsiteList = new ArrayList<HashMap<String, String>>();

 // Loading Albums JSON in Background Thread
 new LoadAlbums().execute();

    // get listview

    inputSearch = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextViewhomefrag);

    inputSearch.addTextChangedListener(new TextWatcher() 


        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) 

            arrayTemplist= new ArrayList<HashMap<String,String>>();
            String searchString =inputSearch.getText().toString().toLowerCase();

            for (int i = 0; i < WebsiteList.size(); i++)
            
                String currentString =WebsiteList.get(i).get(HomeFragment.this.WEBSITE_MENU_ID);
                if (currentString.toLowerCase().startsWith(searchString ))
                
                    arrayTemplist.add(WebsiteList.get(i));
                
            

            for (int i = 0; i < arrayTemplist.size(); i++)
            
                String currentstrin = arrayTemplist.get(i).get(HomeFragment.this.WEBSITE_MENU_ID);
                //Toast.makeText(getApplicationContext(), currentstrin, Toast.LENGTH_LONG).show();

            
      SimpleAdapter adapters = new SimpleAdapter(getActivity(), arrayTemplist,R.layout.list_item_match, new String[] WEBSITE_MENU_ID,TAG_CAST
            , new int[] 
                    R.id.txtbrndnm,R.id.txtbrndcomp);
            lv.setAdapter(adapters);

        

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) 
            // TODO Auto-generated method stub

        

        @Override
        public void afterTextChanged(Editable arg0) 
            // TODO Auto-generated method stub                         
        
    );


    lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() 
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,
                long arg3) 


            Selected_Product_Listing tf = new Selected_Product_Listing();
             Bundle bundle = new Bundle();
             bundle.putString("prducts_name", WebsiteList.get(position).get((WEBSITE_MENU_ID)));
            // bundle.putString("prducts_name", aList.get(position).get(INTEREST_ACCEPT_NAME));
             tf.setArguments(bundle);
              FragmentManager fm = getFragmentManager();
              FragmentTransaction ft = fm.beginTransaction();
              ft.replace(R.id.frame_container, tf);
              ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
              ft.addToBackStack(null);
              ft.commit();

            // Toast.makeText(getActivity(),"lets"+ WebsiteList.get(position).get(convertToBase64(convertToBase64(WEBSITE_MENU_ID))), Toast.LENGTH_LONG).show();

        
    );   

    lv.setOnScrollListener(new OnScrollListener() 

        public void onScrollStateChanged(AbsListView view, int scrollState) 


        

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

        if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
        

        
        
        );



    return rootView;


public String convertToBase64(String text) 
    byte[] data = null;
    try 
        data = text.getBytes("UTF-8");
     catch (UnsupportedEncodingException e) 
        e.printStackTrace();
    
    return Base64.encodeToString(data, Base64.DEFAULT);


class LoadAlbums extends AsyncTask<String, String, String> 

    private JSONObject jsonObj;


    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() 
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Loading...");
        pDialog.setIndeterminate(true);
        pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));

        pDialog.setCancelable(false);
        pDialog.show();
    
    protected String doInBackground(String... args) 
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        // getting JSON string from URL
        String json = jsonParser.makeHttpRequest(URL_ALBUMS, "GET",
                params);

        // Check your log cat for JSON reponse
        Log.d("Albums JSON: ", "> " + json);
        if (json != null) 
            try 
                jsonObj = new JSONObject(json);
                // Getting JSON Array node
                data = jsonObj.getJSONArray(TAG_MATCH);

                String totpage=jsonObj.getString("total_page");
                System.out.println("Total Page"+totpage);

                String curpage=jsonObj.getString("current_page");
                System.out.println("Total Page"+curpage);



                // looping through All Contacts
                for (int i = 0; i < data.length(); i++) 
                    JSONObject c = data.getJSONObject(i);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(WEBSITE_MENU_ID,c.getString(WEBSITE_MENU_ID));
                 map.put(TAG_PROFILE, c.getString(TAG_PROFILE));
                    map.put(TAG_CAST, c.getString(TAG_CAST));

                    WebsiteList.add(map);
                
             catch (JSONException e) 
                e.printStackTrace();
            
         else 
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        
       return null;
    


    protected void onPostExecute(String file_url) 
        // dismiss the dialog after getting all albums
        pDialog.dismiss();
        // updating UI from Background Thread

                ListAdapter adapter = new SimpleAdapter(
                        getActivity(), WebsiteList,
                        R.layout.list_item_match, new String[] WEBSITE_MENU_ID,TAG_CAST
                         , new int[] 
                                R.id.txtbrndnm,R.id.txtbrndcomp);


               lv.setAdapter(adapter); 

            


    

无尽适配器

 public class EndLessAdapter extends ArrayAdapter<String> 

private Context context;
private List<String> items;
private int layoutId;

public EndLessAdapter(Context context, List<String> items, int LayoutId) 
    super(context,LayoutId,items);
    this.context = context;
    this.items = items;
    this.layoutId = LayoutId;


@Override
public View getView(int position, View convertView, ViewGroup parent) 

    if(convertView == null)
        convertView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(layoutId,parent,false);

    TextView tView = (TextView)convertView.findViewById(R.id.txt);
    tView.setText(items.get(position));

    return convertView;

 

EndlessLisView

public class EndlessListView extends ListView implements OnScrollListener 

private Context context;
private View footer;
private boolean isLoading;
private EndLessListener listener;

private BaseAdapter  adapter;

public EndlessListView(Context context, AttributeSet attrs, int defStyle) 
    super(context, attrs, defStyle);        
    this.setOnScrollListener(this);

    this.context = context;


public EndlessListView(Context context, AttributeSet attrs) 
    super(context, attrs);
    this.setOnScrollListener(this);

    this.context = context;


public EndlessListView(Context context) 
    super(context);     
    this.setOnScrollListener(this);

    this.context = context;


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

   

//  4
public void addNewData() 
    this.removeFooterView(footer);
   //       adapter.addAll(products);

    adapter.notifyDataSetChanged();
    isLoading = false;

public void setLoading()

    isLoading = false;


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

    if(getAdapter() == null)
        return;

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

    int l = visibleItemCount + firstVisibleItem;
    System.out.println("List View Total Item Count = "+totalItemCount+" , l = "+l+", is_loading = "+isLoading);
    if(l >= totalItemCount && !isLoading)

        //  add footer layout
        this.addFooterView(footer);

        //  set progress boolean
        isLoading = true;
        System.out.println("$$$$$$$$$ call loaddata $$$$$$$$$$$");
        //  call interface method to load new data
        listener.loadData();
    


//  Calling order from MainActivity
//  1
public void setLoadingView(int resId) 

    footer = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(resId, null);      //      footer = (View)inflater.inflate(resId, null);
    this.addFooterView(footer);
    System.out.println("addfooter ====@@@@@@");
   

//  2
public void setListener(EndLessListener listener) 
    this.listener = listener;
   

//  3
public void setAdapter(BaseAdapter adapter)        

    super.setAdapter(adapter);
    this.adapter = adapter;

    this.removeFooterView(footer);


public void removeFooter()
    this.removeFooterView(footer);

【问题讨论】:

那么,你的问题是什么?你遇到了什么错误? 在此处出现错误应用程序崩溃 listener.loadData(); 查看 ViewHolder 模式 @EpicPandaForce 我想得到你 @Roman 您尝试实现的内容称为延迟加载或分页。您能否发布处理此问题的整个代码? 【参考方案1】:

您可以通过使用 Github 中的以下库来实现此目的

https://github.com/nicolasjafelle/PagingListView

简单地创建您的PagingAdapter 并将其添加到com.paging.listview.PagingListView。 您必须实现新的 Pagingable 接口及其onLoadMoreItems() 方法。例如:

listView.setHasMoreItems(true);
listView.setPagingableListener(new PagingListView.Pagingable() 
    @Override
    public void onLoadMoreItems() 
        if (pager < 3) 
            new CountryAsyncTask(false).execute();
         else 
            listView.onFinishLoading(false, null);
        
    
);

终于可以使用onFinishLoading(boolean hasMoreItems, List newItems)方法更新列表了。

listView.onFinishLoading(true, newItems);

还记得在你的布局文件中使用这个包:

<com.paging.listview.PagingListView
    android:id="@+id/paging_list_view"
    android:layout_
    android:layout_/>

【讨论】:

你能告诉***.com/questions/31847086/… @roman :- 你可以看看答案***.com/questions/31847086/…【参考方案2】:

你好在这里你可以找到延迟加载的nostra库然后你可以 实现以下惰性加载示例

https://github.com/nostra13/Android-Universal-Image-Loader

你可以使用下面的实现延迟加载

代码

让我们开始编写关于延迟加载的代码。

MainActivity.java


    package com.sunil.lazyloading;


    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;


public class MainActivity extends Activity implements OnClickListener

    private Button btnlist=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnlist= (Button)findViewById(R.id.button_listview);
        btnlist.setOnClickListener(this);
    
    @Override
    public void onClick(View arg0) 

        Intent intent = new Intent(this, ImageListActivity.class);
        intent.putExtra("stringarrayimage", Constants.IMAGES);
        startActivity(intent);

    


CustomAdapter.java


    package com.sunil.adapter;

    import java.util.Collections;
    import java.util.LinkedList;
    import java.util.List;

    import com.nostra13.universalimageloader.core.DisplayImageOptions;
    import com.nostra13.universalimageloader.core.ImageLoader;
    import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
    import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
    import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
    import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
    import com.sunil.lazyloading.R;

    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;



public class CustomAdapter extends BaseAdapter

    private String imageurl[]=null;
    private Context context=null;
    DisplayImageOptions doption=null;
    private ImageLoadingListener animateFirstListener =null;


    public CustomAdapter(Activity activity, String[] imageurl)
    
        this.context=activity;
        this.imageurl=imageurl;
        doption=new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.ic_empty).showImageOnFail(R.drawable.ic_error).showStubImage(R.drawable.ic_stub).cacheInMemory(true).cacheOnDisc(true).displayer(new RoundedBitmapDisplayer(20)).build();
        animateFirstListener = new AnimateFirstDisplayListener();
    

    @Override
    public int getCount() 
        return imageurl.length;
    

    @Override
    public Object getItem(int arg0) 
        return arg0;
    

    @Override
    public long getItemId(int arg0) 
        return arg0;
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 

        View view = convertView;
        final ViewHolder holder;

        if (convertView == null) 
            view = ((Activity) context).getLayoutInflater().inflate(R.layout.item_list_row, parent, false);
            holder = new ViewHolder();
            holder.text = (TextView) view.findViewById(R.id.text);
            holder.image = (ImageView) view.findViewById(R.id.image);
            view.setTag(holder);
         else 
            holder = (ViewHolder) view.getTag();
        

        holder.text.setText("Item " + (position + 1));
        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.displayImage(imageurl[position], holder.image, doption, animateFirstListener);

        return view;
    

    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener 

        static final List<string> displayedImages = Collections.synchronizedList(new LinkedList<string>());

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) 
            if (loadedImage != null) 
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) 
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                
            
        
    

    private class ViewHolder 
        public TextView text;
        public ImageView image;
    

</string></string>
ImageListActivity.java

    package com.sunil.lazyloading;



        import java.util.Collections;
        import java.util.LinkedList;
        import java.util.List;

        import android.app.Activity;
        import android.content.Intent;
        import android.graphics.Bitmap;
        import android.os.Bundle;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;
        import android.widget.AdapterView;
        import android.widget.AdapterView.OnItemClickListener;
        import android.widget.ImageView;
        import android.widget.ListView;

        import com.nostra13.universalimageloader.core.ImageLoader;
        import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
MainActivity.java


    package com.sunil.lazyloading;


    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;


public class MainActivity extends Activity implements OnClickListener

    private Button btnlist=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnlist= (Button)findViewById(R.id.button_listview);
        btnlist.setOnClickListener(this);
    
    @Override
    public void onClick(View arg0) 

        Intent intent = new Intent(this, ImageListActivity.class);
        intent.putExtra("stringarrayimage", Constants.IMAGES);
        startActivity(intent);

    


CustomAdapter.java


    package com.sunil.adapter;

    import java.util.Collections;
    import java.util.LinkedList;
    import java.util.List;

    import com.nostra13.universalimageloader.core.DisplayImageOptions;
    import com.nostra13.universalimageloader.core.ImageLoader;
    import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
    import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
    import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
    import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
    import com.sunil.lazyloading.R;

    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;



public class CustomAdapter extends BaseAdapter

    private String imageurl[]=null;
    private Context context=null;
    DisplayImageOptions doption=null;
    private ImageLoadingListener animateFirstListener =null;


    public CustomAdapter(Activity activity, String[] imageurl)
    
        this.context=activity;
        this.imageurl=imageurl;
        doption=new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.ic_empty).showImageOnFail(R.drawable.ic_error).showStubImage(R.drawable.ic_stub).cacheInMemory(true).cacheOnDisc(true).displayer(new RoundedBitmapDisplayer(20)).build();
        animateFirstListener = new AnimateFirstDisplayListener();
    

    @Override
    public int getCount() 
        return imageurl.length;
    

    @Override
    public Object getItem(int arg0) 
        return arg0;
    

    @Override
    public long getItemId(int arg0) 
        return arg0;
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 

        View view = convertView;
        final ViewHolder holder;

        if (convertView == null) 
            view = ((Activity) context).getLayoutInflater().inflate(R.layout.item_list_row, parent, false);
            holder = new ViewHolder();
            holder.text = (TextView) view.findViewById(R.id.text);
            holder.image = (ImageView) view.findViewById(R.id.image);
            view.setTag(holder);
         else 
            holder = (ViewHolder) view.getTag();
        

        holder.text.setText("Item " + (position + 1));
        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.displayImage(imageurl[position], holder.image, doption, animateFirstListener);

        return view;
    

    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener 

        static final List<string> displayedImages = Collections.synchronizedList(new LinkedList<string>());

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) 
            if (loadedImage != null) 
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) 
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                
            
        
    

    private class ViewHolder 
        public TextView text;
        public ImageView image;
    

</string></string>
ImageListActivity.java

    package com.sunil.lazyloading;



        import java.util.Collections;
        import java.util.LinkedList;
        import java.util.List;

        import android.app.Activity;
        import android.content.Intent;
        import android.graphics.Bitmap;
        import android.os.Bundle;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;
        import android.widget.AdapterView;
        import android.widget.AdapterView.OnItemClickListener;
        import android.widget.ImageView;
        import android.widget.ListView;

        import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
        import com.sunil.adapter.CustomAdapter;


public class ImageListActivity extends Activity implements OnItemClickListener

    private ListView listview=null;
    private String[] imageUrls;
    protected ImageLoader imageLoader=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imagelist);

        listview=(ListView)findViewById(R.id.listView_image);
        imageLoader = ImageLoader.getInstance();
        Bundle bundle = getIntent().getExtras();
        imageUrls = bundle.getStringArray("stringarrayimage");
        CustomAdapter adapter=new CustomAdapter(ImageListActivity.this, imageUrls);
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(this);

    
    @Override
    public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) 
        Intent intent = new Intent(this, ImagePagerActivity.class);
        intent.putExtra("imageurlpostion", imageUrls);
        intent.putExtra("imagepostion", position);
        startActivity(intent);

    

    @Override
    public void onBackPressed() 
        AnimateFirstDisplayListener.displayedImages.clear();
        super.onBackPressed();
    


    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener 

        static final List<string> displayedImages = Collections.synchronizedList(new LinkedList<string>());

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) 
            if (loadedImage != null) 
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) 
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                
            
        
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        switch (item.getItemId()) 
            case R.id.item_clear_memory_cache:
                imageLoader.clearMemoryCache();
                return true;
            case R.id.item_clear_disc_cache:
                imageLoader.clearDiscCache();
                return true;
            default:
                return false;
        
    

activity_main.xml

<button android:id="@+id/button_listview" android:layout_ android:layout_ android:text="ListView ImageLoad">

imagelist.xml

<listview android:id="@+id/listView_image" android:layout_ android:layout_>
</listview>

item_list_row.xml

<imageview android:adjustviewbounds="true" android:contentdescription="@string/descr_image" android:id="@+id/image" android:layout_ android:layout_margin="3dip" android:layout_ android:scaletype="centerCrop">

<textview android:id="@+id/text" android:layout_gravity="left|center_vertical" android:layout_ android:layout_marginleft="20dip" android:layout_ android:textsize="22sp">

对于上面的示例,您需要下载库通用图像加载器,您可以下载库...快乐编码:)

【讨论】:

【参考方案3】:

您没有在 Fragment 中使用 EndlessListView。当我查看您的回复时,它包括分页。因此,您需要使用页码请求才能使用无限滚动。

这只是 Endless Listview 的流程。如果您的代码仍然无法运行,希望您在阅读此消息后能够学习示例项目。我只添加了你需要的部分。谢谢

无限滚动仅在列表视图上显示有限的数据。当用户向下滚动到底部时,它将从服务器获取下一个有限的数据。因此,您需要在请求中添加 current_page 参数。我不确切知道你的api。

首先,你需要用 0 初始化的 current_page 变量

您必须知道何时停止。您的回复有 total_page 字段。保存并添加验证

然后您需要知道何时请求下一页。为此,您已经在 EndlessListView 的 onScroll 中有代码。该代码计算并告诉用户何时向下滚动到底部并调用

listener.loadData();

请求新数据。但是您仍然需要为此添加侦听器。你已经有了

public void setListener(EndLessListener listener) 
    this.listener = listener;
 

您需要创建名为 EndlessListenerInterface 并在您的片段中实现。 EndlessListener 接口将包含向服务器请求的 loadData() 函数。之后,您需要为列表视图添加侦听器。

endlessListView.setListener(this);

【讨论】:

我正在尝试的是......默认情况下,我正在获取当前页面 0 的数据,当用户滚动时......它应该加载当前页面 1 的数据 是的,请求成功后需要增加页面。 哪个聊​​天室?可以帮忙。 在***中 @Roman,我认为这篇文章回答了你的一些问题。 EndlessListView 中已经有了 OnScroll 侦听器。我认为这是一个容易解决的问题。如果您决定使用另一个库,正如另一个建议的那样,它可能运行良好,您可能需要重新设计一些代码。【参考方案4】:

当列表视图结束时尝试加载数据。在您的片段中实现 OnScrollListener。参考这个链接Android - ListView to load more items when reached end

【讨论】:

在 info.androidhive.slidingmenu.endless.EndlessListView.onScroll(EndlessListView.java:81) 处获得空指针异常【参考方案5】:

必须使用Paging 3 library 以最简单的方式解决所有问题。

【讨论】:

【参考方案6】:

ListView XMl:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical" >
    <ListView
        android:id="@+id/orderlistview"
        android:layout_
        android:layout_
        android:layout_gravity="center"
        />

</LinearLayout>

OrderList 类

public class OrderList 

    int _orderId;
    int _orderincrement_id;
    String _orderstatus;
    String _orderdate;
    String _ordership_to;
    String _ordertotal;
    String _ordercurrency_symbol;

    // Empty constructor
    public OrderList() 

    

    // constructor
    public OrderList(int _orderId, int _orderincrement_id, String _orderstatus,
            String _orderdate, String _ordership_to, String _ordertotal,
            String _ordercurrency_symbol) 
        this._orderId = _orderId;
        this._orderincrement_id = _orderincrement_id;
        this._orderstatus = _orderstatus;
        this._orderdate = _orderdate;
        this._ordership_to = _ordership_to;
        this._ordertotal = _ordertotal;
        this._ordercurrency_symbol = _ordercurrency_symbol;

    

    public int get_orderId() 
        return _orderId;
    

    public void set_orderId(int _orderId) 
        this._orderId = _orderId;
    

    public int get_orderincrement_id() 
        return _orderincrement_id;
    

    public void set_orderincrement_id(int _orderincrement_id) 
        this._orderincrement_id = _orderincrement_id;
    

    public String get_orderstatus() 
        return _orderstatus;
    

    public void set_orderstatus(String _orderstatus) 
        this._orderstatus = _orderstatus;
    

    public String get_orderdate() 
        return _orderdate;
    

    public void set_orderdate(String _orderdate) 
        this._orderdate = _orderdate;
    

    public String get_ordership_to() 
        return _ordership_to;
    

    public void set_ordership_to(String _ordership_to) 
        this._ordership_to = _ordership_to;
    

    public String get_ordertotal() 
        return _ordertotal;
    

    public void set_ordertotal(String _ordertotal) 
        this._ordertotal = _ordertotal;
    

    public String get_ordercurrency_symbol() 
        return _ordercurrency_symbol;
    

    public void set_ordercurrency_symbol(String _ordercurrency_symbol) 
        this._ordercurrency_symbol = _ordercurrency_symbol;
    


活动

public class OrderListFragment extends Fragment 

    View rootView;
    ListView lv;
    List<OrderList> list ;
    MyListAdapter adt;
    DatabaseHandler db ;
    ProgressDialog progress;
    SharedPreferences pref;

    public OrderListFragment() 
    

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

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

        if (android.os.Build.VERSION.SDK_INT > 9) 
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
        

        lv = (ListView) rootView.findViewById(R.id.orderlistview);


        db = new DatabaseHandler(getActivity());
        list = db.getAllOrderList();
        if(list.size() == 0)
            progress = new ProgressDialog(getActivity());
            progress.setMessage("Fetching Data.....");
            progress.setIndeterminate(true);
            progress.setCancelable(false);
            progress.show();

            pref = PreferenceManager.getDefaultSharedPreferences(getActivity());

            String customer_id = pref.getString("customer_id", null);

            Log.v("log_tag", "customer_id"+customer_id);
            if(customer_id != null)
                Log.v("log_tag", "customer_id 2332 ::: "+customer_id);
                new FetchOrderListData().execute(customer_id);
            
        
        adt = new MyListAdapter(getActivity());
        lv.setAdapter(adt);

        lv.setOnItemClickListener(new OnItemClickListener() 

                @Override
                public void onItemClick(AdapterView<?> a, View v, int position,
                        long id) 

                    Bundle bundle = new Bundle();
                    bundle.putString("orderlist_increment_id", list.get(position).get_orderincrement_id()+"");
                    Fragment fragment = new OrderdetailFragment();
                    fragment.setArguments(bundle);
                    FragmentManager fragmentManager = OrderListFragment.this
                            .getActivity().getSupportFragmentManager();
                    FragmentTransaction mFragmentTransaction = fragmentManager
                            .beginTransaction();
                    mFragmentTransaction.addToBackStack(null);
                    mFragmentTransaction.replace(R.id.Frame_Layout, fragment)
                            .commit();

                
            );

        return rootView;
    

    public class MyListAdapter extends BaseAdapter 
        private LayoutInflater mInflater;

        public MyListAdapter(Context context) 
            mInflater = LayoutInflater.from(context);

        

        public int getCount() 
            return list.size();
        

        public Object getItem(int position) 
            return position;
        

        public long getItemId(int position) 
            return position;
        

        public View getView(final int position, View convertView,
                ViewGroup parent) 
            convertView = mInflater.inflate(R.layout.custom_orderlistdata,
                    null);
            TextView ordernumbertxt = (TextView) convertView
                    .findViewById(R.id.ordernumberTxt);
            TextView orderdate = (TextView) convertView
                    .findViewById(R.id.orderdateTxt);
            TextView orderamount = (TextView) convertView
                    .findViewById(R.id.orderAmountTxt);
            TextView orderSatusTxt = (TextView) convertView
                    .findViewById(R.id.orderSatusTxt);

            ordernumbertxt.setText(html.fromHtml("<b>Order Number : </b>"+list.get(position).get_orderincrement_id()+""));
            orderdate.setText(list.get(position).get_orderdate());
            orderSatusTxt.setText(list.get(position).get_orderstatus());
            String parts = list.get(position).get_ordertotal(); 

            double d = Double.parseDouble(parts);
            double dval = roundTwoDecimals(d);
            orderamount.setText(Html.fromHtml("<b>Total  : </b>"+list.get(position).get_ordercurrency_symbol()+" "+dval));

            return convertView;
        

         public double roundTwoDecimals(double d) 
              DecimalFormat twoDForm = new DecimalFormat("#.##");
              return Double.valueOf(twoDForm.format(d));
            
    

    private class FetchOrderListData extends AsyncTask<String, Void, JSONObject> 
        @Override
        protected void onPreExecute() 

        

        @Override
        protected JSONObject doInBackground(String... args) 
            JSONObject listSize = null;

            Integer cust_id = Integer.parseInt(args[0]);
            try 
                listSize = DBAda.getAllOrderListData(cust_id);
                ////*** call your HTTP Service over here ***////
             catch (JSONException e) 
                // TODO Auto-generated catch block
                e.printStackTrace();
            

            return listSize;
        

        protected void onPostExecute(JSONObject jsonObj) 
            db = new DatabaseHandler(getActivity());
            if (progress != null) 
                progress.hide();
            
            if (jsonObj == null) 

                return;
            



            JSONArray json;

            try 
                json = jsonObj.getJSONArray("records");

                for (int i = 0; i < json.length(); i++) 
                    JSONObject jobj;
                    try 
                        jobj = json.getJSONObject(i);
                        int order_id = Integer.parseInt(jobj.getString("order_id").toString());
                        int order_IncrementId = Integer.parseInt(jobj.getString("increment_id").toString());
                        String orderStatus = jobj.getString("status");
                        String orderdate = jobj.getString("date");
                        String ordershipto = jobj.getString("ship_to");
                        String ordertotal = jobj.getString("order_total");
                        String ordersymbol = jobj.getString("currency_symbol");

                        //db.addWishlistItem(new WishList(entity_id,quentity,comment,name,short_description,thumbnails,itemId));

                        db.addOrderListItem(new OrderList(order_id, order_IncrementId, orderStatus, orderdate, ordershipto, ordertotal, ordersymbol));



                     catch (JSONException e) 
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    
                
             catch (JSONException e1) 
                // TODO Auto-generated catch block
                e1.printStackTrace();
            

            lv = (ListView) rootView.findViewById(R.id.orderlistview);


            db = new DatabaseHandler(getActivity());
            list = db.getAllOrderList();
            adt = new MyListAdapter(getActivity());
            lv.setAdapter(adt);

        
    



【讨论】:

以上是关于如何实现无限滚动列表视图的主要内容,如果未能解决你的问题,请参考以下文章

Android无限滚动列表视图

ember.js 无限滚动(延迟加载)

Android 高级UI设计笔记09:Android如何实现无限滚动列表

如何在 SwiftUI 或无限列表视图中实现列表分页?

如何使用图像视图制作无限分页滚动视图?

无限滚动到集合视图的最佳实现是啥?就像 Pinterest 在滚动结束时获取数据