在片段中不调用onScroll()方法

时间:2015-03-19 07:46:28

标签: java android

我制作图库并尝试在滚动期间下载新图像,但不调用onScroll()!当我启动片段时,onScroll()被调用一次,但是当我滚动listView时,onScroll不起作用

public class FotoActivity extends Activity {

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

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new FotoFragment())
                .commit();
    }
}


public static class FotoFragment extends AbsListViewBaseFragment implements AbsListView.OnScrollListener {

    private static final String CLIENT_ID = "7f38ead5152e46f1a42284c2d84b0862";
    private static final String API_REQUEST = "cat";
    private static final String API_URL = "https://api.instagram.com/v1";

    static String downloadJsonUrl = API_URL + "/tags/" + API_REQUEST + "/media/recent?client_id=" + CLIENT_ID;

    private ImageAdapter picAdapter;

    ArrayList<String> picUrls = new ArrayList<String>();

    GetImages getInstagramImages;

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

        picAdapter = new ImageAdapter();

        getInstagramImages = new GetImages();
        getInstagramImages.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fr_image_grid, container, false);
        listView = (GridView) rootView.findViewById(R.id.grid);
        ((GridView) listView).setAdapter(picAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            }
        });

        listView.setOnScrollListener(this);

        return rootView;
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {

    }

    @Override
    public void onScroll(AbsListView absListView, int firstVisible, int visibleCount, int totalCount) {
        boolean loadMore = firstVisible + visibleCount >= totalCount;

        if (loadMore == true && getInstagramImages.getStatus() == AsyncTask.Status.FINISHED) {
            getInstagramImages = new GetImages();
            getInstagramImages.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        }
    }


    protected class GetImages extends AsyncTask<Void, Void, ArrayList> {

        @Override
        protected ArrayList doInBackground(Void... voids) {
            try {
                picUrls = parseJson();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return picUrls;
        }

        @Override
        protected void onPostExecute(ArrayList result) {
            super.onPostExecute(result);
            picAdapter.notifyDataSetChanged();
        }
    }


    private InputStream downloadJSON(String stringUrl) throws IOException {
        URL url = new URL(stringUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        connection.connect();
        return connection.getInputStream();
    }


    private ArrayList parseJson() throws IOException, JSONException {
        InputStream inputStream = downloadJSON(downloadJsonUrl);
        JSONObject jsonObject = (JSONObject) new JSONTokener(responseJSON).nextValue();
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        for(int i = 0; i < jsonArray.length(); i ++){
            JSONObject imageJsonObject = jsonArray.getJSONObject(i).getJSONObject("images").getJSONObject("standard_resolution");
            picUrls.add(imageJsonObject.getString("url"));
        }
        downloadJsonUrl = jsonObject.getJSONObject("pagination").getString("next_url");

        return picUrls;
    }

}

1 个答案:

答案 0 :(得分:0)

代码看起来很好,有些东西在填写时缺少,它可以工作..你可以在这里参考代码 FotoActivity.java