T
T
TigrIT2022-02-09 22:07:08
Java
TigrIT, 2022-02-09 22:07:08

How to load the list first and then update it with SwipeRefreshLayout?

It is necessary that the list is loaded from the beginning, and then updated using SwipeRefreshLayout.
I get one thing.
Or the list is loaded but then it is not updated (although a reboot occurs).

public class MainActivity extends AppCompatActivity {

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

        ListView list_block = findViewById(R.id.list_block);
        TextView contentView = findViewById(R.id.topText);

        mSwipeRefreshLayout = findViewById(R.id.swipe_refresh);

        ...код загрузки списка...

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                    mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }
}

Or the list is updated but then there is no loading at program start. Which is not very nice when you download the program and look at a blank screen.
public class MainActivity extends AppCompatActivity {

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

        ListView list_block = findViewById(R.id.list_block);
        TextView contentView = findViewById(R.id.topText);

        mSwipeRefreshLayout = findViewById(R.id.swipe_refresh);

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                    ...код загрузки списка...

                    mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }
}


I only got it if it says (... list loading code...) twice, one outside the SwipeRefreshLayout and one inside, repeat it again. I understand that there is no other way.
public class MainActivity extends AppCompatActivity {

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

        ListView list_block = findViewById(R.id.list_block);
        TextView contentView = findViewById(R.id.topText);

        mSwipeRefreshLayout = findViewById(R.id.swipe_refresh);

        ...код загрузки списка...

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                    ...код загрузки списка...

                    mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AlexVWill, 2022-02-09
@TigrIT

The second case is clearly wrong, because All the same, the list must first be loaded, and then it must be updated by swipe. Therefore, you don’t see anything, because. the formation of the list does not occur at the beginning, but after the setOnRefreshListener triggers on the mSwipeRefreshLayout element.
Why does list output not work in the first case? you hid the code (what a stupid habit?), and the telepaths all went to Bali to clear their chakras.
But if there is some kind of RecyclerViever, then it works something like this:

spoiler

RecyclerView historyRecyclerView = binding.adrecyclerView;
        historyRecyclerView = historyRecyclerView.findViewById(R.id.adrecycler_view);
        HistoryViewRecycler historyView = new HistoryViewRecycler();
        historyView.initRecycler(historyRecyclerView);

        swipeRefreshLayout = binding.swipeRefreshLayout.findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //make new recycle view on swipe refresh
                RecyclerView newhistoryRecyclerView = binding.adrecyclerView;
                HistoryViewRecycler newhistoryView = new HistoryViewRecycler();
                JSONArray newhistory = new HistoryViewRecycler().updateHistory(getContext());
                if (newhistory == null) {
                    new History().getFullHistory(getContext());
                    //Snackbar.make(getActivity().findViewById(android.R.id.content), R.string.auth_error, 3000).setDuration(5000).show();
                }
                newhistoryView.initRecycler(newhistoryRecyclerView);
                swipeRefreshLayout.setRefreshing(false);
            }
        });


I do not claim to be exclusive code, it's just an example.
You don't have any updating of the list in the onRefresh() code, except for stopping the indicator from spinning... well, why should it update something there?
Then place the list generation code in both sections (like a 1 + 2 example of compatibility) and see what happens.

D
Denis Zagaevsky, 2022-02-10
@zagayevskiy

Move the data loading code into a separate method, and call it in two places (when creating an activity and inside the onRefresh method)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question