refresh recyclerview Elemente in einem Fragment

Meine Anwendung verfügt über 4 Registerkarten mit verschiebbarem Registerkartenlayout und ich habe in MainActivity einen Ansichts-Pager verwendet, der für jede Registerkarte ein Fragment mit 4 Registerkarten enthält ...

Ich empfange Daten von MySQL mit Volley. Kartenansicht. Recycler-Ansicht und JSON

Ich möchte die Liste mit SwipeRefreshLayout aktualisieren, aber die Liste kann nicht aktualisiert werden. Ich sollte die App schließen und das @ neu starte

in meinem SwipeRefreshLayout OnRefreshListener habe ich meine getdata () -Methode aufgerufen, aber ich denke, der bessere Weg ist, das Fragment zu aktualisieren (Reload).

irgendwelche Ideen? Dank

all meines Fragment-Codes:

public class AllOfAds extends Fragment {
    SwipeRefreshLayout mSwipeRefreshLayout;

    //Creating a List of superheroes
    private List<Getter_Setter> listCars;
    //Creating Views
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private RecyclerView.Adapter adapter;
    //Volley Request Queue
    private RequestQueue requestQueue;
    //The request counter to send ?page=1, ?page=2  requests
    private int requestCount = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.allofads, container, false);
        return v;
    }

    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        super.onCreate(savedInstanceState);

        final ProgressBar progressBar = (ProgressBar) getActivity().findViewById(R.id.progressBar1);

        mSwipeRefreshLayout = (SwipeRefreshLayout) getActivity().findViewById(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);

        //Initializing Views
        recyclerView = (RecyclerView) getActivity().findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

        //Initializing our superheroes list
        listCars = new ArrayList<>();
        requestQueue = Volley.newRequestQueue(getActivity());
        //Calling method to get data to fetch data
        getData();

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
                adapter.notifyDataSetChanged();
            }
        });

        //Adding an scroll change listener to recyclerview
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (isLastItemDisplaying(recyclerView)) {
                    //Calling the method getdata again
                    getData();
                    progressBar.setVisibility(View.GONE);
                }
            }
        });

        //initializing our adapter
        adapter = new CardAdapter(listCars, getActivity());

        //Adding adapter to recyclerview
        recyclerView.setAdapter(adapter);
    }

    //Request to get json from server we are passing an integer here
    //This integer will used to specify the page number for the request ?page = requestcount
    //This method would return a JsonArrayRequest that will be added to the request queue
    private JsonArrayRequest getDataFromServer(int requestCount) {
        //Initializing ProgressBar
        final ProgressBar progressBar = (ProgressBar) getActivity().findViewById(R.id.progressBar1);

        //Displaying Progressbar

        //JsonArrayRequest of volley
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(ConfigJson.DATA_URL + String.valueOf(requestCount),
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    //Calling method parseData to parse the json response
                    parseData(response);
                    //Hiding the progressbar
                    mSwipeRefreshLayout.setRefreshing(false);
                    progressBar.setVisibility(View.GONE);
                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressBar.setVisibility(View.GONE);
                    //If an error occurs that means end of the list has reached
                    Toast.makeText(getActivity(), "no internet access!", Toast.LENGTH_SHORT).show();
                }
            });

        //Returning the request
        return jsonArrayRequest;
    }

    //This method will get data from the web api
    private void getData() {
        //Adding the method to the queue by calling the method getDataFromServer
        requestQueue.add(getDataFromServer(requestCount));
        //Incrementing the request counter
        requestCount++;
    }

    //This method will parse json data
    private void parseData(JSONArray array) {
        for (int i = 0; i < array.length(); i++) {
            //Creating the superhero object
            Getter_Setter cars = new Getter_Setter();
            JSONObject json = null;

            try {
                //Getting json
                json = array.getJSONObject(i);

                //Adding data to the superhero object
              cars.setImageUrl(json.getString(ConfigJson.TAG_IMAGE_URL));
                cars.setName(json.getString(ConfigJson.TAG_PUBLISHER));
                cars.setBrand(json.getString(ConfigJson.TAG_BRAND));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //Adding the superhero object to the list
            listCars.add(cars);
        }

        //Notifying the adapter that data has been added or changed
        adapter.notifyDataSetChanged();
    }

    //This method would check that the recyclerview scroll has reached the bottom or not
    private boolean isLastItemDisplaying(RecyclerView recyclerView) {
        if (recyclerView.getAdapter().getItemCount() != 0) {
            int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
            if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
                Toast.makeText(getActivity(), "end of ads...!", Toast.LENGTH_SHORT).show();
                return true;
        }
        return false;
    }
}

EDIT: Mein XML:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar1"
        android:layout_gravity="center"/> 

</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>

Antworten auf die Frage(0)

Ihre Antwort auf die Frage