android.widget.ListView#getChildAt ( )源码实例Demo

下面列出了android.widget.ListView#getChildAt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: guideshow   文件: ListViewAutoScrollHelper.java
@Override
public void scrollTargetBy(int deltaX, int deltaY) {
    final ListView target = mTarget;
    final int firstPosition = target.getFirstVisiblePosition();
    if (firstPosition == ListView.INVALID_POSITION) {
        return;
    }

    final View firstView = target.getChildAt(0);
    if (firstView == null) {
        return;
    }

    final int newTop = firstView.getTop() - deltaY;
    target.setSelectionFromTop(firstPosition, newTop);
}
 
源代码2 项目: prevent   文件: PreventFragment.java
public void updateTimeIfNeeded(String packageName) {
    if (scrolling || mAdapter == null) {
        return;
    }
    ListView l = getListView();
    int size = mAdapter.getCount();
    for (int i = 0; i < size; ++i) {
        View view = l.getChildAt(i);
        if (view == null || view.getTag() == null || view.getVisibility() != View.VISIBLE) {
            continue;
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        if (PackageUtils.equals(packageName, holder.packageName)) {
            holder.updatePreventView(mActivity);
            holder.running = mActivity.getRunningProcesses().get(packageName);
            holder.summaryView.setText(StatusUtils.formatRunning(mActivity, holder.running));
        } else if (holder.running != null) {
            holder.summaryView.setText(StatusUtils.formatRunning(mActivity, holder.running));
        }
    }
}
 
@Override
public void scrollTargetBy(int deltaX, int deltaY) {
    final ListView target = mTarget;
    final int firstPosition = target.getFirstVisiblePosition();
    if (firstPosition == ListView.INVALID_POSITION) {
        return;
    }

    final View firstView = target.getChildAt(0);
    if (firstView == null) {
        return;
    }

    final int newTop = firstView.getTop() - deltaY;
    target.setSelectionFromTop(firstPosition, newTop);
}
 
/**
 * @return
 */
private boolean isHeaderViewAtTop() {
    if(mRefreshableView instanceof ListView){
        if(mRefreshableView.getFirstVisiblePosition() > 1) {
            return false;
        }
        
        ListView listView = (ListView)mRefreshableView;
        if(listView != null && listView.getChildCount() > 0){
            View view = listView.getChildAt(0);
            if(view != null){
                return view.getTop() == 0;
            }
        }
    }
    return true;
}
 
源代码5 项目: Dashchan   文件: ListPosition.java
public static ListPosition obtain(ListView listView) {
	int position = listView.getFirstVisiblePosition();
	int y = 0;
	Rect rect = new Rect();
	int paddingTop = listView.getPaddingTop(), paddingLeft = listView.getPaddingLeft();
	for (int i = 0, count = listView.getChildCount(); i < count; i++) {
		View view = listView.getChildAt(i);
		view.getHitRect(rect);
		if (rect.contains(paddingLeft, paddingTop)) {
			position += i;
			y = rect.top - paddingTop;
			break;
		}
	}
	return new ListPosition(position, y);
}
 
源代码6 项目: Common   文件: ViewHelper.java
/**
 * 判断 ListView 是否已经滚动到底部
 *
 * @param listView 需要被判断的 ListView
 * @return
 */
public static boolean isListViewAlreadyAtBottom(ListView listView) {
    if (listView.getAdapter() == null || listView.getHeight() == 0) {
        return false;
    }

    if (listView.getLastVisiblePosition() == listView.getAdapter().getCount() - 1) {
        View lastItemView = listView.getChildAt(listView.getChildCount() - 1);
        if (lastItemView != null && lastItemView.getBottom() == listView.getHeight()) {
            return true;
        }
    }
    return false;
}
 
源代码7 项目: ALLGO   文件: HomeACTIVITY.java
/**
 * drawer 的初始化方法
 */
private void creatdrawer() {
	// TODO 生成抽屉方法开始
       mTitle = mDrawerTitle = getTitle();
       mHomeTitles = getResources().getStringArray(R.array.home_array);
       mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
       mDrawerList = (ListView) findViewById(R.id.left_drawer);
       // set a custom shadow that overlays the main content when the drawer opens
       mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
       // set up the drawer's list view with items and click listener
       mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item,mHomeTitles));
       mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
       // enable ActionBar app icon to behave as action to toggle nav drawer
       mActionBar.setDisplayHomeAsUpEnabled(true);
       mActionBar.setHomeButtonEnabled(true);

       // ActionBarDrawerToggle ties together the the proper interactions
       // between the sliding drawer and the action bar app icon
       mDrawerToggle = new ActionBarDrawerToggle(
               this,                  /* host Activity */
               mDrawerLayout,         /* DrawerLayout object */
               R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
               R.string.drawer_open,  /* "open drawer" description for accessibility */
               R.string.drawer_close  /* "close drawer" description for accessibility */
               ) {
           public void onDrawerClosed(View view) {
           	mActionBar.setTitle(mTitle);
               invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 此时关闭抽屉
           }

           public void onDrawerOpened(View drawerView) {
               mActionBar.setTitle(mDrawerTitle);
               invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()  此时打开抽屉
               View v=mDrawerList.getChildAt(position);
               v.setBackgroundColor(Color.GRAY);
           }
       };
       mDrawerLayout.setDrawerListener(mDrawerToggle);
   
}
 
