以下是代码:
MainActivity:
recyclerView是一个RecyclerView
recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { UpdaterController controller = mUpdaterService == null ? null : mUpdaterService.getUpdaterController(); if(controller != null && mAdapter != null) { List<UpdateInfo> updates = controller.getUpdates(); for (int i = 0; i < recyclerView.getChildCount(); i++) { UpdateInfo update = updates.get(i); View view = recyclerView.getLayoutManager().findViewByPosition(i); LinearLayout mBtnsLayout = (LinearLayout) view.findViewById(R.id.update_btns); ImageButton mAction = (ImageButton) view.findViewById(R.id.update_action); ImageButton mShowChangelog = (ImageButton) view.findViewById(R.id.show_changelog); CardView mCard = (CardView) view.findViewById(R.id.update_card); Space mBtnsSpace = (Space) view.findViewById(R.id.update_btns_space); OnlineImageView mImageView = view.findViewById(R.id.update_imageView); int btnsHeight = mBtnsLayout.getMeasuredHeight(); int cardHeight = mCard.getMeasuredHeight(); mBtnsLayout.setLayoutParams((LinearLayout.LayoutParams)Utils.getLayoutParams(btnsHeight, btnsHeight, mBtnsLayout)); mImageView.setImageURL(update.getImageUrl()); int w = ((mImageView.getImageHeight() * mImageView.getImageWidth()) == 0 ? 0 : (cardHeight * mImageView.getImageWidth() / mImageView.getImageHeight())); mImageView.setLayoutParams((RelativeLayout.LayoutParams)Utils.getLayoutParams(w, cardHeight, mImageView)); mAction.setLayoutParams((LinearLayout.LayoutParams)Utils.getLayoutParams((int) (btnsHeight * 0.6), (int) (btnsHeight * 0.6), mAction)); mShowChangelog.setLayoutParams((LinearLayout.LayoutParams)Utils.getLayoutParams((int) (btnsHeight * 0.3), (int) (btnsHeight * 0.3), mShowChangelog)); mBtnsSpace.setLayoutParams((LinearLayout.LayoutParams)Utils.getLayoutParams(btnsHeight - mAction.getMeasuredHeight() - mShowChangelog.getMeasuredHeight(), btnsHeight, mBtnsSpace)); mAdapter.notifyItemChanged(i); } recyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this); } } });
以下是Utils.getLayoutParams函数:
public static ViewGroup.LayoutParams getLayoutParams(int tw, int th, View view) { ViewGroup.LayoutParams params = view.getLayoutParams(); params.width = tw; params.height = th; return params; }
Debug发现,在上面的第一个代码块执行完毕后,各个控件的LayoutParams发生了改变,其中的宽高与传入值相符,而在控件中的mMessuredWidth和mMessuredHeight的值却均没有变化,也就是实际显示的宽高并未改变。
这是我的仓库:https://github.com/SF2005/Updater
希望能得到个解决方案,或者告诉我,你也不觉得有问题,谢谢各位了!!!!
这几天真是烦死我了??
慕斯4360584
慕斯4360584
慕慕5436299