如何使用SwipeRefreshLayout?

and*_*per 18 android android-support-library swiperefreshlayout

背景

Google最近发布了对其支持库的更新,该库现在有一个新的" SwipeRefreshLayout "视图.

该视图允许包装另一个视图,同时支持向下滑动以执行刷新操作.

截图:

在此输入图像描述

问题

谷歌没有提供样本(至少没有我能找到的样本),所以我自己尝试过使用它.

每当我刷卡时,我都会遇到崩溃(NPE),但后来我发现这是因为我没有为它提供"OnRefreshListener".

但我仍然不知道如何使用它,更不用说自定义它了

这是布局文件的XML:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.swiperefreshlayouttest.MainActivity"
    tools:ignore="MergeRootFrame" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TTT"
                android:textSize="40sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TTT"
                android:textSize="40sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TTT"
                android:textSize="40sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TTT"
                android:textSize="40sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TTT"
                android:textSize="40sp" />
        </LinearLayout>
    </ScrollView>

</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)

代码,虽然它根本没有做任何特别的事情:

public class MainActivity extends ActionBarActivity
  {
  @Override
  protected void onCreate(final Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final SwipeRefreshLayout swipeRefreshLayout=(SwipeRefreshLayout)findViewById(R.id.container);
    swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener()
      {
        @Override
        public void onRefresh()
          {
          // do nothing
          }
      });
    }
  }
Run Code Online (Sandbox Code Playgroud)

这个问题

使用此视图的正确方法是什么?

我该如何定制?目前它只是一条黑线......

rpe*_*yng 36

Dunno ActionBarActivity你正在扩展的课程是什么,但是我使用FragmentActivity让它工作得很好

public class ActivityMain extends FragmentActivity implements OnRefreshListener {

    private SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.container);
        mSwipeRefreshLayout.setOnRefreshListener(this);

        super.onCreate(savedInstanceState);
    }

    @Override
    public void onRefresh() {
        Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(false);
            }
        }, 2000);
    }
}
Run Code Online (Sandbox Code Playgroud)

值得指出我完全按照原样复制粘贴你的xml布局

在自定义方面,除了通过调用setColorScheme(int colorResId,int colorResId,int colorResId,int colorResId)更改彩色条的颜色之外,你真的没什么可做的.

例如

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="blue">#0099CC</color>
    <color name="purple">#9933CC</color>
    <color name="green">#669900</color>
    <color name="orange">#FF8800</color>

</resources>
Run Code Online (Sandbox Code Playgroud)

mSwipeRefreshLayout.setColorScheme(R.color.blue, R.color.purple, R.color.green, R.color.orange);

这真是令人失望的补充.刷新的灵敏度相当高,没有设置可以更改它

编辑

当这个类(和ActionBarActivity类)刚刚添加到sdk 时,我写了这个.因此,从我写这个答案时,有些事情发生了变化.此外,您使用的Activity类型不应影响此解决方案.

setColorScheme现在已弃用,setColorSchemeResources(int... colorResIds)应改为使用.(您可以根据需要在其中放置尽可能多的颜色ID).

setDistanceToTriggerSync(int distance) 也可用于设置用户需要滑动多远才能触发刷新.

我建议查看官方文档,看看该课程还提供了什么.


Zoh*_*han 11

MainActivity.java

public class MainActivity extends ActionBarActivity {
    TextView textView;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    static int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.scrollTextView);

    // /You will setup the action bar with pull to refresh layout
    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.container);

     mSwipeRefreshLayout.setColorScheme(R.color.blue,
     R.color.green, R.color.orange, R.color.purple);
    mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
        @Override
        public void onRefresh() {
            Log.e(getClass().getSimpleName(), "refresh");
            new GetLinks().execute();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public class GetLinks extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Void doInBackground(Void... params) {

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {

            e.printStackTrace();

        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
         //Here you can update the view
        textView.setText(textView.getText().toString()+"--New Content Added" + ++count);

        // Notify swipeRefreshLayout that the refresh has finished
        mSwipeRefreshLayout.setRefreshing(false);
    }

}

}
Run Code Online (Sandbox Code Playgroud)

activity_main.xml中

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/scrollTextView"
                android:text="TTT"
                android:textSize="40sp" />
    </ScrollView>

</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <item name="blue" type="color">#FF33B5E5</item>
    <item name="purple" type="color">#FFAA66CC</item>
    <item name="green" type="color">#FF99CC00</item>
    <item name="orange" type="color">#FFFFBB33</item>

</resources>
Run Code Online (Sandbox Code Playgroud)

  • 使用setColorSchemeResources检查此链接https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html#setColorScheme(int,int,int,int) (3认同)

小智 6

使用 Kotlin 进行 SwipeRefreshLayout

Xml文件代码

 <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:layout_width="0dp"
        android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="90dp" app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="93dp" app:layout_constraintEnd_toEndOf="parent"
        android:id="@+id/swipe_refresh_layout"
>
    <androidx.recyclerview.widget.RecyclerView
            android:layout_width="0dp"
            android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="90dp" app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="93dp" app:layout_constraintEnd_toEndOf="parent"
            android:id="@+id/recyclerView"
    />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)

活动代码

在 oncreate 方法上方声明以下代码

    var mSwipeRefreshLayout: SwipeRefreshLayout? = null
Run Code Online (Sandbox Code Playgroud)

在 setContentView 行之后的 oncreate 方法中声明以下代码

mSwipeRefreshLayout= findViewById<SwipeRefreshLayout>(R.id.swipe_refresh_layout)
mSwipeRefreshLayout!!.setOnRefreshListener {
   //API Calls
}
Run Code Online (Sandbox Code Playgroud)

API 调用成功或 API 调用失败时

                mSwipeRefreshLayout!!.isRefreshing = false
Run Code Online (Sandbox Code Playgroud)

  • 请记住,您需要将其添加到 build.gradle(应用程序)级别文件实现中(“androidx.swiperefreshlayout:swiperefreshlayout:1.1.0”) (2认同)