簡體   English   中英

相對布局實現滑動刷新布局時的問題

[英]Issues when relative layout implement swipe-to-refresh layout

我目前正在嘗試將滑動刷新布局實現為相對布局,但它非常不敏感且不穩定。 當我下拉屏幕時,它通常不會刷新或刷新而沒有進度條。

<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!");
}

您不能使用 RelativeLayout 作為其子項來實現 SwipeToRefreshLayout。 您需要將可滾動視圖作為其唯一的子項。 例如 Listview、RecyclerView、ScrollView。 這就是為什么它沒有按您的預期工作的原因。

那不是正確的位置。

你需要這樣輸入:

<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>

你的代碼看起來不錯,奇怪的是它不能正常工作......也許是因為 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);

希望這有幫助! 安德烈斯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM