如何使用InfiniteFireRecyclerViewAdapter获取在RecyclerView中选择的位置

时间:2018-04-12 20:42:47

标签: android firebase firebase-realtime-database recycler-adapter firebaseui

我正在尝试从firebase接收数据并且我做得很完美,但我想获得位置ID和它的关键!我尝试了很多,但我找不到正确的答案!

我使用的是FirebaseDatabase,Infinite-Fire库,InfiniteFireRecyclerViewAdapter和Recyclerview。

Here is a screenshot for fireDatabase

以下是InfiniteFireRecyclerViewAdapter的代码:

public class Adapter extends InfiniteFireRecyclerViewAdapter<ItemRecycleview> {

    private Context context;
    public static final int VIEW_TYPE_CONTENT = 1;
    public static final int VIEW_TYPE_FOOTER = 2;


    /**
     * This is the view holder for the simple header and footer of this example.
     */

    public class LoadingHolder extends RecyclerView.ViewHolder {
        public ProgressBar progressBar;

        public LoadingHolder(View view) {
            super(view);
            progressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
        }
    }

    private boolean loadingMore = false;

    /**
     * @param snapshots data source for this adapter.
     */
    public Adapter(InfiniteFireArray snapshots, Context context) {
        super(snapshots, 0, 1);
        this.context = context;
    }


    /**
     * @return status of load-more loading procedures
     */
    public boolean isLoadingMore() {
        return loadingMore;
    }


    public void setLoadingMore(boolean loadingMore) {
        if (loadingMore == this.isLoadingMore()) return;
        this.loadingMore = loadingMore;
        notifyItemChanged(getItemCount() - 1);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return VIEW_TYPE_FOOTER;
        }
        return VIEW_TYPE_CONTENT;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        View view;
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        switch (viewType) {
            case VIEW_TYPE_CONTENT:
                view = inflater.inflate(R.layout.recycleview_item, parent, false);
                viewHolder = new UserRecycleView(view);
                break;
            case VIEW_TYPE_FOOTER:
                view = inflater.inflate(R.layout.progress_bar, parent, false);
                viewHolder = new LoadingHolder(view);
                break;
            default:
                throw new IllegalArgumentException("Unknown type");
        }
        return viewHolder;
    }



    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        switch (viewType) {
            case VIEW_TYPE_CONTENT:
                final ItemRecycleview model = snapshots.getItem(position - indexOffset).getValue();

                UserRecycleView mHolder = (UserRecycleView) holder;
                mHolder.setImageName(model.getImageName().toString());
                mHolder.setImageURL(model.getImageURL(),getContext());


                break;
            case VIEW_TYPE_FOOTER:
                LoadingHolder footerHolder = (LoadingHolder) holder;
                footerHolder.progressBar.setVisibility((isLoadingMore()) ? View.VISIBLE : View.GONE);
                break;
            default:
                throw new IllegalArgumentException("Unknown type");
        }
    }


}

UserRecycleView.java:

public static class UserRecycleView extends RecyclerView.ViewHolder {
    View mView;

    public UserRecycleView(View itemView) {
        super(itemView);
        mView = itemView;

    }
    public void setImageName(String imageName){

        TextView userNameView = (TextView) mView.findViewById(R.id.ImageNameTextView);
        userNameView.setText(imageName);

    }
    public void setImageURL(final String imageURL,final Context ctx){

        final ImageView userImageView = (ImageView) mView.findViewById(R.id.imageView);
        Picasso.with(ctx).load(imageURL).networkPolicy(NetworkPolicy.OFFLINE).into(userImageView, new Callback() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onError() {
                Picasso.with(ctx).load(imageURL).into(userImageView);
            }
        });

    }
}

1 个答案:

答案 0 :(得分:0)

获取点击视图的位置

          String id = String.valueOf(mHolder.getItemId());

在数据库中获取数据ID

final DatabaseReference postRef = getRef(position);
final String cheeseKey = postRef.getKey();