SwipeRefreshLayout no animation on fragment creation

前端 未结 5 2102
感情败类
感情败类 2021-01-05 03:58

I\'m using android.support.v4.widget.SwipeRefreshLayout with android.support.v7.widget.RecyclerView.
On fragment view creation I need to show <

相关标签:
5条回答
  • 2021-01-05 04:05

    Thanks, guys.

    I`ve just realised that the easiest solution is to call method measure(int, int) with non-zero values) before calling setRefreshing(true). The problem is happening because setRefreshing(true) is calling before onMeasure.

    So you need to call:

    swipeRefreshLayout.measure(initialWidth, initialHeight);
    swipeRefreshLayout.setRefreshing(true);
    
    0 讨论(0)
  • 2021-01-05 04:07

    This is a reported bug (reported here) and a workaround is available:

    mSwipeRefreshLayout.setProgressViewOffset(false, 0,
        (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 
            24, 
            getResources().getDisplayMetrics()));
    mSwipeRefreshLayout.setRefreshing(true);
    
    0 讨论(0)
  • 2021-01-05 04:10

    SwipeRefreshLayout must contain a child to work properly. Make sure you initialize it before using it anywhere!

    0 讨论(0)
  • 2021-01-05 04:15

    you must call setRefreshing(true) after onDraw(), else it would not display animation.

    so, in Activity, you can do it in onCreate(), like this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // other codes
    
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
                refreshing = true;
                refreshData();
            }
        }
    }
    

    in fragment, you can put these codes in onActivityCreated().

    0 讨论(0)
  • 2021-01-05 04:19

    It depends on which API level you're building under - if you're using up to API 20 then you can just turn on setRefreshing(true), this will run the animation in the ActionBar, but in API 21 (Material Design) they changed the progress to be a spinner than is "pulled into view" before it spins

    You have 2 ways of getting around this in API 21: 1) shift the spinner down with setProgressViewOffset(), but remember to shift it back up afterwords (note that this works in px, while setDistanceToTriggerSync() uses dp) 2) make a duplicate spinner that is displayed when you're loading the data

    The more code-efficient solution is to use the existing spinner, but you have to be careful that you do reset its position

    If you need to calculate the pixel density, you can grab it from:

    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    float scale = metrics.density;
    
    0 讨论(0)
提交回复
热议问题