当相对布局实现滑动到删除布局时,问题



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

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

您无法将swipetorefreshlayout用relativelayout作为其孩子。您需要将视图视为其唯一的孩子。例如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>

您的代码看起来不错,很奇怪它无法正常工作……也许是因为黄油刀库?我认为这不是问题,但是值得一试

然后,如果您需要加载动画出现/隐藏,则可以使用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);

希望这会有所帮助!andres。

最新更新