The solution of ScrollView nested listview sliding Conflict _android

Source: Internet
Author: User

ScrollView and ListView are not strangers to the controls, but they can be problematic when nested. For example, if we want to add other layouts or controls under ListView and then want them to slide as a whole, the most common idea is to wrap them with a scrollview. The idea seems very good, but the reality is a bit cruel. We can write a small example to experience.

First create an activity, placing a ListView on its layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  xmlns:tools=" Http://schemas.android.com/tools "
  android:layout_width=" Match_parent
  " android:layout_height= "Match_parent"
  android:orientation= "vertical"
  tools:context= " Com.lin.mr.mystudy.scrollview.TestActivity ">

  <listview
    android:id=" @+id/listview "
    android: Layout_width= "Match_parent"
    android:layout_height= "wrap_content" >
  </ListView>

</ Linearlayout>

You then use a for loop in your code to generate some data and use Arrayadapter to fit the data. This allows me to steal a lazy, ListView item layout directly using Android to provide the r.layout.simple_list_item_1, and not to customize it yourself.

public class Testactivity extends activity {
  private ListView ListView;
  Private arraylist<string> list;

  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_test);
    ListView = (ListView) Findviewbyid (R.id.listview);
Findviewbyid (R.id.ll_container);

    List = new arraylist<> ();
    Generate the data to be displayed in ListView for
    (int i = 0; i < i++) {
      List.add ("This Is Data" +i);
    }
    Use the Arrayadapter adapter data
    Listview.setadapter (this, Android. r.layout.simple_list_item_1,list));
  }


Make sure your current activity is starting the activity and then running the app, you can see the following effects:

Well, it looks fine, but what if we need to add some controls to the ListView's head or bottom, and then let them slide the whole thing? We can try this first:

<?xml version= "1.0" encoding= "Utf-8"?> <scrollview xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" tools:context= "com.lin.mr.mystudy.scrollview.TestActivity" > <linearlayout android:layout_wi
      Dth= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <listview

    Android:id= "@+id/listview" android:layout_width= "match_parent" android:layout_height= "Wrap_content" >
      </ListView> <imageview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:src= "@mipmap/ic_launcher"/> <imageview android:layout_width= "Wrap_content" android:l ayout_height= "Wrap_content" android:src= "@mipmap/ic_launcher"/> <button android:layout_width= "MATC H_parent "android:layout_height="Wrap_content" android:text= "button One"/> <button android:layout_width= "Match_parent" Android:layo

 ut_height= "wrap_content" android:text= "button two"/> </LinearLayout> </ScrollView>

Add a few controls to the head and bottom of the ListView, then wrap all the controls in a linear layout, then change the outermost layout to ScrollView and run again, trouble arises:

Days! There's only a small line left in our listview! Try to slide and find that there is no problem with sliding, that is, only one line can be displayed. So what do we do?

Don't worry, there is a simple way to revive the dead. We can customize a ListView:

/**
 * Custom ListView *
/public class Mylistview extends ListView {public

  Mylistview (context) {
    super (context);
  }

  Public Mylistview (context, AttributeSet attrs) {
    Super (context, attrs);
  }

  Public Mylistview (context, AttributeSet attrs, int defstyleattr) {
    Super (context, attrs, defstyleattr); c12/>}

  @Override
  protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
    Heightmeasurespec = Measurespec.makemeasurespec (integer.max_value>>2,//Right shift operator, equivalent to 4
        measurespec.at_ MOST);//measurement mode take maximum
    super.onmeasure (WIDTHMEASURESPEC,HEIGHTMEASURESPEC);//re-measure height
  }
}

In this listview we rewrite the Onmeasure method, and then redefine the Heightmeasurespec parameter, which takes a maximum of One-fourth of its size (the general practice), the maximum value of the measurement mode, It then invokes the constructor method of the parent class to heightmeasurespec the parameter again. These steps are designed to ensure that the height of the ListView is not problematic. When we are done, we use the custom ListView in the layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <scrollview xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" tools:context= "com.lin.mr.mystudy.scrollview.TestActivity" > <linearlayout android:layout_wi
      Dth= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <imageview Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:src= "@mipmap/ic_launcher" > <imageview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android
      : src= "@mipmap/ic_launcher"/> <com.lin.mr.mystudy.scrollview.mylistview android:id= "@+id/listView" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" > </com.lin.mr.mystudy.scrollview .
      Mylistview> <buttonAndroid:layout_margin= "4DP" android:layout_width= "match_parent" android:layout_height= "Wrap_content" and roid:text= "button One"/> <button android:layout_margin= "4DP" android:layout_width= "Match_parent" an

 droid:layout_height= "wrap_content" android:text= "button two"/> </LinearLayout> </ScrollView>

After running, found the problem solved! ListView can be fully displayed, and can also be slid to the head and top of the layout.

In fact, if you want to show ListView's head or bottom layout or control, you don't need to use ScrollView, we can also use the head and bottom as a whole layout, that is, head layout or foot layout, You can then call the ListView Addheaderview method or the Addfooterview method to add it to ListView's head or bottom.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.