Android 如何让 RecyclerView 更新其布局?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25306870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:19:41  来源:igfitidea点击:

How do I make RecyclerView update its layout?

androidandroid-recyclerview

提问by Nilzor

I have a RecyclerViewwith a bunch of custom views which may change height after a while, because they contain ImageViews which load their image asynchronously. The RecyclerViewdoes not pick up on this layout change, although I call forceLayouton the ImageView, and the RecyclerViewis initialized with setHasFixedSize(false). All container-parents of the ImageViewhave set android:layout_height="wrap_content".

我有RecyclerView一堆自定义视图,它们可能会在一段时间后改变高度,因为它们包含ImageView异步加载图像的 s。在RecyclerView没有拿起这个布局的变化,虽然我叫forceLayoutImageView,并RecyclerView与初始化setHasFixedSize(false)。的所有容器父级ImageView都设置了android:layout_height="wrap_content".

How can I make the RecyclerViewupdate its layout? With good'ol ListViewthis was not a problem.

我怎样才能RecyclerView更新它的布局?有了good'ol,ListView这不是问题。

采纳答案by Nilzor

Google has finally fixed this in support library v23.2. Issue is fixed by updating this line in build.gradleafter updating your support repository with the SDK Manager:

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

compile 'com.android.support:recyclerview-v7:23.2.0'

回答by Barad

You can use the notifyDataSetChangedin the Adapterfor the RecyclerViewif all your images change at the same time or notifyItemChanged(int position)if only one of the items has changed. This will tell the RecyclerViewto rebind the particular View.

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

回答by Edwin Muigai

Call the notifyDataSetChanged()or notifyItemChanged(int position)methods of the RecyclerView.Adapterfrom the MAIN THREAD. Calling it from an AsyncTaskmay result in the main thread not updating the ImageViews. Its most convenient to have a callback method in the activity/fragment work as a listener, and have it called by the AsyncTaskin onPostExecute()

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

回答by Libin Thomas

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);

This code is worked for me.

这段代码对我有用。

回答by PaulNunezM

I had the same problem, I just set a fix width and height for the ImageView. Try that and see where you get. You can also use a lib for this, I use Square Picasso and I got it working with RecyclerView.

我遇到了同样的问题,我只是为 ImageView 设置了一个固定的宽度和高度。试试看,看看你会得到什么。你也可以为此使用一个库,我使用 Square Picasso,我让它与 RecyclerView 一起工作。