ScrollView and Recycleview conflict resolution

Source: Internet
Author: User

In our development process often encountered after the addition of ScrollView Recycleview control does not show this is their conflict, so Baidu has found a solution, the way is as follows. He's doing it in onmeasure, so you can customize it. Gridlayoutmanager, or Linearlayoutmanager.

Look at the following tool classes

public class Fullygridlayoutmanager extends Gridlayoutmanager {public Fullygridlayoutmanager-context, int spa
    ncount) {Super (context, spancount); Public Fullygridlayoutmanager (context context, int spancount, int orientation, Boolean reverselayout) {sup
    ER (context, spancount, orientation, reverselayout);

    Private int[] mmeasureddimension = new int[2]; @Override public void Onmeasure (Recyclerview.recycler Recycler, recyclerview.state State, int widthspec, int heightspe
        c) {final int widthmode = View.MeasureSpec.getMode (Widthspec);
        Final int heightmode = View.MeasureSpec.getMode (Heightspec);
        Final int widthsize = View.MeasureSpec.getSize (Widthspec);

        Final int heightsize = View.MeasureSpec.getSize (Heightspec);
        int width = 0;
        int height = 0;
        int count = GetItemCount ();
        int span = Getspancount (); for (int i = 0; i < count; i++) {Measurescrapchild(Recycler, I, View.MeasureSpec.makeMeasureSpec (i, View.MeasureSpec.UNSPECIFIED), V Iew.

            Measurespec.makemeasurespec (i, View.MeasureSpec.UNSPECIFIED), mmeasureddimension); if (getorientation () = = Horizontal) {if (i% span = 0) {width = width + mmeasureddi
                MENSION[0];
                } if (i = = 0) {height = mmeasureddimension[1];
                } else {if (i% span = 0) {height = height + mmeasureddimension[1];
                } if (i = = 0) {width = mmeasureddimension[0];  } switch (widthmode) {Case View.MeasureSpec.EXACTLY:width =
            Widthsize;
       Case View.MeasureSpec.AT_MOST:case View.MeasureSpec.UNSPECIFIED:} switch (Heightmode) {     Case View.MeasureSpec.EXACTLY:height = heightsize; Case View.MeasureSpec.AT_MOST:case View.MeasureSpec.UNSPECIFIED:} setmeasureddimension (Widt
    h, height);
                                   } private void Measurescrapchild (Recyclerview.recycler Recycler, int position, int widthspec,
                int Heightspec, int[] measureddimension {if (Position < GetItemCount ()) {try { View view = recycler.getviewforposition (0);//fix dynamically add times indexoutofboundsexception if (view!= nu
                    ll) {Recyclerview.layoutparams p = (recyclerview.layoutparams) view.getlayoutparams (); int childwidthspec = Viewgroup.getchildmeasurespec (Widthspec, Getpaddingleft () + Getpad
                    Dingright (), p.width); int childheightspec = Viewgroup.getchildmeasurespec (Heightspec, getpaddingtop () + Getpaddingbo TtoM (), p.height);
                    View.measure (Childwidthspec, Childheightspec);
                    Measureddimension[0] = view.getmeasuredwidth () + P.leftmargin + p.rightmargin;
                    MEASUREDDIMENSION[1] = view.getmeasuredheight () + P.bottommargin + p.topmargin;
                Recycler.recycleview (view);
            } catch (Exception e) {e.printstacktrace ();
 }
        }
    }

}
This is the solution to their conflict within the activity only need to add it

Fullygridlayoutmanager Fullygridlayoutmanager = new Fullygridlayoutmanager (this, 2);
Recycle_pubu.setnestedscrollingenabled (false);
Recycle_pubu.setlayoutmanager (Fullygridlayoutmanager);
Menjoylists = News.getenjoylist ();
Masonryadapter adapter = new Masonryadapter (this, menjoylists);
Recycle_pubu.setadapter (adapter);
Set the interval between item
spacesitemdecoration decoration = new Spacesitemdecoration ();
Recycle_pubu.additemdecoration (decoration);
This is the perfect solution to the conflict. The amount is very simple. Sometimes control conflicts are unavoidable Linearlayoutmanager

Conflict Empathy resolution

public class Fullylinearlayoutmanager extends Linearlayoutmanager {private static final String TAG = Fullylinearlayo

    UtManager.class.getSimpleName ();
    Public Fullylinearlayoutmanager {Super (context); Fullylinearlayoutmanager (context, int orientation, Boolean reverselayout) {super (context, O
    Rientation, reverselayout);

    Private int[] mmeasureddimension = new int[2]; @Override public void Onmeasure (Recyclerview.recycler Recycler, recyclerview.state state, in
        T widthspec, int heightspec) {final int widthmode = View.MeasureSpec.getMode (Widthspec);
        Final int heightmode = View.MeasureSpec.getMode (Heightspec);
        Final int widthsize = View.MeasureSpec.getSize (Widthspec);

        Final int heightsize = View.MeasureSpec.getSize (Heightspec); LOG.I (TAG, "onmeasure called.")
         \nwidthmode "+ Widthmode +" \nheightmode "+ heightspec       + "\nwidthsize" + widthsize + "\nheightsize" + heightsize + "\ngetitemcount" ()

        "+ GetItemCount ());
        int width = 0;
        int height = 0; for (int i = 0; i < GetItemCount (); i++) {Measurescrapchild (recycler, I, view.measures Pec.makemeasurespec (i, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec (i, view.measure

            spec.unspecified), mmeasureddimension);
                if (getorientation () = = Horizontal) {width = width + mmeasureddimension[0];
                if (i = = 0) {height = mmeasureddimension[1];
                } else {height = height + mmeasureddimension[1];
                if (i = = 0) {width = mmeasureddimension[0]; } switch (widthmode) {case View.MeasureSpec.EXACTLY:wiDTH = Widthsize;
            Case View.MeasureSpec.AT_MOST:case View.MeasureSpec.UNSPECIFIED:} switch (Heightmode) {
            Case View.MeasureSpec.EXACTLY:height = heightsize; Case View.MeasureSpec.AT_MOST:case View.MeasureSpec.UNSPECIFIED:} setmeasureddimension (Widt
    h, height);
                                   } private void Measurescrapchild (Recyclerview.recycler Recycler, int position, int widthspec, int Heightspec, int[] measureddimension) {try {view view = recycler.getviewforposition (0 );//fix dynamic Add times indexoutofboundsexception if (view!= null) {Recyclerview.layoutparams p = (Re

                Cyclerview.layoutparams) View.getlayoutparams (); int childwidthspec = Viewgroup.getchildmeasurespec (Widthspec, Getpaddingleft () + getpaddingright ()

                , p.width); int childheightspec = Viewgroup.getchilDmeasurespec (Heightspec, getpaddingtop () + Getpaddingbottom (), p.height);
                View.measure (Childwidthspec, Childheightspec);
                Measureddimension[0] = view.getmeasuredwidth () + P.leftmargin + p.rightmargin;
                MEASUREDDIMENSION[1] = view.getmeasuredheight () + P.bottommargin + p.topmargin;
            Recycler.recycleview (view);
        } catch (Exception e) {e.printstacktrace ();
 } finally {}}}


In addition, it can shield Recycleview sliding events

Recyclerview.setnestedscrollingenabled (FALSE);

Attach Custom Linearlayoutmanager


If these two methods are not good for conflict resolution I have another way

<relativelayout
    android:layout_width= "match_parent"
    android:layout_height= "Wrap_content"
    Android:tag= "(0,0)"
    
    >
    <android.support.v7.widget.recyclerview
        android:id= "@+id/lv_home_list"
        android:layout_width= "match_parent"
        android:layout_height= "wrap_content"

        android:layout_margintop = "0px"
        android:background= "@color/colorwhite"
        android:cliptopadding= "false"
        android:tag= "[ 0,0,0,0]{0,0,0,0} (0,0) "
        android:visibility=" visible "></android.support.v7.widget.RecyclerView>


</RelativeLayout>

Android:descendantfocusability= "Blocksdescendants"

You can set this property to


This property is the relationship between the ViewGroup and its child controls when a view gets the focus.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.