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
How do I make RecyclerView update its layout?
提问by Nilzor
I have a RecyclerView
with a bunch of custom views which may change height after a while, because they contain ImageView
s which load their image asynchronously. The RecyclerView
does not pick up on this layout change, although I call forceLayout
on the ImageView
, and the RecyclerView
is initialized with setHasFixedSize(false)
. All container-parents of the ImageView
have set android:layout_height="wrap_content"
.
我有RecyclerView
一堆自定义视图,它们可能会在一段时间后改变高度,因为它们包含ImageView
异步加载图像的 s。在RecyclerView
没有拿起这个布局的变化,虽然我叫forceLayout
上ImageView
,并RecyclerView
与初始化setHasFixedSize(false)
。的所有容器父级ImageView
都设置了android:layout_height="wrap_content"
.
How can I make the RecyclerView
update its layout? With good'ol ListView
this 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.gradle
after 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 notifyDataSetChanged
in the Adapter
for the RecyclerView
if all your images change at the same time or notifyItemChanged(int position)
if only one of the items has changed. This will tell the RecyclerView
to rebind the particular View
.
如果您的所有图像同时更改或仅其中一项更改,您可以使用notifyDataSetChanged
in 。这将告诉重新绑定特定的.Adapter
RecyclerView
notifyItemChanged(int position)
RecyclerView
View
回答by Edwin Muigai
Call the notifyDataSetChanged()
or notifyItemChanged(int position)
methods of the RecyclerView.Adapter
from the MAIN THREAD. Calling it from an AsyncTask
may result in the main thread not updating the ImageView
s. Its most convenient to have a callback method in the activity/fragment work as a listener, and have it called by the AsyncTask
in onPostExecute()
从MAIN THREAD调用 的notifyDataSetChanged()
或notifyItemChanged(int position)
方法。从 an 调用它可能会导致主线程不更新s。在活动/片段中使用回调方法作为侦听器最方便,并由in调用它RecyclerView.Adapter
AsyncTask
ImageView
AsyncTask
onPostExecute()
回答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 一起工作。