@Test
public void testGetOtpWithOneHotpAccountUsingListRowClick() {
  accountDb.add(HOTP_ACCOUNT_NAME, "7777777777777777", OtpType.HOTP, null, null, null);

  activityTestRule.launchActivity(null);

  ListView userList = activityTestRule.getActivity().findViewById(R.id.user_list);
  assertThat(userList.getChildCount()).isEqualTo(1);
  View listEntry = userList.getChildAt(0);
  String user = ((TextView) listEntry.findViewById(R.id.current_user)).getText().toString();
  String pin =
      getOriginalPincode(
          ((TextView) listEntry.findViewById(R.id.pin_value)).getText().toString());
  assertThat(user).isEqualTo(HOTP_ACCOUNT_NAME);
  // starts empty
  assertThat(pin).isEqualTo(activityTestRule.getActivity().getString(R.string.empty_pin));
  assertThat(listEntry.findViewById(R.id.countdown_icon).isShown()).isFalse();

  // Get next OTP value by clicking the list item/row.
  assertThat(TestUtilities.clickListViewItem(userList, 0)).isTrue();
  listEntry = userList.getChildAt(0);
  onView(is((View) listEntry.findViewById(R.id.pin_value)))
      .check(
          matches(
              withText(
                  Utilities.getStyledPincode(
                      FIRST_HOTP_EXPECTED_PIN)))); // Verify the expected pin is present
  assertThat(accountDb.getCounter(new AccountIndex(HOTP_ACCOUNT_NAME, null)))
      .isEqualTo(Integer.valueOf(1));
}
 
@Override
public boolean canTargetScrollVertically(int direction) {
    final ListView target = mTarget;
    final int itemCount = target.getCount();
    if (itemCount == 0) {
        return false;
    }

    final int childCount = target.getChildCount();
    final int firstPosition = target.getFirstVisiblePosition();
    final int lastPosition = firstPosition + childCount;

    if (direction > 0) {
        // Are we already showing the entire last item?
        if (lastPosition >= itemCount) {
            final View lastView = target.getChildAt(childCount - 1);
            if (lastView.getBottom() <= target.getHeight()) {
                return false;
            }
        }
    } else if (direction < 0) {
        // Are we already showing the entire first item?
        if (firstPosition <= 0) {
            final View firstView = target.getChildAt(0);
            if (firstView.getTop() >= 0) {
                return false;
            }
        }
    } else {
        // The behavior for direction 0 is undefined and we can return
        // whatever we want.
        return false;
    }

    return true;
}
 
源代码10 项目: edslite   文件: DrawerSubMenuBase.java
public View findView(ListView list)
{
    for(int i = 0, n = list.getChildCount();i < n;i++)
    {
        View v = list.getChildAt(i);
        if (v.getTag() == this)
            return v;
    }
    /*int start = list.getFirstVisiblePosition();
    for (int i = start, j = list.getLastVisiblePosition(); i <= j; i++)
        if (this == list.getItemAtPosition(i))
            return list.getChildAt(i - start);*/
    return null;
}
 
