Beispiel #1
0
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
   // Header (negative positions will throw an
   // ArrayIndexOutOfBoundsException)
   int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns;
   if (position < numHeadersAndPlaceholders) {
     View headerViewContainer = mHeaderViewInfos.get(position / mNumColumns).viewContainer;
     if (position % mNumColumns == 0) {
       return headerViewContainer;
     } else {
       if (convertView == null) {
         convertView = new View(parent.getContext());
       }
       // We need to do this because GridView uses the height of
       // the last item
       // in a row to determine the height for the entire row.
       convertView.setVisibility(View.INVISIBLE);
       convertView.setMinimumHeight(headerViewContainer.getHeight());
       return convertView;
     }
   }
   // Adapter
   final int adjPosition = position - numHeadersAndPlaceholders;
   int adapterCount = 0;
   if (mAdapter != null) {
     adapterCount = mAdapter.getCount();
     if (adjPosition < adapterCount) {
       return mAdapter.getView(adjPosition, convertView, parent);
     }
   }
   throw new ArrayIndexOutOfBoundsException(position);
 }
  private View getBookmarkFolderView(String folderName) {
    openAboutHomeTab(AboutHomeTabs.BOOKMARKS);
    mSolo.hideSoftKeyboard();
    getInstrumentation().waitForIdleSync();

    ListView bookmarksTabList = findListViewWithTag(HomePager.LIST_TAG_BOOKMARKS);
    if (!waitForNonEmptyListToLoad(bookmarksTabList)) {
      return null;
    }

    ListAdapter adapter = bookmarksTabList.getAdapter();
    if (adapter == null) {
      return null;
    }

    for (int i = 0; i < adapter.getCount(); i++) {
      View bookmarkView = bookmarksTabList.getChildAt(i);
      if (bookmarkView instanceof TextView) {
        TextView folderTextView = (TextView) bookmarkView;
        if (folderTextView.getText().equals(folderName)) {
          return bookmarkView;
        }
      }
    }

    return null;
  }
Beispiel #3
0
  public void setListViewHeightBasedOnChildren(ListView listView, int offset) {
    // 获取ListView对应的Adapter
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
      return;
    }

    int totalHeight = 0;
    for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
      View listItem = listAdapter.getView(i, null, listView);
      listItem.measure(0, 0); // 计算子项View 的宽高
      // listItem.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
      // MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
      // listItem.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.AT_MOST),
      // MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
      totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height =
        totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)) + offset;
    // listView.getDividerHeight()获取子项间分隔符占用的高度
    // params.height最后得到整个ListView完整显示需要的高度
    listView.setLayoutParams(params);
  }
Beispiel #4
0
  /**
   * Sets ListView height dynamically based on the height of the items.
   *
   * @param listView to be resized
   * @return true if the listView is successfully resized, false otherwise
   */
  public static boolean setListViewHeightBasedOnItems(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter != null) {

      int numberOfItems = listAdapter.getCount();

      // Get total height of all items.
      int totalItemsHeight = 0;
      for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
        View item = listAdapter.getView(itemPos, null, listView);
        item.measure(0, 0);
        totalItemsHeight += item.getMeasuredHeight();
      }

      // Get total height of all item dividers.
      int totalDividersHeight = listView.getDividerHeight() * (numberOfItems - 1);

      // Set list height.
      ViewGroup.LayoutParams params = listView.getLayoutParams();
      params.height = totalItemsHeight + totalDividersHeight;
      listView.setLayoutParams(params);
      listView.requestLayout();
      return true;

    } else {
      return false;
    }
  }
  @Override
  public int getSectionForPosition(int position) {
    int section = 0;

    for (ListAdapter piece : pieces) {
      int size = piece.getCount();

      if (position < size) {
        if (piece instanceof SectionIndexer) {
          return (section + ((SectionIndexer) piece).getSectionForPosition(position));
        }

        return (0);
      } else {
        if (piece instanceof SectionIndexer) {
          Object[] sections = ((SectionIndexer) piece).getSections();

          if (sections != null) {
            section += sections.length;
          }
        }
      }

      position -= size;
    }

    return (0);
  }
Beispiel #6
0
  /**
   * @param adapter a regular ListAdapter, not all methods if the list adapter are used by the
   *     flipview
   */
  public void setAdapter(ListAdapter adapter) {
    if (mAdapter != null) {
      mAdapter.unregisterDataSetObserver(dataSetObserver);
    }

    // remove all the current views
    removeAllViews();

    mAdapter = adapter;
    mPageCount = adapter == null ? 0 : mAdapter.getCount();

    if (adapter != null) {
      mAdapter.registerDataSetObserver(dataSetObserver);

      mRecycler.setViewTypeCount(mAdapter.getViewTypeCount());
      mRecycler.invalidateScraps();
    }

    // TODO pretty confusing
    // this will be correctly set in setFlipDistance method
    mCurrentPageIndex = INVALID_PAGE_POSITION;
    mFlipDistance = INVALID_FLIP_DISTANCE;
    setFlipDistance(0);

    updateEmptyStatus();
  }
        @Override
        public void onLongPress(MotionEvent e) {
          Rect viewRect = new Rect();
          for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            int left = child.getLeft();
            int right = child.getRight();
            int top = child.getTop();
            int bottom = child.getBottom();
            viewRect.set(left, top, right, bottom);
            if (viewRect.contains((int) e.getX(), (int) e.getY())) {

              if (mOnItemLongClickListener != null) {
                mOnItemLongClickListener.onItemLongClick(
                    HorizontalListView.this,
                    child,
                    mLeftViewIndex + 1 + i,
                    mAdapter.getItemId(mLeftViewIndex + 1 + i));
              }
              if (mOnItemSelected != null) {
                mOnItemSelected.onItemSelected(
                    HorizontalListView.this,
                    child,
                    mLeftViewIndex + 1 + i,
                    mAdapter.getItemId(mLeftViewIndex + 1 + i));
              }
              break;
            }
          }
        }
  public boolean onTestReceive(MotionEvent e) {

    if (e.getAction() == MotionEvent.ACTION_UP) {
      Rect viewRect = new Rect();
      for (int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);
        int left = child.getLeft();
        int right = child.getRight();
        int top = child.getTop() + this.getTop();
        int bottom = child.getBottom() + this.getBottom();
        int x = (int) e.getX();
        int y = (int) e.getY();
        viewRect.set(left, top, right, bottom);
        if (viewRect.contains(x, y)) {
          if (mOnItemClicked != null) {
            mOnItemClicked.onItemClick(
                HorizontalListView.this,
                child,
                mLeftViewIndex + 1 + i,
                mAdapter.getItemId(mLeftViewIndex + 1 + i));
          }
          if (mOnItemSelected != null) {
            mOnItemSelected.onItemSelected(
                HorizontalListView.this,
                child,
                mLeftViewIndex + 1 + i,
                mAdapter.getItemId(mLeftViewIndex + 1 + i));
          }
          break;
        }
      }
      return true;
    }
    return false;
  }
Beispiel #9
0
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int newHeight = 0;
    final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    if (heightMode != MeasureSpec.EXACTLY) {
      ListAdapter listAdapter = getAdapter();
      if (listAdapter != null && !listAdapter.isEmpty()) {
        int listPosition = 0;
        for (listPosition = 0;
            listPosition < listAdapter.getCount() && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE;
            listPosition++) {
          View listItem = listAdapter.getView(listPosition, null, this);
          // now it will not throw a NPE if listItem is a ViewGroup instance
          if (listItem instanceof ViewGroup) {
            listItem.setLayoutParams(
                new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
          }
          listItem.measure(widthMeasureSpec, heightMeasureSpec);
          newHeight += listItem.getMeasuredHeight();
        }
        newHeight += getDividerHeight() * listPosition;
      }
      if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
        if (newHeight > heightSize) {
          newHeight = heightSize;
        }
      }
    } else {
      newHeight = getMeasuredHeight();
    }
    setMeasuredDimension(getMeasuredWidth(), newHeight);
  }
