【问题标题】:How to use the SwipeRefreshLayout?如何使用 SwipeRefreshLayout?
【发布时间】:2014-04-11 14:02:32
【问题描述】:

背景

Google 最近发布了an update to its support library,现在有一个新的“SwipeRefreshLayout”视图。

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

截图:

问题

Google 没有提供样本(至少我还没有找到),所以我自己尝试使用它。

一开始我每次刷卡都会崩溃 (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>

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

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

问题

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

如何自定义它?目前它只是一条黑线......

【问题讨论】:

  • 没有意识到这是在官方的android sdk中。显然这家伙已经想通了,antonioleiva.com/swiperefreshlayout
  • 你能展示一下你是如何在java代码中实现它的吗?
  • @Rperryng 我没有。我所做的只是添加一个什么都不做的监听器。代码本身位于支持库中。所以你说这些是唯一可用的功能?
  • @ZohraKhan 这不是支持库类。

标签: android android-support-library swiperefreshlayout


【解决方案1】:

我不知道您要扩展的 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);
    }
}

值得指出的是,我完全按照原样复制粘贴了您的 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>

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) 还可用于设置用户需要向下滑动多远才能触发刷新。

我建议查看official documentation,看看该课程还提供什么。

【讨论】:

  • 这太糟糕了,它缺少这么多可能的功能。也许它至少是开源的?
  • 它是开源的。您可以通过导航到目录 \sdk\extras\android\support\v4\src\java\android\support\v4\widget 查看源代码
  • 你在“setColorScheme”中使用了两次“purple”... :)
【解决方案2】:

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

}

}

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>

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>

【讨论】:

  • 现在似乎不推荐使用“setColorScheme”。应该改用哪种方法? setColorSchemeResources 和 setColorSchemeColors ?
  • 使用 setColorSchemeResources 检查此链接 developer.android.com/reference/android/support/v4/widget/…, int, int, int)
  • 为什么android studio说..我不能从静态上下文调用非静态方法mSwipeRefreshLayout.setOnRefreshListener(this)?
【解决方案3】:

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

活动代码

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

    var mSwipeRefreshLayout: SwipeRefreshLayout? = null

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

mSwipeRefreshLayout= findViewById<SwipeRefreshLayout>(R.id.swipe_refresh_layout)
mSwipeRefreshLayout!!.setOnRefreshListener {
   //API Calls
}

API 调用成功或 API 调用失败

                mSwipeRefreshLayout!!.isRefreshing = false

【讨论】:

  • 记住你需要在 build.gradle (app) 级别的文件实现中添加这个("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")
【解决方案4】:

您可以通过以下方法(我在 Expandable list 上使用 swipelayout) 测试.xml

  <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/RelativeTop"
        android:background="@drawable/listbg" >

        <ExpandableListView
            android:id="@+id/lvExp"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="@android:color/transparent"
            android:groupIndicator="@null"
            android:listSelector="@android:color/white" />
    </android.support.v4.widget.SwipeRefreshLayout>

ListHeader.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:background="@drawable/sectionbg"
         android:orientation="horizontal"
         android:padding="8dp" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:contentDescription="@string/contentdescription"
    android:src="@drawable/expandbtn" />

<TextView
    android:id="@+id/lblListHeader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="3dp"
    android:text="this is test"
    android:textColor="@android:color/white"
    android:textSize="17sp" />

ChildItem.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:background="@color/GrayProductBackground" >

<TextView
    android:id="@+id/lblListItem"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginLeft="35dp"
    android:layout_marginRight="20dp"
    android:gravity="center_vertical"
    android:paddingBottom="5dp"
    android:textColor="@color/GrayTextProduct"
    android:textSize="17sp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignRight="@+id/lblListItem"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:contentDescription="@string/arrowdescription"
    android:gravity="center"
    android:src="@drawable/navigationarrow" />

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_alignParentBottom="true"
    android:background="@android:color/white" >
</RelativeLayout>

使用的颜色

<color name="pDarkGreen">#8ec549</color>
<color name="pDarskSlowGreen">#c1f57f</color>
<color name="pLightGreen">#f7ffed</color>
<color name="pFullLightGreen">#d6d4d4</color>

Mainactivity.java

        swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe2);
        swipeView.setColorSchemeResources(R.color.pDarkGreen, R.color.Gray, R.color.Yellow, R.color.pFullLightGreen);

swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {

                        swipeView.setRefreshing(false);

                    }
                    (new Handler()).postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            Log.d("Swipe", "Refreshing Number");

                        }
                    }, 0);
                }
            });

您可以根据您的要求将swipeView.setRefreshing(false);设置为false或true 这种滑动机制将在 android 的所有 API 级别中工作

【讨论】:

    【解决方案5】:

    activity_main.xml

     <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:visibility="gone"/>
    

    MainActivity.java

    public class MainActivity extends AppCompatActivity
        implements SwipeRefreshLayout.OnRefreshListener {
    
          static ViewPager viewPager;
    
         @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        swipeRefreshLayout.setRefreshing(true);
                                        setupViewPager(viewPager);
                                    }
                                }
        );
    }
    
    
    
     private void setupViewPager(ViewPager viewPager) {
    
    
    swipeRefreshLayout.setRefreshing(false);
    
    
     }
    
    
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多