简体   繁体   中英

Reset RecylerView without clear the Adapter

I'm trying to manage a RyclerView into my fragment. When I open the app, the fragment gets loaded and a list of element is generated too (this by the execution of the loadMovie() method). Now, I added a search button and I'd like to reload the loadMovie() method to refresh the list of elements. My problem is that even if the list changes, when I click on an element it opens me two new activities: the one of the object on which I clicked on and the one of the element that was in the same position of the current one before calling the method. How can I delete all the old elements of the RecyclerView without modify the Adapter? This is the code:

 public class PopularFragment extends Fragment {

    private EditText mSearchField;
    private ImageButton mSearchBtn;

    @BindView(R.id.rc_view)
    RecyclerView recyclerView;

    public PopularFragment() {
    }

    public static PopularFragment newInstance(String param1, String param2) {
        PopularFragment fragment = new PopularFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.popular_fragment, container, false);
        recyclerView = view.findViewById(R.id.popular_rc_view);

        mSearchField = (EditText) view.findViewById(R.id.search_field);
        mSearchBtn = (ImageButton) view.findViewById(R.id.search_btn);

        mSearchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadMovie();
            }
        });

        loadMovie();
        return view;
    }


    @Override
    public void onPause() {
        super.onPause();
    }

    private void loadMovie() {
        ApiService apiService = ApiBuilder.getClient(getContext()).create(ApiService.class);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL, false));
        Call<MovieResponse> call = apiService.getPopular(Values.CATEGORY[1], BuildConfig.API_KEY,Values.LANGUAGE,Values.PAGE[0]);
        call.enqueue(new Callback<MovieResponse>() {
            @Override
            public void onResponse(Call<MovieResponse>call, Response<MovieResponse> response) {
                final List<MovieModel> movies = response.body().getResults();
                recyclerView.setAdapter(new PopularAdapter(movies, R.layout.content_main, getContext()));
                recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
                    GestureDetector gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
                        public boolean onSingleTapUp(MotionEvent e){
                            return true;
                        }
                    });

                    @Override
                    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
                        View child = rv.findChildViewUnder(e.getX(), e.getY());
                        if (child != null && gestureDetector.onTouchEvent(e)){
                            int position = rv.getChildAdapterPosition(child);
                            //loadActivity
                        }
                        return false;
                    }

                    @Override
                    public void onTouchEvent(RecyclerView rv, MotionEvent e) {

                    }

                    @Override
                    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

                    }
                });
            }

            @Override
            public void onFailure(Call<MovieResponse>call, Throwable t) {
            }
        });
    }
}

Have you tried to define the Adapter class globally?

Try doing the following steps:-

  1. Define the Adapter class globally and pass an empty list of movies.
  2. Add a function in adapter that accepts the list of movies that you can call in place of defining your adapter over an over again.
  3. Call (Adapter class variable).notifyDataSetChanged(); every time after you call the new function that populates your data, that'll refresh your list of data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM