Using Etsy Android Staggered gridview

混江龙づ霸主 提交于 2019-12-25 04:24:39

问题


Iam using https://github.com/etsy/AndroidStaggeredGrid to achieve a StaggeredGridView for my app. I get a NullPointerException when I try to initialize my DynamicHeightImageView.

I cant figure out what is going wrong.

Here is my code:-

row_staggered.xml

<com.etsy.android.grid.util.DynamicHeightImageView
        android:id="@+id/imgViewDynamic"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

StaggeredAdapter.java

     static class LazyViewHolder
         {

             DynamicHeightImageView thumb_image;

         }
    @Override
         public View getView(int position, View convertView, ViewGroup parent)
        {

            LazyViewHolder viewHolder;

            if(convertView==null)
                convertView = inflater.inflate(R.layout.row_staggered,null);

           viewHolder = new LazyViewHolder();
         viewHolder.thumb_image=(DynamicHeightImageView)convertView.findViewById(R.id.imgViewDynamic); // thumb image

    imageLoader.DisplayImage(bitmapUrl, viewHolder.thumb_image); //Error here Line 215
}

ExtendableListView.java

     /**
     * Get a view and have it show the data associated with the specified
     * position. This is called when we have already discovered that the view is
     * not available for reuse in the recycle bin. The only choices left are
     * converting an old view or making a new one.
     *
     * @param position The position to display
     * @param isScrap  Array of at least 1 boolean, the first entry will become true if
     *                 the returned view was taken from the scrap heap, false if otherwise.
     * @return A view displaying the data associated with the specified position
     */
    private View obtainView(int position, boolean[] isScrap) {
        isScrap[0] = false;
        View scrapView;

        scrapView = mRecycleBin.getScrapView(position);

        View child;
        if (scrapView != null) {
            if (DBG) Log.d(TAG, "getView from scrap position:" + position);
            child = mAdapter.getView(position, scrapView, this);

            if (child != scrapView) {
                mRecycleBin.addScrapView(scrapView, position);
            }
            else {
                isScrap[0] = true;
            }
        }
        else {
            if (DBG) Log.d(TAG, "getView position:" + position);
            child = mAdapter.getView(position, null, this); //Error here
        }

        return child;
    }
 /**
     * Gets a view either a new view an unused view?? or a recycled view and adds it to our children
     */
    private View makeAndAddView(int position, int y, boolean flowDown, boolean selected) {
        View child;

        onChildCreated(position, flowDown);

        if (!mDataChanged) {
            // Try to use an existing view for this position
            child = mRecycleBin.getActiveView(position);
            if (child != null) {

                // Found it -- we're using an existing child
                // This just needs to be positioned
                setupChild(child, position, y, flowDown, selected, true);
                return child;
            }
        }

        // Make a new view for this position, or convert an unused view if possible
        child = obtainView(position, mIsScrap); //ERROR HERE
        // This needs to be positioned and measured
        setupChild(child, position, y, flowDown, selected, mIsScrap[0]);

        return child;
    }

 private View fillDown(int pos, int nextTop) {
        if (DBG) Log.d(TAG, "fillDown - pos:" + pos + " nextTop:" + nextTop);

        View selectedView = null;

        int end = getHeight();
        if (mClipToPadding) {
            end -= getListPaddingBottom();
        }

        while ((nextTop < end || hasSpaceDown()) && pos < mItemCount) {
            // TODO : add selection support
            makeAndAddView(pos, nextTop, true, false);
            pos++;
            nextTop = getNextChildDownsTop(pos); // = child.getBottom();
        }

        return selectedView;
    }
 /**
     * Fills the list from top to bottom, starting with mFirstPosition
     */
    private View fillFromTop(int nextTop) {
        mFirstPosition = Math.min(mFirstPosition, mItemCount - 1);
        if (mFirstPosition < 0) {
            mFirstPosition = 0;
        }
        return fillDown(mFirstPosition, nextTop);
    }

            switch (mLayoutMode) {
                case LAYOUT_FORCE_TOP: {
                    mFirstPosition = 0;
                    resetToTop();
                    adjustViewsUpOrDown();
                    fillFromTop(childrenTop);
                    adjustViewsUpOrDown();
                    break;
                }

I get an error:-

06-10 05:05:23.389: E/AndroidRuntime(2178): java.lang.NullPointerException
06-10 05:05:23.389: E/AndroidRuntime(2178): at Utility.StaggeredAdapter.getView(StaggeredAdapter.java:215)
06-10 05:05:23.389: E/AndroidRuntime(2178): at com.etsy.android.grid.ExtendableListView.obtainView(ExtendableListView.java:1578)
06-10 05:05:23.389: E/AndroidRuntime(2178): at com.etsy.android.grid.ExtendableListView.makeAndAddView(ExtendableListView.java:1402)
06-10 05:05:23.389: E/AndroidRuntime(2178): at com.etsy.android.grid.ExtendableListView.fillDown(ExtendableListView.java:1288)
06-10 05:05:23.389: E/AndroidRuntime(2178): at com.etsy.android.grid.ExtendableListView.fillFromTop(ExtendableListView.java:1336)
06-10 05:05:23.389: E/AndroidRuntime(2178): at com.etsy.android.grid.ExtendableListView.layoutChildren(ExtendableListView.java:587)

回答1:


Try This in your **StaggeredAdapter.java ** class

 ImageLoader imageloader = new ImageLoader(); 
 // defining value to imageLoader


来源:https://stackoverflow.com/questions/24133456/using-etsy-android-staggered-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!