Beispiel #10
0
  private void fillListRight(int rightEdge, final int dx) {
    // Loop adding views to the right until the screen is filled
    while (rightEdge + dx + mDividerWidth < getWidth()
        && mRightViewAdapterIndex + 1 < mAdapter.getCount()) {
      mRightViewAdapterIndex++;

      // If mLeftViewAdapterIndex < 0 then this is the first time a view is being added, and left ==
      // right
      if (mLeftViewAdapterIndex < 0) {
        mLeftViewAdapterIndex = mRightViewAdapterIndex;
      }

      // Get the view from the adapter, utilizing a cached view if one is available
      View child =
          mAdapter.getView(mRightViewAdapterIndex, getRecycledView(mRightViewAdapterIndex), this);
      addAndMeasureChild(child, INSERT_AT_END_OF_LIST);

      // If first view, then no divider to the left of it, otherwise add the space for the divider
      // width
      rightEdge += (mRightViewAdapterIndex == 0 ? 0 : mDividerWidth) + child.getMeasuredWidth();

      // Check if we are running low on data so we can tell listeners to go get more
      determineIfLowOnData();
    }
  }
  private View viewForPage(int page) {

    // if the requested page is outside of the adapters scope, return null.
    // This should only happen when over flipping with mode RUBBER_BAND
    if (page < 0 || page >= mPageCount) {
      return null;
    }

    final int viewType = mAdapter.getItemViewType(page);

    // check if view needed is in active views, if so order to front and
    // return this view
    View v = getActiveView(page);
    if (v != null) {
      return v;
    }

    // get(and remove) a convertview with correct viewtype from recycled
    // views
    v = mRecycler.getScrapView(page, viewType);

    // pass that view (can be null) into adapter to fill the view
    v = mAdapter.getView(page, v, this);

    // insert that view into active views pushing the least used active view
    // into recycled views
    addToActiveView(v, page, viewType);

    // measure and layout view
    measureAndLayoutChild(v);

    // return view
    return v;
  }
  private void dataSetChanged() {
    final int currentPage = mCurrentPage;

    // if the adapter has stable ids, try to keep the page currently on
    // stable.
    if (mAdapter.hasStableIds() && mCurrentPage != INVALID_PAGE_POSITION) {
      mCurrentPage = getNewPositionOfCurrentPage();
    } else if (mCurrentPage == INVALID_PAGE_POSITION) {
      mCurrentPage = 0;
    }

    // remove all the current views
    removeAllViews();
    mActivePageQueue.clear();

    mPageCount = mAdapter.getCount();

    // put the current page within the new adapter range
    mCurrentPage =
        Math.min(mPageCount - 1, mCurrentPage == INVALID_PAGE_POSITION ? 0 : mCurrentPage);

    if (mCurrentPage != INVALID_PAGE_POSITION) {
      mRecycler.setViewTypeCount(mAdapter.getViewTypeCount());
      mCurrentPageId = mAdapter.getItemId(mCurrentPage);
      addView(viewForPage(mCurrentPage));
      if (mCurrentPage != currentPage) {
        flipTo(mCurrentPage);
      }
    } else {
      mPageCount = 0;
      mFlipDistance = 0;
    }

    updateEmptyStatus();
  }
  /**
   * @param adapter a regular ListAdapter, not all methods if the list adapter are used by the
   *     flipview
   */
  public void setAdapter(ListAdapter adapter) {
    if (mAdapter != null) {
      mAdapter.unregisterDataSetObserver(dataSetObserver);
    }

    // remove all the current views
    removeAllViews();
    mActivePageQueue.clear();

    mAdapter = adapter;
    mPageCount = adapter == null ? 0 : mAdapter.getCount();

    // put the current page within the new adapter range
    mCurrentPage =
        Math.min(mPageCount - 1, mCurrentPage == INVALID_PAGE_POSITION ? 0 : mCurrentPage);

    if (adapter != null) {
      mAdapter.registerDataSetObserver(dataSetObserver);
    }

    if (mCurrentPage != INVALID_PAGE_POSITION) {
      mRecycler.setViewTypeCount(mAdapter.getViewTypeCount());
      mCurrentPageId = mAdapter.getItemId(mCurrentPage);
      addView(viewForPage(mCurrentPage));
    } else {
      mPageCount = 0;
      mFlipDistance = 0;
    }

    updateEmptyStatus();
  }
  private int measureContentWidth() {
    // Menus don't tend to be long, so this is more sane than it looks.
    int maxWidth = 0;
    View itemView = null;
    int itemType = 0;

    final ListAdapter adapter = mAdapter;
    final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    final int count = adapter.getCount();
    for (int i = 0; i < count; i++) {
      final int positionType = adapter.getItemViewType(i);
      if (positionType != itemType) {
        itemType = positionType;
        itemView = null;
      }

      if (mMeasureParent == null) {
        mMeasureParent = new FrameLayout(mContext);
      }

      itemView = adapter.getView(i, itemView, mMeasureParent);
      itemView.measure(widthMeasureSpec, heightMeasureSpec);

      final int itemWidth = itemView.getMeasuredWidth();
      if (itemWidth >= mPopupMaxWidth) {
        return mPopupMaxWidth;
      } else if (itemWidth > maxWidth) {
        maxWidth = itemWidth;
      }
    }

    return maxWidth;
  }
