Android-如何在listview中实现无休止的滚动

Android-如何在listview中实现无休止的滚动,android,Android,这是小结。我创建了一个滑动选项卡应用程序,并在每个片段上合并了json。我正在尝试实现无止境滚动侦听器:,以便在加载listview和向下滚动更多项目时,listview中只显示一定数量的项目 然而,我被卡住了。我不确定如何实现/使用这个类(公共布尔onLoadMore(int page,int totalItemsCount)等等)。我在stackoverflow上读过另一篇关于这个的文章,但我不知道如何让它起作用。任何关于如何使用的建议或想法都将不胜感激 我包括了我的列表适配器,我的片段之一

这是小结。我创建了一个滑动选项卡应用程序,并在每个片段上合并了json。我正在尝试实现无止境滚动侦听器:,以便在加载listview和向下滚动更多项目时,listview中只显示一定数量的项目

然而,我被卡住了。我不确定如何实现/使用这个类(公共布尔onLoadMore(int page,int totalItemsCount)等等)。我在stackoverflow上读过另一篇关于这个的文章,但我不知道如何让它起作用。任何关于如何使用的建议或想法都将不胜感激

我包括了我的列表适配器,我的片段之一,endlessrollisterner类。我最初的尝试是将此方法包含在public void on activity中,该activity就在这一行之后创建:listView=(listView)getView().findViewById(R.id.list)

endlesscrollstener:

 import android.widget.AbsListView;
 public abstract class EndlessScrollListener implements AbsListView.OnScrollListener {
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;

public EndlessScrollListener() {
}

public EndlessScrollListener(int visibleThreshold) {
    this.visibleThreshold = visibleThreshold;
}

public EndlessScrollListener(int visibleThreshold, int startPage) {
    this.visibleThreshold = visibleThreshold;
    this.startingPageIndex = startPage;
    this.currentPage = startPage;
}

// This happens many times a second during a scroll, so be wary of the code you place here.
// We are given a few useful parameters to help us work out if we need to load some more data,
// but first we check if we are waiting for the previous load to finish.
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
    // If the total item count is zero and the previous isn't, assume the
    // list is invalidated and should be reset back to initial state
    if (totalItemCount < previousTotalItemCount) {
        this.currentPage = this.startingPageIndex;
        this.previousTotalItemCount = totalItemCount;
        if (totalItemCount == 0) { this.loading = true; }
    }
    // If it's still loading, we check to see if the dataset count has
    // changed, if so we conclude it has finished loading and update the current page
    // number and total item count.
    if (loading && (totalItemCount > previousTotalItemCount)) {
        loading = false;
        previousTotalItemCount = totalItemCount;
        currentPage++;
    }

    // If it isn't currently loading, we check to see if we have breached
    // the visibleThreshold and need to reload more data.
    // If we do need to reload some more data, we execute onLoadMore to fetch the data.
    if (!loading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + visibleThreshold)) {
        loading = onLoadMore(currentPage + 1, totalItemCount);
    }
}

// Defines the process for actually loading more data based on page
// Returns true if more data is being loaded; returns false if there is no more data to load.
public abstract boolean onLoadMore(int page, int totalItemsCount);

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    // Don't take any action on changed
}
}
导入android.widget.AbsListView;
公共抽象类EndlessCrollListener实现了AbsListView.OnScrollListener{
//低于当前滚动位置的最小项目数
//在加载更多之前。
私有int visibleThreshold=5;
//已加载数据的当前偏移索引
private int currentPage=0;
//上次加载后数据集中的项目总数
private int previousTotalItemCount=0;
//如果仍在等待加载最后一组数据,则为True。
私有布尔加载=真;
//设置起始页索引
私有int startingPageIndex=0;
public endlesscrollstener(){
}
公共EndlessScrollListener(int VisibleSThreshold){
this.visibleThreshold=visibleThreshold;
}
公共EndlessScrollListener(int VisibleSThreshold,int startPage){
this.visibleThreshold=visibleThreshold;
this.startingPageIndex=startPage;
this.currentPage=起始页;
}
//在滚动过程中,这种情况在一秒钟内会发生很多次,因此请小心放置在此处的代码。
//我们得到了一些有用的参数,以帮助我们确定是否需要加载更多数据,
//但首先我们检查是否在等待前一次加载完成。
@凌驾
public void onScroll(AbsListView视图、int firstVisibleItem、int visibleItemCount、int totalItemCount)
{
//如果项目总数为零,而前一个不是,则假定
//列表无效,应重置回初始状态
if(totalItemCountpreviousTotalItemCount)){
加载=假;
previousTotalItemCount=totalItemCount;
currentPage++;
}
//如果当前未加载,我们将检查是否已违反
//VisibleSThreshold已关闭,需要重新加载更多数据。
//如果确实需要重新加载一些数据,则执行onLoadMore来获取数据。
如果(!loading&&(totalItemCount-visibleItemCount)
 public class CustomListAdapter extends BaseAdapter {
 private Activity activity;
 private LayoutInflater inflater;
 private List<Tanga> tangasItems;
 ImageLoader imageLoader = AppController.getInstance().getImageLoader();

 public CustomListAdapter(Activity activity, List<Tanga> movieItems) {
this.activity = activity;
this.tangasItems = movieItems;
}

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

@Override
public Object getItem(int location) {
return tangasItems.get(location);
 }

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

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

if (inflater == null)
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
    convertView = inflater.inflate(R.layout.list_row, null);

TextView name = (TextView) convertView.findViewById(R.id.name);

// getting movie data for the row
Tanga m = tangasItems.get(position);

// title
name.setText(m.getName());

return convertView;
}

 }
 public class Tab1 extends Fragment {
 private static final String url = "website.json";
 private List<Tanga> tangaList = new ArrayList<Tanga>();
 private ListView listView;
 private CustomListAdapter adapter;

 @Override
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 }

 public void onActivityCreated (Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);

listView = (ListView) getView().findViewById(R.id.list);
adapter = new CustomListAdapter(getActivity(), tangaList);
listView.setAdapter(adapter);
JsonArrayRequest movieReq = new JsonArrayRequest(url,
        new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                // Parsing json
                for (int i = 0; i < response.length(); i++) {
                    try {

                        JSONObject obj = response.getJSONObject(i);
                        Tanga movie = new Tanga();

                        //name
                        String name = obj.getString("name");
                        movie.setName(name);


                        // adding movie to movies array
                        tangaList.add(movie);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                // notifying list adapter about data changes
                // so that it renders the list view with updated data
                adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        VolleyLog.d("Error: ");

    }
});

// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
 }

 @Override
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tabs, container, false);

return v;
 }
 }