FirebaseRecyclerAdapter多个viewHolder

时间:2016-08-02 14:56:35

标签: android firebase firebase-realtime-database firebaseui

我正在尝试使用FirebaseRecyclerAdapterRecyclerView来显示不同类型的项目。特别是用户对特定股票的股票交易。根据我的理解,我已经延长FirebaseRecyclerAdapter,但它不起作用。以下是代码:

public abstract class StockTransactionFirebaseRecyclerAdapter extends
        FirebaseRecyclerAdapter<StockTransactionType, TransactionViewHolder>
        implements ListenerInterface
{
    private static final String TAG = "StockTxnAdapter";
    private DatabaseReference mFirebaseDatabaseReference;
    private FirebaseListenerManager mFirebaseListenerManager;
    private String mUserId;

    public StockTransactionFirebaseRecyclerAdapter (Query ref, DatabaseReference databaseReference,
                                                    String userId)
    {
        // The layout portfolio_buy_transaction_item is just a dummy since we are
        // inflating our own layouts when required.
        super (StockTransactionType.class, R.layout.portfolio_buy_transaction_item,
                TransactionViewHolder.class, ref);

        mFirebaseDatabaseReference = databaseReference;
        mFirebaseListenerManager = new FirebaseListenerManager ();
        mUserId = userId;
        Log.e (TAG, "Inside StockTransactionFirebaseRecyclerAdapter");
    }

    @Override
    public int getItemViewType (int position)
    {
        StockTransactionType stockTransactionType = getItem (position);
        Log.e (TAG, "Inside getItemViewType (" + position + "): " + stockTransactionType.returnType ());
        return stockTransactionType.returnType ();
    }

    @Override
    public TransactionViewHolder onCreateViewHolder (ViewGroup parent, int viewType)
    {
        Log.e (TAG, "onCreateViewHolder for " + viewType);
        View itemView;
        switch (viewType)
        {
            case StockTransactionType.TRANSACTION_TYPE_BUY:
                itemView = LayoutInflater.from (parent.getContext ())
                        .inflate (R.layout.portfolio_buy_transaction_item, parent, false);
                return new BuyTransactionViewHolder (itemView);
            case StockTransactionType.TRANSACTION_TYPE_SELL:
                itemView = LayoutInflater.from (parent.getContext ())
                        .inflate (R.layout.portfolio_sell_transaction_item, parent, false);
                return new SellTransactionViewHolder (itemView);
            case StockTransactionType.TRANSACTION_TYPE_DIVIDEND:
                itemView = LayoutInflater.from (parent.getContext ())
                        .inflate (R.layout.portfolio_dividend_transaction_item, parent, false);
                return new DividendTransactionViewHolder (itemView);
            case StockTransactionType.TRANSACTION_TYPE_SPLIT:
                itemView = LayoutInflater.from (parent.getContext ())
                        .inflate (R.layout.portfolio_split_transaction_item, parent, false);
                return new SplitTransactionViewHolder (itemView);
        }

        throw new IllegalStateException ("Invalid transaction type: " + viewType);
    }

    @Override
    protected void populateViewHolder (final TransactionViewHolder viewHolder,
                                       StockTransactionType stockTransactionType, int position)
    {
        final String transactionId = this.getRef (position).getKey ();
        final Class transaction;
        final String location;

        Log.e (TAG, "populateViewHolder: " + stockTransactionType.getClass ().getName () + " "
                + position);

        switch (stockTransactionType.returnType ())
        {
            case StockTransactionType.TRANSACTION_TYPE_BUY:
                transaction = BuyStockTransaction.class;
                location = FirebaseDatabaseUtil.CHILD_TRANSACTIONS_BUY;
                break;
            case StockTransactionType.TRANSACTION_TYPE_SELL:
                transaction = SellStockTransaction.class;
                location = FirebaseDatabaseUtil.CHILD_TRANSACTIONS_SELL;
                break;
            case StockTransactionType.TRANSACTION_TYPE_SPLIT:
                transaction = SplitStockTransaction.class;
                location = FirebaseDatabaseUtil.CHILD_TRANSACTIONS_SPLIT;
                break;
            case StockTransactionType.TRANSACTION_TYPE_DIVIDEND:
                transaction = DividendStockTransaction.class;
                location = FirebaseDatabaseUtil.CHILD_TRANSACTIONS_DIVIDEND;
                break;
            default:
                throw new IllegalStateException ("Invalid transaction type: "
                        + stockTransactionType.renameTypeString ());
        }

        DatabaseReference databaseReference = mFirebaseDatabaseReference
                .child (location).child (mUserId).child (transactionId);
        ValueEventListener valueEventListener = databaseReference
                .addValueEventListener (new ValueEventListener ()
                {
                    @Override
                    public void onDataChange (DataSnapshot dataSnapshot)
                    {
                        StockTransaction stockTransaction = (StockTransaction) dataSnapshot.getValue (transaction);
                        viewHolder.assignViewHolder (stockTransaction);
                    }

                    @Override
                    public void onCancelled (DatabaseError databaseError)
                    {
                        if (getActivity () instanceof MainActivity)
                        {
                            MainActivity mainActivity = (MainActivity) getActivity ();
                            mainActivity.showDatabaseError (TAG, Thread.currentThread ()
                                    .getStackTrace (), databaseError);
                        }
                    }
                });

        mFirebaseListenerManager.addListner (databaseReference, valueEventListener);
    }

    protected abstract Activity getActivity ();

    @Override
    public void removeAllListeners ()
    {
        mFirebaseListenerManager.removeAllListners ();
    }
}

BuyTransactionViewHolderSellTransactionViewHolder等是TransactionViewHolder的子类,而BuyStockTransactionSellStockTransaction等是StockTransaction的子类

除了构造函数之外,甚至没有函数被调用。因为我是android新手编程可能是我在做傻事。有人可以帮我理解我做错了吗?

修改

这是我初始化适配器的方式:

final DatabaseReference databaseReference = mFirebaseDatabaseReference
            .child (FirebaseDatabaseUtil.CHILD_USER_STOCK_TRANSACTIONS)
            .child (mUserId).child (mStockId);
mStockTransactionFirebaseRecyclerAdapter = new StockTransactionFirebaseRecyclerAdapter (
        databaseReference, mFirebaseDatabaseReference, mUserId)
{
    @Override
    protected Activity getActivity ()
    {
        return StockDisplayFragment.this.getActivity ();
    }
};

我希望我没有错过任何事情,这清除了所有疑虑。但如果它没有,我会很乐意添加更多信息。

1 个答案:

答案 0 :(得分:0)

确定。我实现了自己的RecyclerAdapter自定义实现,而不是FirebaseRecyclerAdapter。只是意识到它还没有奏效。但真正的问题是由于上面的textView将RecyclerView设置为layout_heightmatch_parent不可见。所以这一直是我的愚蠢错误。