下面列出了android.widget.ListView#getChildAt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
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;
}
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);
}
/**
* 判断 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;
}
/**
* 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;
}
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;
}
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);
}
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;
}
/**
* 判断 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);
}
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);
}
}
}
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);
}
}
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;
}
}
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);
}
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;
}
});
}