mars885 / persistent-search-view

An Android library designed to simplify the process of implementing search-related functionality.
Apache License 2.0
414 stars 41 forks source link

RecyclerView.addOnScrollListener is called automatically called whenever i click on persistentSearchView in my ui #9

Closed Shankar0130 closed 4 years ago

Shankar0130 commented 5 years ago

public class MainActivity extends AppCompatActivity {

private int page = 1;

PersistentSearchView persistentSearchView;
RecyclerView recyclerView;

PhotosAdapter adapter;
PhotosAdapter.OnPhotoClickedListener photoClickListener;

UnsplashInterface dataService;
private Merlin merlin;

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

    merlin = new Merlin.Builder().withConnectableCallbacks().build(getApplicationContext ());

    recyclerView = findViewById(R.id.recyclerView);

    persistentSearchView = findViewById (R.id.persistentSearchView);

    dataService = UnsplashClient.getUnsplashClient().create(UnsplashInterface.class);

    photoClickListener = new PhotosAdapter.OnPhotoClickedListener() {
        @Override
        public void photoClicked(Photo photo, ImageView imageView) {
            Intent intent = new Intent();
            intent.putExtra("image", photo);
            setResult(RESULT_OK, intent);
            finish();
        }
    };

    persistentSearchView.setOnLeftBtnClickListener(new View.OnClickListener () {

        @Override
        public void onClick(View view) {
            finish ();
        }

    });

    persistentSearchView.setVoiceRecognitionDelegate(new VoiceRecognitionDelegate (this));

    persistentSearchView.setOnSearchConfirmedListener(new OnSearchConfirmedListener () {

        @Override
        public void onSearchConfirmed(PersistentSearchView searchView, String query) {
            // Handle a search confirmation. This is the place where you'd
            // want to perform a search against your data provider.

            search (query);
            Toast.makeText (MainActivity.this, "search is confirmed", Toast.LENGTH_SHORT).show ();

        }

    });

    persistentSearchView.setOnSuggestionChangeListener(new OnSuggestionChangeListener () {

        @Override
        public void onSuggestionPicked(SuggestionItem suggestion) {
            // Handle a suggestion pick event. This is the place where you'd
            // want to perform a search against your data provider.

        }

        @Override
        public void onSuggestionRemoved(SuggestionItem suggestion) {
            // Handle a suggestion remove event. This is the place where
            // you'd want to remove the suggestion from your data provider.

        }

    });

    GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
    recyclerView.setLayoutManager(layoutManager);
    adapter = new PhotosAdapter(new ArrayList<Photo> (), this, photoClickListener);
    recyclerView.setAdapter(adapter);

    recyclerView.addOnScrollListener(new EndlessRecyclerViewScrollListener(layoutManager) {
        @Override
        public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {

                loadPhotos();

            Toast.makeText (MainActivity.this, "  endless scroll view is called  ", Toast.LENGTH_LONG).show ();

        }
    });

    loadPhotos();

    merlin.registerConnectable(new Connectable () {
        @Override
        public void onConnect() {

            Toast.makeText (MainActivity.this, "connected with internet", Toast.LENGTH_SHORT).show ();

        }

    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult (requestCode, resultCode, data);

    VoiceRecognitionDelegate.handleResult(persistentSearchView, requestCode, resultCode, data);

}

private void loadPhotos() {

    dataService.getPhotos(page,null,"latest")
            .enqueue (new Callback<List<Photo>> () {
                @Override
                public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {
                    List<Photo> photos = response.body();
                    Log.i("Photos", "Photos Fetched " + photos.size());
                    //add to adapter
                    page++;
                    adapter.addPhotos(photos);
                    recyclerView.setAdapter(adapter);

                }

                @Override
                public void onFailure(Call<List<Photo>> call, Throwable t) {

                }
            });

}

public void search(String query){
    if(query != null && !query.equals("")) {

        dataService.searchPhotos(query,1,null,null)
                .enqueue (new Callback<SearchResults> () {
                    @Override
                    public void onResponse(Call<SearchResults> call, Response<SearchResults> response) {
                        SearchResults results = response.body();
                        Log.d("Photos", "Total Results Found " + results.getTotal());
                        List<Photo> photos = results.getResults();
                        adapter = new PhotosAdapter(photos, MainActivity.this, photoClickListener);
                        recyclerView.setAdapter(adapter);

                    }

                    @Override
                    public void onFailure(Call<SearchResults> call, Throwable t) {

                    }
                });

    }
    else {
        loadPhotos();
    }
}

@Override
protected void onResume() {
    super.onResume();
    merlin.bind();
}

@Override
protected void onPause() {
    merlin.unbind();
    super.onPause();
}

}

mars885 commented 4 years ago

Hey there.

Have you resolved the issue because it's been inactive for a couple of months?