如何在我的下面的代码中添加OnScrollListener

时间:2016-09-04 09:30:00

标签: android android-recyclerview recycler-adapter

自从我使用android以来已经有一段时间了。你可以告诉我如何在这段代码中添加OnScrollListener吗?每次我向下滚动我都想再拍5张图片。

这是Asyncatask它的工作正确,但我每次向下滚动(加载更多)时都需要获取5张图像。

public class RecyclerOkHttpHandler extends AsyncTask<String, Void, String> {
    private Context mContext;
    private MyInterface mListener;
    public String category;
    public String basestart;
    public String limitend;
    public RecyclerOkHttpHandler(Context context, MyInterface mListener, String categ, String base, String limit){
        mContext = context;
        this.mListener  = mListener;
        category=categ;
        basestart=base;
        limitend=limit;
    }
    public interface MyInterface {
        public void myMethod(ArrayList result);
    }

    private final String Fetch_URL = "http://justedhak.com/old-files/Recyclerview_data.php";
    // ArrayList<Listitem> Listitem;
    ArrayList<CategoryList> Listitem;
    int resulta;

    OkHttpClient httpClient = new OkHttpClient();
    ListView list;
    String myJSON;
    JSONArray peoples = null;
    InputStream inputStream = null;

    @Override
    protected String doInBackground(String... params) {
        Log.d("okhttp Fetch_URL", Fetch_URL);



        RequestBody formBody = new FormEncodingBuilder()
                .add("category", category)
                .add("base", basestart)
                .add("limit", limitend)
                .build();
        Request request = new Request.Builder()
                .url(Fetch_URL)
                .post(formBody)
                .build();


        String result = null;
        try {
            Response response = httpClient.newCall(request).execute();
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
            inputStream = response.body().byteStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            result = sb.toString();
            resulta = 1; //"Success
            //  return response.body().bytes();
        } catch (Exception e) {
            Toast.makeText(mContext, "Connection failed, check your connection",
                    Toast.LENGTH_LONG).show();
            e.printStackTrace();        }
        finally {
            try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
        }
        return result;
    }
    @Override
    protected void onPostExecute(String result){
        if( resulta ==1){
            myJSON=result;
            Log.e("result",result);

            showList();
        }
        else{
            Log.e("d","there is an error on postexecute in okhhttphandler.java");

        }
    }

    protected void showList(){
        try {

            JSONObject jsonObj = new JSONObject(myJSON);
            peoples = jsonObj.getJSONArray("result");
            System.out.println("Length:"+peoples.length());
            int J_length=peoples.length()-1;
            //JSONObject maxj  = peoples.getJSONObject(peoples.length() - 1);
            // max of arrray

            jsonObj= peoples.getJSONObject(J_length);

            String j_id=  jsonObj.getString("id");
            int _id = Integer.parseInt(j_id);
            System.out.println(j_id);

            //max of
            DatabaseHandler db = new DatabaseHandler(mContext);
            String db_id="";
            db_id = db.getmax();
            if (db_id== null)
            {
                db_id="0";
            }
            int d_id = Integer.parseInt(db_id);
            Log.e("db_id", db_id);
            Log.e("j_id",j_id);

            //  if (_id < d_id) {
            System.out.println("Getting json result ");

            Listitem = new ArrayList<CategoryList>();
            for (int i = 0; i < peoples.length(); i++) {
                JSONObject c = peoples.getJSONObject(i);

                String id = c.getString("id");
                String url = c.getString("url");



                Listitem.add(new CategoryList(id, url));
            }

            if (mListener != null)
                mListener.myMethod(Listitem);


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

这是我设置适配器的时间

 private String base = "0";
 private String limit = "5";

 final RecyclerOkHttpHandler handler = new RecyclerOkHttpHandler( this, new RecyclerOkHttpHandler.MyInterface() {
            @Override
            public void myMethod(ArrayList result) {
                mAdapter_first = new MyAdapter(result,SearchActivity.this);
                mAdapter_first.notifyDataSetChanged();
                mRecyclerView_first.setAdapter(mAdapter_first);
            }
        },"girls jokes",base,limit);

        try {
             handler.execute().get();

        } catch (Exception e) {
           Log.d("SearchActivity error", "error in mRecyclerView_first");

            e.printStackTrace();
        }

1 个答案:

答案 0 :(得分:0)

首次加载时,请致电RecyclerOkHttpHandler AsyncTask获取前5个项目。

现在,对于任何进一步的负载,您所要做的就是检查listView是否滚动到底部,您可以参考此链接Find out if ListView is scrolled to the bottom?来了解如何处理它。

因此,每当您检测到用户已将列表视图滚动到底部时,就应该调用RecyclerOkHttpHandler AsynTask来获取5张新图片。

PS:您需要保存每次加载时达到的限制,以便在下次加载时,从该限制开始加载。

希望这会有所帮助:)