【问题标题】:Issues when relative layout implement swipe-to-refresh layout相对布局实现滑动刷新布局时的问题
【发布时间】:2016-06-20 02:38:42
【问题描述】:

我目前正在尝试将滑动刷新布局实现为相对布局,但它非常不敏感且不稳定。当我下拉屏幕时,它通常要么不刷新,要么刷新没有进度条。

<android.support.v4.widget.SwipeRefreshLayout
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.miaor.tutorialweather.MainActivity"
android:id="@+id/refreshLayout"
android:background="#fe970b">

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

</RelativeLayout>

public class MainActivity extends AppCompatActivity{

public static final String TAG = MainActivity.class.getSimpleName();
@BindView(R.id.TimeString) TextView mTimeLabel;
@BindView(R.id.TemperatureLabel) TextView mTemperatureLabel;
@BindView(R.id.HumidityValue) TextView mHumidityValue;
@BindView(R.id.rainingChanceValue) TextView mChance;
@BindView(R.id.weatherIcon) ImageView mWeatherIcon;
@BindView(R.id.SummaryText) TextView mSummaryText;
@BindView(R.id.refreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;

///why do we make a new variable of CurrentWeather
private CurrentWeather mCurrentWeather;
private String apiKey = "6b9448b8e21c2abe2fb623b25554a77c";
private double latitude = 31.230708;
private double longitude = 121.472916;
private String forecastUrl = "https://api.forecast.io/forecast/" + apiKey +
        "/" + latitude + "," + longitude;

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

    ///implement swipe-to-refresh feature
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            Log.i(TAG, "onRefresh is working");
            getForecast();
        }
    });

    getForecast();

    Log.d(TAG, "Main UI code is running!");
}

【问题讨论】:

    标签: java android android-relativelayout pull-to-refresh


    【解决方案1】:

    您不能以 RelativeLayout 作为其子级来实现 SwipeToRefreshLayout。您需要有一个可滚动的视图作为其唯一的孩子。例如列表视图、回收器视图、滚动视图。这就是它无法按预期工作的原因。

    【讨论】:

      【解决方案2】:

      这不是正确的位置。

      你需要这样的类型:

      <android.support.v4.widget.SwipeRefreshLayout
          android:id="@+id/your_id"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
      
          <ScrollView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">
      
              YOUR RELATIVE LAYOUT HERE
      
          </ScrollView>
      
      </android.support.v4.widget.SwipeRefreshLayout>
      

      【讨论】:

        【解决方案3】:

        您的代码看起来不错,但它不能正常工作很奇怪......也许是因为 ButterKnife 库?我认为这不是问题,但值得尝试手动获取视图以查看是否是问题

        然后,如果您需要加载动画显示/隐藏,您可以使用mSwipeRefreshLayout.setRefreshing(boolean);

        例如,在我的应用程序中,我首先附加监听器(就像您在示例中所做的那样),然后在获取数据时使用以下代码显示加载动画。

        // Show loading while fetching the first set of data
        mainSwipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                mainSwipeRefreshLayout.setRefreshing(true);
            }
        });
        

        在我的 fetchData() 方法中,当它完成获取数据时,我使用以下行再次隐藏动画。

        // On load complete stop refresh animation
        mainSwipeRefreshLayout.setRefreshing(false);
        

        希望这会有所帮助! 安德烈斯。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-09
          • 2016-04-21
          相关资源
          最近更新 更多