SwipeRefresh不工作为RecyclerView添加RecyclerView.OnItemTouchListener

时间:2018-02-19 18:46:12

标签: android android-recyclerview touch swiperefreshlayout

我有一个案例,我需要在RecyclerView.OnItemTouchListener中有一些逻辑。

我还需要SwipeRefreshLayout的功能。

但是当我将LinearLayoutManager设置为我的RecyclerView并向其添加OnItemTouchListener时 - SwipeRefresh不起作用。

这是我的活动代码:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RecyclerView recyclerView = findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            // some custom logic here
            return super.onInterceptTouchEvent(rv, e);
        }
    });
}
}

这是我的布局:

<?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.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</android.support.v4.widget.SwipeRefreshLayout>

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor_list);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_doctorsList);
        swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swiperefresh);

        getDoctorList();


        swipeRefreshLayout.setOnRefreshListener(
                new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        getDoctorList();
                    }
                }
        );

 recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, new 
 RecyclerItemClickListener.OnItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
        Intent i=new Intent(EventActivity.this,ClubActivity.class);
        i.putExtra(CLUB_ID,list.get(position).getId());
        startActivity(i);
    }
}));

 }

用于停止swipeReferesh:

 swipeRefreshLayout.setRefreshing(false); // stopping swipe refresh