源代码11 项目: Pix-Art-Messenger   文件: ListViewUtils.java
public static void setSelection(final ListView listView, int pos, boolean jumpToBottom) {
    if (jumpToBottom) {
        final View lastChild = listView.getChildAt(listView.getChildCount() - 1);
        if (lastChild != null) {
            listView.setSelectionFromTop(pos, -lastChild.getHeight());
            return;
        }
    }
    listView.setSelection(pos);
}
 
源代码12 项目: YiBo   文件: MentionsListAdapter.java
public boolean refresh() {
	if (listNewBlogs == null || listNewBlogs.size() == 0) {
		return false;
	}

	addCacheToFirst(listNewBlogs);
	int offset = listNewBlogs.size();
	listNewBlogs.clear();
	newCount = 0;

	ListView lvMicroBlog = (ListView)((Activity)context).findViewById(R.id.lvMicroBlog);
	if (lvMicroBlog == null) {
		return true;
	}
	Adapter adapter = lvMicroBlog.getAdapter();
	if (adapter instanceof HeaderViewListAdapter) {
		adapter = ((HeaderViewListAdapter)adapter).getWrappedAdapter();
	}
	if (lvMicroBlog != null &&
		adapter == this
	) {
		int position = lvMicroBlog.getFirstVisiblePosition();
		View view = lvMicroBlog.getChildAt(0);
		int y = 0;
		if (view != null && position >= 1) {
		    y = view.getTop();
		    position += offset;
		    lvMicroBlog.setSelectionFromTop(position, y);
		}
	}

	return true;
}
 
源代码13 项目: DMusic   文件: ViewHelper.java
/**
 * 判断 ListView 是否已经滚动到底部
 *
 * @param listView 需要被判断的 ListView
 * @return
 */
public static boolean isListViewAlreadyAtBottom(ListView listView) {
    if (listView.getAdapter() == null || listView.getHeight() == 0) {
        return false;
    }

    if (listView.getLastVisiblePosition() == listView.getAdapter().getCount() - 1) {
        View lastItemView = listView.getChildAt(listView.getChildCount() - 1);
        if (lastItemView != null && lastItemView.getBottom() == listView.getHeight()) {
            return true;
        }
    }
    return false;
}
 
@Test
public void testGetOtpWithOneHotpAccountUsingGetNextCodeButtonClick() {
  accountDb.add(HOTP_ACCOUNT_NAME, "7777777777777777", OtpType.HOTP, null, null, null);

  activityTestRule.launchActivity(null);

  ListView userList = activityTestRule.getActivity().findViewById(R.id.user_list);
  assertThat(userList.getChildCount()).isEqualTo(1);
  View listEntry = userList.getChildAt(0);
  String user = ((TextView) listEntry.findViewById(R.id.current_user)).getText().toString();
  String pin =
      getOriginalPincode(
          ((TextView) listEntry.findViewById(R.id.pin_value)).getText().toString());
  assertThat(user).isEqualTo(HOTP_ACCOUNT_NAME);
  // starts empty
  assertThat(pin).isEqualTo(activityTestRule.getActivity().getString(R.string.empty_pin));
  assertThat(listEntry.findViewById(R.id.countdown_icon).isShown()).isFalse();

  // Get next OTP value by clicking the "Get next code" button of this list item.
  View buttonView = listEntry.findViewById(R.id.next_otp);
  assertThat(buttonView.isShown()).isTrue();
  TestUtilities.clickView(InstrumentationRegistry.getInstrumentation(), buttonView);
  pin =
      getOriginalPincode(
          ((TextView) listEntry.findViewById(R.id.pin_value)).getText().toString());
  assertThat(pin).isEqualTo(FIRST_HOTP_EXPECTED_PIN);
  assertThat(accountDb.getCounter(new AccountIndex(HOTP_ACCOUNT_NAME, null)))
      .isEqualTo(Integer.valueOf(1));

  // Check the styled pin code
  String styledPin = ((TextView) listEntry.findViewById(R.id.pin_value)).getText().toString();
  assertThat(Utilities.getStyledPincode(FIRST_HOTP_EXPECTED_PIN)).isEqualTo(styledPin);
}
 
