2016-07-21 142 views
0

這是我第一次使用在線回收站查看。在滾動或滾動後向RecyclerView添加項目

我有一些問題:

我1.how可以區分向上滾動和向下滾動

我2.how可以通過向上滾動更新我的列表,並在同一時間不會失去我的其他項目

3.更新recyclerview後,如何保持我的位置,不回到第一位?

我爲recyclerview編寫了這些代碼。 CustomListAdapterForPostOrgan後

private void memberorganhandel(List<User> userList, Page pages) { 

    if(pages.getCurrentPage()>1){ 

    }else{ 

     recyclerview_personal_organ .setLayoutManager(new LinearLayoutManager(this)); 
     AdapterRcycelerViewPersonalOrgan 
       madapter=new 
       AdapterRcycelerViewPersonalOrgan(userList); 
     recyclerview_personal_organ .setAdapter(madapter); 
     recyclerview_personal_organ .setItemAnimator(new DefaultItemAnimator()); 
    } 
} 

回答

0
  • public class AdapterRcycelerViewPersonalOrgan extends RecyclerView.Adapter<AdapterRcycelerViewPersonalOrgan.ViewHolder> { 
    private List<User> itemsData; 
    public AdapterRcycelerViewPersonalOrgan(List<User> itemsData) { 
        this.itemsData = itemsData; 
    } 
    
    // Create new views (invoked by the layout manager) 
    @Override 
    public AdapterRcycelerViewPersonalOrgan.ViewHolder onCreateViewHolder(ViewGroup parent, 
                         int viewType) { 
        // create a new view 
        View itemLayoutView = LayoutInflater.from(parent.getContext()) 
          .inflate(R.layout.list_row_member_organ, null); 
    
        // create ViewHolder 
    
        ViewHolder viewHolder = new ViewHolder(itemLayoutView); 
        return viewHolder; 
    } 
    
    // Replace the contents of a view (invoked by the layout manager) 
    @Override 
    public void onBindViewHolder(ViewHolder viewHolder, int position) { 
        ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 
        // - get data from your itemsData at this position 
        // - replace the contents of the view with that itemsData 
        if (imageLoader == null) 
         imageLoader = AppController.getInstance().getImageLoader(); 
    
        viewHolder.member__avatar_show_thumbnail.setImageUrl(itemsData.get(position).getAvatar(),imageLoader); 
        viewHolder.member_name.setText(itemsData.get(position).getFirst_name()+" "+itemsData.get(position).getLast_name()); 
    
    
    } 
    
    // inner class to hold a reference to each item of RecyclerView 
    public static class ViewHolder extends RecyclerView.ViewHolder { 
    
    public NetworkImageView member__avatar_show_thumbnail; 
    
    public TextView member_name; 
    
        public ViewHolder(View itemLayoutView) { 
         super(itemLayoutView); 
    
         member__avatar_show_thumbnail = (NetworkImageView) itemLayoutView.findViewById(R.id.member__avatar_show_thumbnail); 
         member_name=(TextView) itemLayoutView.findViewById(R.id.txt_member_name_list_member_organ) ; 
    
        } 
    } 
    
    
    // Return the size of your itemsData (invoked by the layout manager) 
    @Override 
    public int getItemCount() { 
        return itemsData.size(); 
        } 
    } 
    

    和我怎樣才能dinstinguish向上滾動和向下滾動

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { 

    @Override 
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
     //check if dy > 0 then it's scrolling down, otherwise - up 
    } 
}); 
  • 我如何可以使用滾動起來,並在同一時間更新我的列表不會失去我的其他項目

onScrolled方法從我第一次代碼修改snippent數據的來源,你在你的適配器使用,並然後致電notifyItemInserted。文檔。

  • 更新recyclerview後,我該如何保持自己的位置,不要回到第一個位置?

看到我的第二點。它不應該導致任何滾動。

+0

我應該在哪裏調用這個函數?我在oncreate中編寫這個函數,但它沒有工作 –