SwipeRefreshLayout中的RecyclerView无效

时间:2017-11-20 07:19:00

标签: android android-recyclerview swiperefreshlayout

我尝试在SwipeRefreshLayout中使用RecyclerView但SwipeRefreshLayout无效

{
  "city": "ax",
  "message": "School Table",
  "ids": [
    {
      "roll_no": 100,
      "id": 1,
      "age": 13,
      "batch": "a1",
      "name": "abc"
    },
    {
      "roll_no": 200,
      "id": 1,
      "age": 45,
      "batch": "a2",
      "name": "dsf"
    },
    {
      "roll_no": 400,
      "id": 2,
      "age": 23,
      "batch": "a1",
      "name": "fas"
    }
  ],
  "school_id": "xx"
}

我需要改变什么?

1 个答案:

答案 0 :(得分:-2)

试试activity_my.xml

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/swipe_layout">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/recycler_view" />
    </android.support.v4.widget.SwipeRefreshLayout>

,活动类就像这样MainActivity

public class MyActivity extends Activity {

    private RecyclerView recyclerView;
    private SwipeRefreshLayout refreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout);
        refreshLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

            }
        });

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        final LinearLayoutManager layoutParams = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutParams);
        SomeAdapter adapter = new SomeAdapter(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(int newState) {
            }

            @Override
            public void onScrolled(int dx, int dy) {
                refreshLayout.setEnabled(layoutParams.findFirstCompletelyVisibleItemPosition() == 0);
            }
        });
    }
}

https://gist.github.com/VladSumtsov/ad4e13511a9b73ff3b13