下面列出了android.widget.ListView#getChildCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void findViewClicked(MotionEvent event, float eventY, ListView list) {
mChildCount = list.getChildCount();
mListViewCoords = new int[2];
list.getLocationOnScreen(mListViewCoords);
int x = (int) event.getRawX() - mListViewCoords[0];
int y = (int) event.getRawY() - mListViewCoords[1];
View child;
for (int i = 0; i < mChildCount; i++) {
child = list.getChildAt(i);
child.getHitRect(mRect);
if (mRect.contains(x, y)) {
mDownView = child;
mDownEventY = eventY;
break;
}
}
}
private void loadImage(ListView listView) {
if (listView != null) {
try {
int count = listView.getChildCount();
for (int i = 0; i < count; i++) {
Object tag = listView.getChildAt(i).getTag();
if (tag != null) {
ViewHolder holder = (ViewHolder) tag;
Object t1 = holder.iv_1.getTag();
if (t1 != null) {
ImageDownloader.getInstance().download(holder.iv_1, (String) t1);
holder.iv_1.setTag(null);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public boolean canTargetScrollVertically(int direction) {
ListView target = this.mTarget;
int itemCount = target.getCount();
if (itemCount == 0) {
return false;
}
int childCount = target.getChildCount();
int firstPosition = target.getFirstVisiblePosition();
int lastPosition = firstPosition + childCount;
if (direction > 0) {
if (lastPosition >= itemCount && target.getChildAt(childCount - 1).getBottom() <= target.getHeight()) {
return false;
}
} else if (direction >= 0) {
return false;
} else {
if (firstPosition <= 0 && target.getChildAt(0).getTop() >= 0) {
return false;
}
}
return true;
}
private void updatePreferences(ListView accountList){
for(int i = 0; i < accountList.getChildCount(); i++) {
try {
LinearLayout rootLayout = (LinearLayout) accountList.getChildAt(i);
RelativeLayout preferenceLayout = (RelativeLayout) rootLayout.getChildAt(1);
TextView titleView = (TextView) preferenceLayout.getChildAt(0);
TextView summaryView = (TextView) preferenceLayout.getChildAt(1);
if(titleView.getText().toString().equals(getString(R.string.language))) {
summaryView.setCompoundDrawablePadding(10);
summaryView.setCompoundDrawablesRelativeWithIntrinsicBounds(LanguageUtil.getLanguageFlagDrawable(getActivity(),
LanguageUtil.getCurrentLanguageCode(getActivity())), null, null, null);
} else if(titleView.getText().toString().equals(getString(R.string.theme))) {
summaryView.setText(ThemeUtil.getThemePreview(getActivity(), ThemeUtil.getCurrentTheme(getActivity())));
summaryView.setTextSize(30);
}
} catch (Exception e){
e.printStackTrace();
}
}
}
/**
* 截图listview
**/
public static Bitmap getBitmapByListView(ListView listView) {
int h = 0;
Bitmap bitmap = null;
// 获取listView实际高度
for (int i = 0; i < listView.getChildCount(); i++) {
h += listView.getChildAt(i).getHeight();
}
Log.d(TAG, "实际高度:" + h);
Log.d(TAG, "list 高度:" + listView.getHeight());
// 创建对应大小的bitmap
bitmap = Bitmap.createBitmap(listView.getWidth(), h,
Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
listView.draw(canvas);
return bitmap;
}
@Override
public void onDoubleClick(View v) {
//双击事件
Activity context = (Activity)v.getContext();
ListView lvMicroBlog = (ListView)context.findViewById(R.id.lvMicroBlog);
if (lvMicroBlog != null) {
ListAdapter adapter = lvMicroBlog.getAdapter();
CacheAdapter<?> cacheAdapter = getCacheAdapter(adapter);
if (cacheAdapter != null) {
cacheAdapter.reclaim(ReclaimLevel.MODERATE);
}
if (lvMicroBlog.getChildCount() > 1) {
lvMicroBlog.setSelection(1);
}
}
}
@Override
public void onImageLoadComplete(String key, Bitmap bitmap, boolean error) {
UiManager uiManager = getUiManager();
ThreadsAdapter adapter = getAdapter();
ListView listView = getListView();
for (int i = 0; i < listView.getChildCount(); i++) {
View view = listView.getChildAt(i);
if (adapter.isGridMode()) {
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int j = 0; j < viewGroup.getChildCount(); j++) {
View child = viewGroup.getChildAt(j);
if (child.getVisibility() == View.VISIBLE) {
uiManager.view().displayLoadedThumbnailsForView(child, key, bitmap, error);
}
}
}
} else {
uiManager.view().displayLoadedThumbnailsForView(view, key, bitmap, error);
}
}
}
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 null;
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
/**
* Utility method to check whether a {@link ListView} can scroll up from it's current position.
* Handles platform version differences, providing backwards compatible functionality where
* needed.
*/
private static boolean canListViewScrollUp(ListView listView) {
if (SdkUtils.hasIceCreamSandwich()) {
// For ICS and above we can call canScrollVertically() to determine this
return ViewCompat.canScrollVertically(listView, -1);
} else {
// Pre-ICS we need to manually check the first visible item and the child view's top
// value
return listView.getChildCount() > 0 &&
(listView.getFirstVisiblePosition() > 0
|| listView.getChildAt(0).getTop() < listView.getPaddingTop());
}
}
private static boolean canListViewScrollUp(ListView listView) {
if (android.os.Build.VERSION.SDK_INT >= 14) {
// For ICS and above we can call canScrollVertically() to determine this
return ViewCompat.canScrollVertically(listView, -1);
} else {
// Pre-ICS we need to manually check the first visible item and the child view's top
// value
return listView.getChildCount() > 0 &&
(listView.getFirstVisiblePosition() > 0
|| listView.getChildAt(0).getTop() < listView.getPaddingTop());
}
}
private void renderView(int position) {
View view = gridViewsArray[position];
if (view != null) {
ListView listView = (ListView) view.findViewById(R.id.list_view);
for (int i = 0; i < listView.getChildCount(); i++) {
renderThumbView(listView.getChildAt(i));
}
}
}
/**
* Utility method to check whether a {@link ListView} can scroll up from it's current position.
* Handles platform version differences, providing backwards compatible functionality where
* needed.
*/
private static boolean canListViewScrollUp(ListView listView) {
if (android.os.Build.VERSION.SDK_INT >= 14) {
// For ICS and above we can call canScrollVertically() to determine this
return ViewCompat.canScrollVertically(listView, -1);
} else {
// Pre-ICS we need to manually check the first visible item and the child view's top
// value
return listView.getChildCount() > 0 &&
(listView.getFirstVisiblePosition() > 0
|| listView.getChildAt(0).getTop() < listView.getPaddingTop());
}
}
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);
}
}
@Override
public boolean canTargetScrollVertically(int direction) {
final ListView target = mTarget;
final int itemCount = target.getCount();
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;
}
/**
* 把view转化为bitmap(截图)
* 参见:http://www.cnblogs.com/lee0oo0/p/3355468.html
*/
public static Bitmap toBitmap(View view) {
int width = view.getWidth();
int height = view.getHeight();
if (view instanceof ListView) {
height = 0;
// 获取listView实际高度
ListView listView = (ListView) view;
for (int i = 0; i < listView.getChildCount(); i++) {
height += listView.getChildAt(i).getHeight();
}
} else if (view instanceof ScrollView) {
height = 0;
// 获取scrollView实际高度
ScrollView scrollView = (ScrollView) view;
for (int i = 0; i < scrollView.getChildCount(); i++) {
height += scrollView.getChildAt(i).getHeight();
}
}
view.setDrawingCacheEnabled(true);
view.clearFocus();
view.setPressed(false);
boolean willNotCache = view.willNotCacheDrawing();
view.setWillNotCacheDrawing(false);
// Reset the drawing cache background color to fully transparent for the duration of this operation
int color = view.getDrawingCacheBackgroundColor();
view.setDrawingCacheBackgroundColor(Color.WHITE);//截图去黑色背景(透明像素)
if (color != Color.WHITE) {
view.destroyDrawingCache();
}
view.buildDrawingCache();
Bitmap cacheBitmap = view.getDrawingCache();
if (cacheBitmap == null) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(cacheBitmap, 0, 0, null);
canvas.save();
canvas.restore();
if (!bitmap.isRecycled()) {
bitmap.recycle();
}
// Restore the view
view.destroyDrawingCache();
view.setWillNotCacheDrawing(willNotCache);
view.setDrawingCacheBackgroundColor(color);
return bitmap;
}
/**
* 把view转化为bitmap(截图)
* 参见:http://www.cnblogs.com/lee0oo0/p/3355468.html
*/
public static Bitmap toBitmap(View view) {
int width = view.getWidth();
int height = view.getHeight();
if (view instanceof ListView) {
height = 0;
// 获取listView实际高度
ListView listView = (ListView) view;
for (int i = 0; i < listView.getChildCount(); i++) {
height += listView.getChildAt(i).getHeight();
}
} else if (view instanceof ScrollView) {
height = 0;
// 获取scrollView实际高度
ScrollView scrollView = (ScrollView) view;
for (int i = 0; i < scrollView.getChildCount(); i++) {
height += scrollView.getChildAt(i).getHeight();
}
}
view.setDrawingCacheEnabled(true);
view.clearFocus();
view.setPressed(false);
boolean willNotCache = view.willNotCacheDrawing();
view.setWillNotCacheDrawing(false);
// Reset the drawing cache background color to fully transparent for the duration of this operation
int color = view.getDrawingCacheBackgroundColor();
view.setDrawingCacheBackgroundColor(Color.WHITE);//截图去黑色背景(透明像素)
if (color != Color.WHITE) {
view.destroyDrawingCache();
}
view.buildDrawingCache();
Bitmap cacheBitmap = view.getDrawingCache();
if (cacheBitmap == null) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(cacheBitmap, 0, 0, null);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
if (!bitmap.isRecycled()) {
LogUtils.verbose("recycle bitmap: " + bitmap.toString());
bitmap.recycle();
}
// Restore the view
view.destroyDrawingCache();
view.setWillNotCacheDrawing(willNotCache);
view.setDrawingCacheBackgroundColor(color);
return bitmap;
}
/**
* 把view转化为bitmap(截图)
* 参见:http://www.cnblogs.com/lee0oo0/p/3355468.html
*/
public static Bitmap toBitmap(View view) {
int width = view.getWidth();
int height = view.getHeight();
if (view instanceof ListView) {
height = 0;
// 获取listView实际高度
ListView listView = (ListView) view;
for (int i = 0; i < listView.getChildCount(); i++) {
height += listView.getChildAt(i).getHeight();
}
} else if (view instanceof ScrollView) {
height = 0;
// 获取scrollView实际高度
ScrollView scrollView = (ScrollView) view;
for (int i = 0; i < scrollView.getChildCount(); i++) {
height += scrollView.getChildAt(i).getHeight();
}
}
view.setDrawingCacheEnabled(true);
view.clearFocus();
view.setPressed(false);
boolean willNotCache = view.willNotCacheDrawing();
view.setWillNotCacheDrawing(false);
// Reset the drawing cache background color to fully transparent for the duration of this operation
int color = view.getDrawingCacheBackgroundColor();
view.setDrawingCacheBackgroundColor(Color.WHITE);//截图去黑色背景(透明像素)
if (color != Color.WHITE) {
view.destroyDrawingCache();
}
view.buildDrawingCache();
Bitmap cacheBitmap = view.getDrawingCache();
if (cacheBitmap == null) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(cacheBitmap, 0, 0, null);
canvas.save();
canvas.restore();
if (!bitmap.isRecycled()) {
bitmap.recycle();
}
// Restore the view
view.destroyDrawingCache();
view.setWillNotCacheDrawing(willNotCache);
view.setDrawingCacheBackgroundColor(color);
return bitmap;
}
/**
* 把view转化为bitmap(截图)
* 参见:http://www.cnblogs.com/lee0oo0/p/3355468.html
*/
public static Bitmap toBitmap(View view) {
int width = view.getWidth();
int height = view.getHeight();
if (view instanceof ListView) {
height = 0;
// 获取listView实际高度
ListView listView = (ListView) view;
for (int i = 0; i < listView.getChildCount(); i++) {
height += listView.getChildAt(i).getHeight();
}
} else if (view instanceof ScrollView) {
height = 0;
// 获取scrollView实际高度
ScrollView scrollView = (ScrollView) view;
for (int i = 0; i < scrollView.getChildCount(); i++) {
height += scrollView.getChildAt(i).getHeight();
}
}
view.setDrawingCacheEnabled(true);
view.clearFocus();
view.setPressed(false);
boolean willNotCache = view.willNotCacheDrawing();
view.setWillNotCacheDrawing(false);
// Reset the drawing cache background color to fully transparent for the duration of this operation
int color = view.getDrawingCacheBackgroundColor();
view.setDrawingCacheBackgroundColor(Color.WHITE);//截图去黑色背景(透明像素)
if (color != Color.WHITE) {
view.destroyDrawingCache();
}
view.buildDrawingCache();
Bitmap cacheBitmap = view.getDrawingCache();
if (cacheBitmap == null) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(cacheBitmap, 0, 0, null);
canvas.save();
canvas.restore();
if (!bitmap.isRecycled()) {
LogUtils.verbose("recycle bitmap: " + bitmap.toString());
bitmap.recycle();
}
// Restore the view
view.destroyDrawingCache();
view.setWillNotCacheDrawing(willNotCache);
view.setDrawingCacheBackgroundColor(color);
return bitmap;
}
@Override
protected void onNewIntent(Intent newIntent) {
super.onNewIntent(newIntent);
if (Logger.isDebug()) {
Log.v(TAG, "onNewIntent……" + ", Intent : " + newIntent);
}
LocalAccount account = (LocalAccount)newIntent.getSerializableExtra("ACCOUNT");
if (account == null) {
updateContentView(null);
return;
}
int contentType = newIntent.getIntExtra("CONTENT_TYPE", Skeleton.TYPE_MY_HOME);
sheJiaoMao.setCurrentAccount(account); // 设置当前帐号
skeleton.setCurrentAccount(account, true);
skeleton.setContentType(contentType);
// move to head
ListView lvMicroBlog = (ListView) this.findViewById(R.id.lvMicroBlog);
if (lvMicroBlog != null) {
ListAdapter adapter = lvMicroBlog.getAdapter();
CacheAdapter<?> cacheAdapter = AdapterUtil.getCacheAdapter(adapter);
//有可能处于分组中
if (contentType == Skeleton.TYPE_MY_HOME
&& cacheAdapter instanceof GroupStatusesListAdapter) {
Cache cache = CacheManager.getInstance().getCache(account);
AdapterCollectionCache adapterCache = (AdapterCollectionCache)cache;
if (adapterCache != null) {
cacheAdapter = adapterCache.getMyHomeListAdapter();
lvMicroBlog.setAdapter(cacheAdapter);
TextView tvTitle = (TextView)this.findViewById(R.id.tvTitle);
String title = "";
if (account.getUser() != null) {
title += account.getUser().getScreenName() + "@";
}
title += account.getServiceProvider().getSpName();
tvTitle.setText(title);
}
}
if (cacheAdapter != null && cacheAdapter.refresh()) {
cacheAdapter.reclaim(ReclaimLevel.MODERATE);
}
if (lvMicroBlog.getChildCount() > 1) {
lvMicroBlog.setSelection(1);
}
}
}
public void notifyUnbindListView(ListView listView) {
for (int i = 0; i < listView.getChildCount(); i++) {
notifyUnbindView(listView.getChildAt(i));
}
}