Beispiel #15
0
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
   SwipeMenuLayout layout = null;
   if (convertView == null) {
     View contentView = mAdapter.getView(position, convertView, parent);
     SwipeMenu menu = new SwipeMenu(mContext);
     menu.setViewType(mAdapter.getItemViewType(position));
     createMenu(menu);
     SwipeMenuView menuView = new SwipeMenuView(menu, (SwipeMenuListView) parent);
     menuView.setOnSwipeItemClickListener(this);
     SwipeMenuListView listView = (SwipeMenuListView) parent;
     layout =
         new SwipeMenuLayout(
             contentView,
             menuView,
             listView.getCloseInterpolator(),
             listView.getOpenInterpolator());
     layout.setPosition(position);
   } else {
     layout = (SwipeMenuLayout) convertView;
     layout.closeMenu();
     layout.setPosition(position);
     View view = mAdapter.getView(position, layout.getContentView(), parent);
   }
   return layout;
 }
  public static void setListViewHeightBasedOnChildren(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();

    if (listAdapter == null) return;

    int desiredWidth =
        View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);

    int totalHeight = 0;

    View view = null;

    for (int i = 0; i < listAdapter.getCount(); i++) {

      view = listAdapter.getView(i, view, listView);

      if (i == 0)
        view.setLayoutParams(
            new ViewGroup.LayoutParams(desiredWidth, AbsListView.LayoutParams.MATCH_PARENT));

      view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

      totalHeight += view.getMeasuredHeight() + 20;
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();

    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

    listView.setLayoutParams(params);
  }
  public void fixListViewHeight(ListView listView) {
    // 如果没有设置数据适配器,则ListView没有子项,返回。
    ListAdapter listAdapter = listView.getAdapter();
    int totalHeight = 0;
    if (listAdapter == null) {
      return;
    }
    for (int index = 0, len = listAdapter.getCount(); index < len; index++) {
      View listViewItem = listAdapter.getView(index, null, listView);
      // 计算子项View 的宽高
      // listViewItem.measure(0, 0);
      listViewItem.measure(
          View.MeasureSpec.makeMeasureSpec(
              getResources().getDisplayMetrics().widthPixels, View.MeasureSpec.EXACTLY),
          0);
      // 计算所有子项的高度和
      int height = 0;
      if (listViewItem.getHeight() > listViewItem.getMeasuredHeight())
        height = listViewItem.getHeight();
      else height = listViewItem.getMeasuredHeight();

      totalHeight += height;
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    // listView.getDividerHeight()获取子项间分隔符的高度
    // params.height设置ListView完全显示需要的高度
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
  }
  public int getRowHeight() {
    if (mRowHeight > 0) {
      return mRowHeight;
    }
    ListAdapter adapter = getAdapter();
    int numColumns = getNumColumnsCompatible();

    // adapter has not been set or has no views in it;
    if (adapter == null
        || adapter.getCount() <= numColumns * (mHeaderViewInfos.size() + mFooterViewInfos.size())) {
      return -1;
    }
    int mColumnWidth = getColumnWidthCompatible();
    View view =
        getAdapter().getView(numColumns * mHeaderViewInfos.size(), mViewForMeasureRowHeight, this);
    AbsListView.LayoutParams p = (AbsListView.LayoutParams) view.getLayoutParams();
    if (p == null) {
      p = new AbsListView.LayoutParams(-1, -2, 0);
      view.setLayoutParams(p);
    }
    int childHeightSpec =
        getChildMeasureSpec(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 0, p.height);
    int childWidthSpec =
        getChildMeasureSpec(
            MeasureSpec.makeMeasureSpec(mColumnWidth, MeasureSpec.EXACTLY), 0, p.width);
    view.measure(childWidthSpec, childHeightSpec);
    mViewForMeasureRowHeight = view;
    mRowHeight = view.getMeasuredHeight();
    return mRowHeight;
  }
    @Override
    public Object getItem(int position) {
      // Header (negative positions will throw an ArrayIndexOutOfBoundsException)
      int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns;
      if (position < numHeadersAndPlaceholders) {
        if (position % mNumColumns == 0) {
          return mHeaderViewInfos.get(position / mNumColumns).data;
        }
        return null;
      }

      // Adapter
      final int adjPosition = position - numHeadersAndPlaceholders;
      int adapterCount = 0;
      if (mAdapter != null) {
        adapterCount = getAdapterAndPlaceHolderCount();
        if (adjPosition < adapterCount) {
          if (adjPosition < mAdapter.getCount()) {
            return mAdapter.getItem(adjPosition);
          } else {
            return null;
          }
        }
      }

      // Footer (off-limits positions will throw an IndexOutOfBoundsException)
      final int footerPosition = adjPosition - adapterCount;
      if (footerPosition % mNumColumns == 0) {
        return mFooterViewInfos.get(footerPosition).data;
      } else {
        return null;
      }
    }
  protected String getDocumentTitle(int position) throws Exception {
    ArrayList<ListView> listViews = solo.getCurrentListViews();
    if (listViews == null || listViews.size() == 0) {
      return "No List View";
    }
    ListView listview = listViews.get(0);
    ListAdapter adapter = listview.getAdapter();

    Method method = null;

    try {
      method = adapter.getClass().getMethod("getDocumentAttribute", Integer.class, String.class);
    } catch (NoSuchMethodException e) {
      throw new RuntimeException(
          "Unable to find test method getDocumentAttribute on adapter "
              + adapter.getClass().getName());
      // Log.e(this.getClass().getSimpleName(),
      // "Unable to find test method getDocumentAttribute on adapter " +
      // adapter.getClass().getName());
    }

    if (method != null) {
      String value = (String) method.invoke(adapter, position, "dc:title");
      if (value == null) {
        throw new RuntimeException("Null title returned");
      }
      return value;
    } else {
      return null;
    }
  }
 @Override
 public boolean onSingleTapConfirmed(MotionEvent e) {
   Rect viewRect = new Rect();
   for (int i = 0; i < getChildCount(); i++) {
     View child = getChildAt(i);
     int left = child.getLeft();
     int right = child.getRight();
     int top = child.getTop();
     int bottom = child.getBottom();
     viewRect.set(left, top, right, bottom);
     if (viewRect.contains((int) e.getX(), (int) e.getY())) {
       if (mOnItemClicked != null) {
         mOnItemClicked.onItemClick(
             HorizontalListView.this,
             child,
             mLeftViewIndex + 1 + i,
             mAdapter.getItemId(mLeftViewIndex + 1 + i));
       }
       if (mOnItemSelected != null) {
         mOnItemSelected.onItemSelected(
             HorizontalListView.this,
             child,
             mLeftViewIndex + 1 + i,
             mAdapter.getItemId(mLeftViewIndex + 1 + i));
       }
       break;
     }
   }
   return true;
 }
        @Override
        public void onScroll(
            AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

          if (mDelegateOnScrollListener != null) { // delegate
            mDelegateOnScrollListener.onScroll(
                view, firstVisibleItem, visibleItemCount, totalItemCount);
          }

          // get expected adapter or fail fast
          ListAdapter adapter = getAdapter();
          if (adapter == null || visibleItemCount == 0) return; // nothing to do

          final boolean isFirstVisibleItemSection =
              isItemViewTypePinned(adapter, adapter.getItemViewType(firstVisibleItem));

          if (isFirstVisibleItemSection) {
            View sectionView = getChildAt(0);
            if (sectionView.getTop()
                == getPaddingTop()) { // view sticks to the top, no need for pinned shadow
              destroyPinnedShadow();
            } else { // section doesn't stick to the top, make sure we have a pinned shadow
              ensureShadowForPosition(firstVisibleItem, firstVisibleItem, visibleItemCount);
            }

          } else { // section is not at the first visible position
            int sectionPosition = findCurrentSectionPosition(firstVisibleItem);
            if (sectionPosition > -1) { // we have section position
              ensureShadowForPosition(sectionPosition, firstVisibleItem, visibleItemCount);
            } else { // there is no section for the first visible item, destroy shadow
              destroyPinnedShadow();
            }
          }
        };
  protected Object getDocumentCreationDate(int position) throws Exception {
    ArrayList<ListView> listViews = solo.getCurrentListViews();
    if (listViews == null || listViews.size() == 0) {
      return null;
    }
    ListView listview = listViews.get(0);
    ListAdapter adapter = listview.getAdapter();

    Method method = null;

    try {
      method = adapter.getClass().getMethod("getDocumentAttribute", Integer.class, String.class);
    } catch (NoSuchMethodException e) {
      Log.e(
          this.getClass().getSimpleName(),
          "Unable to find test method getDocumentAttribute on adapter "
              + adapter.getClass().getName());
    }

    if (method != null) {
      return method.invoke(adapter, position, "dc:created");
    } else {
      return null;
    }
  }
  public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
      ViewGroup.LayoutParams params = listView.getLayoutParams();
      params.height = 0;
      listView.setLayoutParams(params);
      listView.requestLayout();
      return;
    }

    int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
    int desiredWidth =
        View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
      View listItem = listAdapter.getView(i, null, listView);

      if (listItem != null) {
        listItem.setLayoutParams(
            new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT));
        listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
      }
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
  }
