如何在我的应用程序中停止无限滚动?

时间:2017-04-19 10:46:47

标签: android scroll android-recyclerview infinite-scroll

我要在我的第一页上制作网站应用并加载Feed,但问题是滚动是无止境的,如何阻止这个?滚动是无止境的,所以我想阻止它。

    public class FeedActivity extends AppCompatActivity
{
    private SwipeRefreshLayout swipeLayout;
    private RecyclerView listView;

    private LinearLayoutManager layoutManager;

    private FeedAdapter adapter;
    private FeedService service;
    private FeedModel model;

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

        ActionBar actionBar = getSupportActionBar();
        if(null != actionBar)
        {
            actionBar.setDisplayShowHomeEnabled(true);
            actionBar.setIcon(R.mipmap.ic_launcher);
        }

        model = new FeedModel();
        adapter = new FeedAdapter(model);
        service = FeedService.create(model);

        layoutManager = new LinearLayoutManager(this);

        swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);

        listView = (RecyclerView) findViewById(R.id.listView);
        listView.setLayoutManager(layoutManager);
        listView.setAdapter(adapter);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);

    }

    @Override
    protected void onStart()
    {
        super.onStart();

        model.subscribe(onDataChangedListener);
        adapter.setOnItemClickListener(onItemClickListener);
        swipeLayout.setOnRefreshListener(onRefreshListener);

        service.fetchOlder();
    }

    @Override
    protected void onStop()
    {
        super.onStop();

        model.unSubscribe(onDataChangedListener);
        adapter.setOnItemClickListener(null);
        swipeLayout.setOnRefreshListener(null);

    }

    FeedModel.OnDataChangedListener onDataChangedListener = new FeedModel.OnDataChangedListener()
    {
        @Override
        public void onFetchedAndAddedToTop(int count)
        {
            swipeLayout.setRefreshing(false);
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFetchedAndAddedToBottom(int count)
        {
            if(count == 0)
            {
                adapter.setFooterEnabled(false);
            }

            adapter.notifyDataSetChanged();
        }
    };

    SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener()
    {
        @Override
        public void onRefresh()
        {
            service.fetchNewer();
        }
    };


    FeedAdapter.OnItemClickListener onItemClickListener = new FeedAdapter.OnItemClickListener()
    {
        @Override
        public void onItemClick(View view)
        {
            int position = listView.getChildAdapterPosition(view);
            FeedItem item = model.get(position);

            Intent intent = new Intent(getApplicationContext(), PostActivity.class);
            intent.putExtra("id", item.getId());

            startActivity(intent);

//            System.out.println("Click: "+item.getTitle());
        }
    };
}

2 个答案:

答案 0 :(得分:0)

您可以通过两种方式停止滚动

  1. 如果您知道每次刷新时刷新了多少项,那么当您获得的项目少于实际项目时,请设置一个条件以停止调用刷新。

  2. 如果您没有从早期刷新中获取数据,您可以简单地制定条件以停止刷新列表。

  3. 希望这有帮助..

    @Rajesh

答案 1 :(得分:0)

停止无休止滚动应用条件。 例如,对于每个滚动20,获取结果并将其添加到列表中。然后你可以申请一个条件

if(lastFetchedItemCount!=0&&lastFetchedItemCount%20==0){
//enable scrolling
}
else{
//disable scrolling
}