2017-04-19 51 views
0

我打算在第一頁製作網站應用程序和加載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()); 
     } 
    }; 
} 

回答

0

你可以停在兩個方面

  1. 滾動如果你知道有多少項目被刷新,每刷新你做,然後做一個條件來停止調用刷新,當你得到比實際的項目少。

  2. 當您沒有從早期刷新中獲取數據時,您可以簡單地制定一個條件來停止刷新列表。

希望這是很有幫助的..

@Rajesh

0

要停止無休止的滾動申請的條件。 例如,每次滾動20個結果都會被提取並添加到列表中。那麼你可以申請一個條件

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