Beispiel #25
0
  public static void setListViewHeightBasedOnChildren(ListView listView, boolean mIsFlag) {
    // 获取ListView对应的Adapter
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
      return;
    }

    int totalHeight = 0;
    int moneHeight = 0;
    for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
      View listItem = listAdapter.getView(i, null, listView);
      listItem.measure(0, 0); // 计算子项View 的宽高
      totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
      // sysout.println("----------高度----------"+listItem.getMeasuredHeight());
      // if(i==len-1 && (mIsFlag)){ //评论特殊处理
      // moneHeight = listItem.getMeasuredHeight()/2+10;
      // }
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    totalHeight = moneHeight + totalHeight;
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    // sysout.println("-----总高度----------------"+params.height);
    // listView.getDividerHeight()获取子项间分隔符占用的高度
    // params.height最后得到整个ListView完整显示需要的高度
    listView.setLayoutParams(params);
    // sysout.println("---------ListView高度----------"+listView.getLayoutParams().height);
  }
  private int measureHeight() {
    // 获取ListView对应的Adapter
    ListAdapter adapter = mListView.getAdapter();
    if (null == adapter) {
      return 0;
    }

    int totalHeight = 0;

    for (int i = 0, len = adapter.getCount(); i < len; i++) {
      View item = adapter.getView(i, null, mListView);
      if (null == item) continue;
      // 计算子项View 的宽高
      item.measure(0, 0);
      // 统计所有子项的总高度
      totalHeight += item.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = mListView.getLayoutParams();

    if (null == params) {
      params =
          new ViewGroup.LayoutParams(
              ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    // 最后得到整个ListView完整显示需要的高度
    params.height = totalHeight + (mListView.getDividerHeight() * (adapter.getCount() - 1));

    mListView.setLayoutParams(params);

    return params.height;
  }
    @Override
    public int getItemViewType(int position) {

      final int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns;
      final int adapterViewTypeStart = mAdapter == null ? 0 : mAdapter.getViewTypeCount() - 1;
      int type = AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER;
      if (mCachePlaceHoldView) {
        // Header
        if (position < numHeadersAndPlaceholders) {
          if (position == 0) {
            if (mCacheFirstHeaderView) {
              type =
                  adapterViewTypeStart + mHeaderViewInfos.size() + mFooterViewInfos.size() + 1 + 1;
            }
          }
          if (position % mNumColumns != 0) {
            type = adapterViewTypeStart + (position / mNumColumns + 1);
          }
        }
      }

      // Adapter
      final int adjPosition = position - numHeadersAndPlaceholders;
      int adapterCount = 0;
      if (mAdapter != null) {
        adapterCount = getAdapterAndPlaceHolderCount();
        if (adjPosition >= 0 && adjPosition < adapterCount) {
          if (adjPosition < mAdapter.getCount()) {
            type = mAdapter.getItemViewType(adjPosition);
          } else {
            if (mCachePlaceHoldView) {
              type = adapterViewTypeStart + mHeaderViewInfos.size() + 1;
            }
          }
        }
      }

      if (mCachePlaceHoldView) {
        // Footer
        final int footerPosition = adjPosition - adapterCount;
        if (footerPosition >= 0
            && footerPosition < getCount()
            && (footerPosition % mNumColumns) != 0) {
          type =
              adapterViewTypeStart
                  + mHeaderViewInfos.size()
                  + 1
                  + (footerPosition / mNumColumns + 1);
        }
      }
      if (DEBUG) {
        Log.d(
            LOG_TAG,
            String.format(
                "getItemViewType: pos: %s, result: %s",
                position, type, mCachePlaceHoldView, mCacheFirstHeaderView));
      }
      return type;
    }
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
   if (DEBUG) {
     Log.d(LOG_TAG, String.format("getView: %s, reused: %s", position, convertView == null));
   }
   // Header (negative positions will throw an ArrayIndexOutOfBoundsException)
   int numHeadersAndPlaceholders = getHeadersCount() * mNumColumns;
   if (position < numHeadersAndPlaceholders) {
     View headerViewContainer = mHeaderViewInfos.get(position / mNumColumns).viewContainer;
     if (position % mNumColumns == 0) {
       return headerViewContainer;
     } else {
       if (convertView == null) {
         convertView = new View(parent.getContext());
       }
       // We need to do this because GridView uses the height of the last item
       // in a row to determine the height for the entire row.
       convertView.setVisibility(View.INVISIBLE);
       convertView.setMinimumHeight(headerViewContainer.getHeight());
       return convertView;
     }
   }
   // Adapter
   final int adjPosition = position - numHeadersAndPlaceholders;
   int adapterCount = 0;
   if (mAdapter != null) {
     adapterCount = getAdapterAndPlaceHolderCount();
     if (adjPosition < adapterCount) {
       if (adjPosition < mAdapter.getCount()) {
         return mAdapter.getView(adjPosition, convertView, parent);
       } else {
         if (convertView == null) {
           convertView = new View(parent.getContext());
         }
         convertView.setVisibility(View.INVISIBLE);
         convertView.setMinimumHeight(mRowHeight);
         return convertView;
       }
     }
   }
   // Footer
   final int footerPosition = adjPosition - adapterCount;
   if (footerPosition < getCount()) {
     View footViewContainer = mFooterViewInfos.get(footerPosition / mNumColumns).viewContainer;
     if (position % mNumColumns == 0) {
       return footViewContainer;
     } else {
       if (convertView == null) {
         convertView = new View(parent.getContext());
       }
       // We need to do this because GridView uses the height of the last item
       // in a row to determine the height for the entire row.
       convertView.setVisibility(View.INVISIBLE);
       convertView.setMinimumHeight(footViewContainer.getHeight());
       return convertView;
     }
   }
   throw new ArrayIndexOutOfBoundsException(position);
 }
 int findPreviousVisibleSectionPosition(int fromPosition) {
   ListAdapter adapter = getAdapter();
   for (int childIndex = fromPosition - 1; childIndex >= 0; childIndex--) {
     int viewType = adapter.getItemViewType(childIndex);
     if (isItemViewTypePinned(adapter, viewType)) return childIndex;
   }
   return -1;
 }
Beispiel #30
0
 public boolean areAllItemsEnabled() {
   ListAdapter listadapter = b;
   if (listadapter != null) {
     return listadapter.areAllItemsEnabled();
   } else {
     return true;
   }
 }