如何从recyclerview位置获取firebase节点的键值?

时间:2019-06-08 16:20:17

标签: java android

我使用OnScrollListener进行recyclerview来获取当前可见项目在屏幕上的位置

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

        postList=(RecyclerView)findViewById(R.id.all_user_post_list);
        final LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        postList.setLayoutManager(linearLayoutManager);

        postList.addOnScrollListener(new RecyclerView.OnScrollListener(){
@Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView,int newState){
        super.onScrollStateChanged(recyclerView,newState);

        switch(newState){
        case RecyclerView.SCROLL_STATE_IDLE:
        System.out.println("The RecyclerView is not scrolling");
        LinearLayoutManager layoutManager=((LinearLayoutManager)postList.getLayoutManager());

       firstVisiblePosition=layoutManager.findFirstVisibleItemPosition();

    Toast.makeText(MainActivity.this,String.valueOf(firstVisiblePosition),Toast.LENGTH_SHORT).show();**//Here i get the position//**
        break;
        case RecyclerView.SCROLL_STATE_DRAGGING:
        System.out.println("Scrolling now");
        break;
        case RecyclerView.SCROLL_STATE_SETTLING:
        System.out.println("Scroll Settling");
        break;
        }
        }

@Override
public void onScrolled(@NonNull RecyclerView recyclerView,int dx,int dy){
        super.onScrolled(recyclerView,dx,dy);
        }
        });

        Query sortPost=PostRef
        FirebaseRecyclerOptions<Post> options=new FirebaseRecyclerOptions.Builder<Post>().setQuery(sortPost,Post.class).build();
        FirebaseRecyclerAdapter<Post, PostsViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Post, PostsViewHolder>(options)
        {
@Override
protected void onBindViewHolder(@NonNull final PostsViewHolder holder, final int position, @NonNull final Post model)
        {

final String PostKey=getRef(position).getKey();
        }

在onBindViewHolder方法中,我从getRef()方法中检索了邮政编码。

但是现在我也想从recyclerview检索邮编。 意味着每当SCROLL_STATE_IDLE我要从recyclerview可见位置(firstVisiblePosition)检索邮编时。

0 个答案:

没有答案