java - 使用回收 View 拉动刷新不起作用

标签 java android swiperefreshlayout

当我将设备屏幕移至下方以刷新 RSS 新闻源时,它不起作用。我尝试使用 swiperefreshlayout 刷新新闻。我不知道为什么,但它不起作用:

主要 Activity :

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    SwipeRefreshLayout refreshLayout;

    private LinearLayoutManager linearLayoutManager;
    private MyAdapter myRecyclerViewAdapter;

    Context context;
    ArrayList<FeedItem> feedItems=new ArrayList<FeedItem>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        refreshLayout=(SwipeRefreshLayout)findViewById(R.id.swipeLayout);
        refreshLayout.setOnRefreshListener((SwipeRefreshLayout.OnRefreshListener) context);
        refreshLayout.setColorSchemeColors(0, 0, 0, 0);
        refreshLayout.setProgressBackgroundColor(android.R.color.transparent);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
        ReadRss readRss=new ReadRss(this,recyclerView);
        readRss.execute();



        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                int topRowVerticalPosition =
                        (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
                refreshLayout.setEnabled(topRowVerticalPosition >= 0);
                refreshLayout.setEnabled(linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0);

            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
        });
        /*recyclerView= (RecyclerView) findViewById(R.id.recyclerview);
        linearLayoutManager =
                new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        myRecyclerViewAdapter = new MyAdapter(context,feedItems);
        //myRecyclerViewAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(myRecyclerViewAdapter);
        recyclerView.setLayoutManager(linearLayoutManager);*/
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

内容主.XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.rssreader.MainActivity"
    tools:showIn="@layout/activity_main">

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />

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

最佳答案

您已在 refreshLayout 中定义了 setOnRefreshListener 但未实现它。

您必须在滑动刷新布局上使用 setOnRefreshListener 方法,如下所示。

refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // call your Refresh method here
                mswipeRefreshLayout.setRefreshing(false);
            }
});

关于java - 使用回收 View 拉动刷新不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39725277/

相关文章:

javascript - 我怎样才能在android webview中捕捉元素的滚动

java - SwipeRefreshLayout 动画卡住 onRefresh()

java - 使用 setItems 方法将数据输入到 TableView

Android MVP - 应该避免在演示者中使用 R.string 引用吗?

java - 从内部存储、Android Studio (JAVA) 播放音频文件的应用程序

list 文件中列出的权限的 Android 安全异常

java - 使用 JButton 启动/停止程序 (Java)

java - 为多个 Web 服务请求保持连接打开

尝试运行 .exefile 时启动时出现 Java 错误

android - 如何使用内部的两个 View 刷新 SwipeRefreshLayout