How do I make RecyclerView update its layout?

前端 未结 5 766
北恋
北恋 2020-12-09 14:54

I have a RecyclerView with a bunch of custom views which may change height after a while, because they contain ImageViews which load their image as

相关标签:
5条回答
  • 2020-12-09 15:21

    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:

    compile 'com.android.support:recyclerview-v7:23.2.0'
    
    0 讨论(0)
  • 2020-12-09 15:23
    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-09 15:39

    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.

    0 讨论(0)
  • 2020-12-09 15:42

    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 ImageViews. 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()

    0 讨论(0)
提交回复
热议问题