Onscroll获得无尽的列表视图

时间:2016-01-03 11:42:17

标签: android listview parse-platform onscroll

我有一个listview,可以从parse.com检索数据。我添加了一个onscroll监听器,首先加载20个项目,当用户滚动到结束时(即20),显示一个加载页脚,接下来显示20个项目,直到列表结束 但我面临以下问题 1}不显示加载页脚 2}当滚动到达我的情况50列表的末尾时,仍然执行onscroll 3}当滚动回到顶部时,滚动不顺畅,它就像一次又一次地卡在同一个列表项

更好地了解我的问题watch this video

我的代码

public class InterActivity extends Activity implements OnItemClickListener, OnItemLongClickListener
 {


ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
FinalAdapter adapter;
 List<CodeList> codelist = null;
 SharedPreference shrdPreference;
 boolean loadingMore = false;
 private int limit = 20;

View footerView;




@Override
public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.inter_layout);
    shrdPreference = new SharedPreference();

    footerView = ((LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.loadmore, null, false);

    //Execute RemoteDataTask AsyncTask

    new RemoteDataTask().execute();
}

private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(InterActivity.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Loading");
        // Set progressdialog message
        mProgressDialog.setMessage("Please wait loading ...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setCancelable(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Create the array
        codelist = new ArrayList<CodeList>();
        try {
            // Locate the class table named "Country" in Parse.com
            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                "InterActivity");
            // Locate the column named "ranknum" in Parse.com and order list
            // by ascending
            query.orderByAscending("_created_at");
            query.setLimit(limit);
            ob = query.find();
            for (ParseObject inter : ob) {



                CodeList map = new CodeList();

                map.setIntroduction((String) inter.get("intro"));
                map.setFinalCodeText((String) inter.get("codetext"));



                codelist.add(map);
            }
        } catch (ParseException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.inter_layoutListView);
        // Pass the results into ListViewAdapter.java
        adapter = new FinalAdapter(InterActivity.this,
                                      codelist);

        AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(adapter);
        animationAdapter.setAbsListView(listview);



        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(InterActivity.this);

        listview.setOnItemLongClickListener(InterActivity.this); 
        // Close the progressdialog
        mProgressDialog.dismiss();

        listview.setOnScrollListener(new OnScrollListener() {


                public void onScrollStateChanged(AbsListView view,
                                                 int scrollState) { // TODO Auto-generated method stub
                    int threshold = 1;
                    int count = listview.getCount();

                    if (scrollState == SCROLL_STATE_IDLE) {
                        if (listview.getLastVisiblePosition() >= count
                            - threshold) {
                            // Execute LoadMoreDataTask AsyncTask
                            new LoadMoreDataTask(InterActivity.this).execute();
                        }
                    }
                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem,
                                     int visibleItemCount, int totalItemCount) {
                    // TODO Auto-generated method stub

                }

            });

    }
}



private class LoadMoreDataTask extends AsyncTask<Void, Void, Void> {

    private Activity activity;
    private View footer;

    private LoadMoreDataTask(Activity activity) {
        this.activity = activity;
        loadingMore = true;
        footer = activity.getLayoutInflater().inflate(R.layout.loadmore, null);
    }


    @Override
    protected void onPreExecute() {
        listview.addFooterView(footer);
        super.onPreExecute();



        }

    @Override
    protected Void doInBackground(Void... params) {
        codelist = new ArrayList<CodeList>();
        try {
            // Locate the class table named "Country" in Parse.com
            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                "InterActivity");
            // Locate the column named "ranknum" in Parse.com and order list
            // by ascending
            query.orderByAscending("_created_at");
            query.setLimit(limit += 20);
            ob = query.find();
            for (ParseObject inter : ob) {


                map.setIntroduction((String) inter.get("intro"));
                map.setFinalCodeText((String) inter.get("codetext"));


                codelist.add(map);
            }
        } catch (ParseException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }   

    @Override
    protected void onPostExecute(Void result) {

        if (footer != null) {
            listview.removeFooterView(footer);
        }
        // Locate listview last item
        int position = listview.getLastVisiblePosition();
        // Pass the results into ListViewAdapter.java
        adapter = new FinalAdapter(InterActivity.this, codelist);
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);
        // Show the latest retrived results on the top
        listview.setSelectionFromTop(position, 0);


    }

}

0 个答案:

没有答案