37

我有RecyclerView一堆自定义视图,它们可能会在一段时间后改变高度,因为它们包含ImageView异步加载图像的 s。尽管我调用了,并且初始化了RecyclerView,但 并没有注意到这种布局更改。的所有容器父级都设置了.forceLayoutImageViewRecyclerViewsetHasFixedSize(false)ImageViewandroid:layout_height="wrap_content"

如何使RecyclerView更新其布局?有了good'ol ListView,这不是问题。

4

5 回答 5

23

如果您的所有图像同时更改或只有一项更改,您可以使用notifyDataSetChanged中的Adapter。这将告诉重新绑定特定的.RecyclerViewnotifyItemChanged(int position)RecyclerViewView

于 2014-09-05T19:45:36.650 回答
11

Google 终于在支持库 v23.2中解决了这个问题。build.gradle在使用 SDK 管理器更新您的支持存储库后,通过更新此行来修复问题:

compile 'com.android.support:recyclerview-v7:23.2.0'
于 2016-02-25T14:12:38.260 回答
5

MAIN THREAD调用notifyDataSetChanged()ornotifyItemChanged(int position)方法。从 an 调用它可能会导致主线程不更新s。在活动/片段中使用回调方法作为侦听器最方便,并由in调用它RecyclerView.AdapterAsyncTaskImageViewAsyncTaskonPostExecute()

于 2015-04-29T17:12:16.453 回答
2
int wantedPosition = 25; // Whatever position you're looking for
int firstPosition = linearLayoutManager.findFirstVisibleItemPosition(); // This is the same as child #0
int wantedChild = wantedPosition - firstPosition;

if (wantedChild < 0 || wantedChild >= linearLayoutManager.getChildCount()) {
    Log.w(TAG, "Unable to get view for desired position, because it's not being displayed on screen.");
    return;
}

View wantedView = linearLayoutManager.getChildAt(wantedChild);
mlayoutOver =(LinearLayout)wantedView.findViewById(R.id.layout_over);
mlayoutPopup = (LinearLayout)wantedView.findViewById(R.id.layout_popup);

mlayoutOver.setVisibility(View.INVISIBLE);
mlayoutPopup.setVisibility(View.VISIBLE);

这段代码对我有用。

于 2015-07-15T09:47:57.840 回答
0

我有同样的问题,我只是为 ImageView 设置了一个固定的宽度和高度。试试看,看看你在哪里。你也可以为此使用一个库,我使用 Square Picasso,我让它与 RecyclerView 一起使用。

于 2014-11-03T23:01:00.260 回答