Android 中SwipeRefreshLayout刷新动画不显示的问题

在Android中,当SwipeRefreshLayout内的子元素从ListView更换为TextView时,发现刷新动画无法正常显示,仅短暂出现即结束。通过查阅文档并参考匆忙拥挤repeat的代码,将ScrollView作为SwipeRefreshLayout的子元素,并将TextView放入ScrollView内,成功解决了动画不显示的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开始使用SwipeRefreshLayout中容纳一个ListView的时候进行刷新不会有任何问题,但是当我把SwipeRefreshLayout中的子元素换成TextView后问题出现了,刷新的时候动画显示居然不完整!刚跳出来就结束了。

查询文档,文档中有这么一句

If an activity wishes to show just the progress animation, it should call setRefreshing(true)
不幸的是问题依旧。


后来参看了 匆忙拥挤repeat 的代码 将ScrollView作为SwipeRefreshLayout的子元素,将TextView添加到ScrollView中,问题圆满解决。

附上测试源码


布局文件 activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="weather.ricearmy.com.weatherforcast.MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/id_swipe_ly"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/tvText"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </ScrollView>

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

</RelativeLayout>

MainActivity.java

package weather.ricearmy.com.weatherforcast;

import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MainActivity extends ActionBarActivity implements SwipeRefreshLayout.OnRefreshListener {

    private static final int REFRESH_COMPLETE = 0X110;
    private SwipeRefreshLayout mSwipeLayout;
    private TextView tvText;

    private Handler mHandler = new Handler()
    {
        public void handleMessage(android.os.Message msg)
        {
            switch (msg.what)
            {
                case REFRESH_COMPLETE: {
                    String text = tvText.getText().toString();
                    tvText.setText(text+" helloWorld");
                    mSwipeLayout.setRefreshing(false);
                }
                break;

            }
        };
    };

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

        tvText = (TextView)findViewById(R.id.tvText);
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);

        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onRefresh() {
        mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值