源代码15 项目: Passbook   文件: MainListAdapter.java
void markAll(ListView listView) {
    int firstVisible = listView.getFirstVisiblePosition();
    int lastVisible = listView.getLastVisiblePosition();
    int pos;
    for(pos = 0; pos < firstVisible; ++pos) {
        mChecked.set(pos, true);
    }
    for(pos = lastVisible + 1; pos < mChecked.size(); ++pos) {
        mChecked.set(pos, true);
    }
    mCheckCount += (mChecked.size() - lastVisible - 1);
    for(pos = firstVisible; pos <= lastVisible; ++pos) {
        View v = listView.getChildAt(pos);
        if(v!=null) {
            ViewHolder holder = (ViewHolder) v.getTag();
            Animation anim1 = AnimationUtils.loadAnimation(mContext, R.anim.shrink_to_middle);
            Animation anim2 = AnimationUtils.loadAnimation(mContext, R.anim.expand_from_middle);
            holder.mIconView.clearAnimation();
            holder.mIconView.setAnimation(anim1);
            holder.mIconView.startAnimation(anim1);

            AnimationListener listener = getAnimListener(v, holder.mIconView, pos, anim1, anim2);
            anim1.setAnimationListener(listener);
            anim2.setAnimationListener(listener);
        }
    }
}
 
源代码16 项目: iqra-android   文件: SearchResultsActivity.java
public View getViewByPosition(int pos, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (pos < firstListItemPosition || pos > lastListItemPosition) {
        return listView.getAdapter().getView(pos, null, listView);
    } else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}
 
源代码17 项目: NIM_Android_UIKit   文件: ListViewUtil.java
public static Object getViewHolderByIndex(ListView listView, int index) {
    int firstVisibleFeedPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount();
    int lastVisibleFeedPosition = listView.getLastVisiblePosition() - listView.getHeaderViewsCount();

    //只有获取可见区域的
    if (index >= firstVisibleFeedPosition && index <= lastVisibleFeedPosition) {
        View view = listView.getChildAt(index - firstVisibleFeedPosition);
        Object tag = view.getTag();
        return tag;
    } else {
        return null;
    }
}
 
源代码18 项目: Conversations   文件: ListViewUtils.java
public static void setSelection(final ListView listView, int pos, boolean jumpToBottom) {
	if (jumpToBottom) {
		final View lastChild = listView.getChildAt(listView.getChildCount() - 1);
		if (lastChild != null) {
			listView.setSelectionFromTop(pos, -lastChild.getHeight());
			return;
		}
	}
	listView.setSelection(pos);
}
 
源代码19 项目: iBeebo   文件: Utility.java
public static View getListViewItemViewFromPosition(ListView listView, int position) {
    return listView.getChildAt(position - listView.getFirstVisiblePosition());
}
 
@Test
@FixWhenMinSdkVersion(11)
@SuppressWarnings("deprecation")
public void testContextMenuCopyToClipboard() {
  // use HOTP to avoid any timing issues when "current" pin is compared with clip board text.
  accountDb.add(HOTP_ACCOUNT_NAME, "7777777777777777", OtpType.HOTP, null, null, null);

  activityTestRule.launchActivity(null);

  ListView userList = activityTestRule.getActivity().findViewById(R.id.user_list);
  // find and click next otp button.
  View buttonView = activityTestRule.getActivity().findViewById(R.id.next_otp);
  TestUtilities.clickView(InstrumentationRegistry.getInstrumentation(), buttonView);
  // get the pin being displayed
  View listEntry0 = userList.getChildAt(0);
  String pin =
      getOriginalPincode(
          ((TextView) listEntry0.findViewById(R.id.pin_value)).getText().toString());
  openListViewContextualActionBar(activityTestRule.getActivity(), userList, 0);

  InstrumentationRegistry.getInstrumentation().waitForIdleSync();

  TestUtilities.runOnMainSyncWithTimeout(
      new Callable<Void>() {
        @Override
        public Void call() {
          // Check clipboard value.
          String clipboardContent = "";
          Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
          ClipboardManager clipboard =
              (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
          if (clipboard.hasPrimaryClip()
              && clipboard.getPrimaryClipDescription()
                  .hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
            clipboardContent = clipboard.getPrimaryClip().getItemAt(0).getText().toString();
          }
          assertThat(clipboardContent).isEqualTo(pin);
          return null;
        }
      });
}