Reproduced Android: How do I implement a ScrollView that contains a ListView?

Source: Internet
Author: User

Original Address:Android: How do I implement a ScrollView that contains a ListView?The Mailofzxflistview itself contains scrollview, so putting the ListView into ScrollView can cause confusion (who responds to the sliding event?). But sometimes there is a need to scroll through the contents of the ListView, along with other content.

There are two ways to embed a ListView in a ScrollView:

Method 1
: Use a ListView as a whole to place the parts that do not need to be scrolled into the header or footer of the ListView.
Note: Be sure to add Hearderview and Footerview first, and then set the adapter of the ListView.
Defect: Content in header or footer cannot be scrolled, and only one ListView can be allowed to exist at the same time.
The code is as follows:

Layoutinflater MLI =
(Layoutinflater) Context.getsystemservice (Context.layout_inflater_service);

ListView = (ListView) Findviewbyid (R.id.listview);
Datas = new arraylist<object> ();
Filldatas ();
Datasadapter = new Demolistadapter (context, datas);
Mylistheaderview Headerview = mli.inflate (r.layout.my_header_layout, NULL);
Filldatatoheaderview (Headerview);
Mylistfooterview Footerview = mli.inflate (r.layout.my_footer_layout, NULL);
Filldatatofooterview (Footerview);

Listview.addheaderview (Headerview);
Listview.addfooterview (Footerview);
Listview.setadapter (Datasadapter);

Method 2: With LinearLayoutSimulationListView, which enables scrolling of any content.
The following implementation is a generic linearlayoutforlistview that can be set to a different adapter to display different data.
The code for Linearlayoutforlistview is as follows:

public class Linearlayoutforlistview extends Android.widget.LinearLayout {
Static final String Log_tag = "Linearlayoutforlistview";
Private Android.widget.BaseAdapter adapter;
Private Onclicklistener onclicklistener = null;

public void Filllinearlayout () {
int count = Adapter.getcount ();
for (int i = 0; i < count; i++) {
View v = adapter.getview (i, NULL, NULL);
V.setonclicklistener (This.onclicklistener);
AddView (v, i);
}
LOG.V ("Counttag", "" + count);
}

Public Linearlayoutforlistview (Context context) {
Super (context);
}

Public Linearlayoutforlistview (context context, AttributeSet Attrs) {
Super (context, attrs);
}

Public Android.widget.BaseAdapter Getadpater () {
return adapter;
}

public void Setadapter (Android.widget.BaseAdapter adpater) {
This.adapter = Adpater;
Filllinearlayout ();
}

Public Onclicklistener Getonclicklistner () {
return onclicklistener;
}

public void Setonclicklinstener (Onclicklistener onclicklistener) {
This.onclicklistener = Onclicklistener;
}
}

Note that the member adapter above Linearlayoutforlistview is Baseadapter, so you can customize the other adapter to add data. The following is a adapter reference implementation that shows the contact information for a software developer:

public class Developerinfoadapter extends Baseadapter {
Static final String log_tag= "Developerinfo";
public final int view_index = 3000;
Private Context Mcontext;
Private string[] Mkeys;
Private string[] mtitles;
Private string[] mvals;
Layoutinflater MLI = null;

Public Developerinfoadapter (context context, string[] keys, string[] titles, string[] vals) {
Mcontext = context;
Mkeys = keys;
Mtitles = titles;
Mvals = Vals;
MLI = (layoutinflater) mcontext.getsystemservice (Context.layout_inflater_service);
To make sure Mkeys (...) is not null:
if (mkeys==null | | mtitles==null | | mvals==null) {
Mkeys = Mtitles = Mvals = new string[]{};
}
}

@Override
public int GetCount () {
return mkeys.length;
}

@Override
Public String getItem (int index) {
if (index>=0 && index<mvals.length)
return Mvals[index];
Else
return null;
}

@Override
public long getitemid (int position) {
return position;
}

@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
if (Position <0 | | position > Mkeys.length)
return null;
Final String key = Mkeys[position];
Final String title = Mtitles[position];
Final String val = mvals[position];

View v = convertview;
if (v = = null) {
v = mli.inflate (r.layout.dev_web_email_layout, NULL);
}
V.setid (Position+view_index);
V.setclickable (TRUE);

if (true) {
TextView V_title = (TextView) V.findviewbyid (r.id.dev_title);
TextView v_val = (TextView) V.findviewbyid (r.id.dev_val);
V_title.settext (title);
V_val.settext (Val);
}
return v;
}


private void Callbrowser (String URL) {
if (textutils.isempty (URL))
Return
Final String prefix = "http://";
try {
if (!url.startswith (prefix))
url = prefix + URL;
Uri uri = uri.parse (URL);
Intent Intent = new Intent (Intent.action_view, URI);
Mcontext.startactivity (Intent);
} catch (Exception e) {
String err_msg = E.getmessage ();
LOG.D (Log_tag, "Uri err:" + err_msg);
}
}

private void Callemail (String emailbox) {
if (Textutils.isempty (Emailbox))
Return
Final String prefix = "mailto:";
try {
if (!emailbox.startswith (prefix))
Emailbox = prefix + emailbox;
Uri uri = Uri.parse (Emailbox);
Intent Intent = new Intent (intent.action_sendto, URI);
Mcontext.startactivity (Intent);
} catch (Exception e) {
String err_msg = E.getmessage ();
LOG.D (Log_tag, "Uri err:" + err_msg);
}
}

Public Android.view.View.OnClickListener Myonclicklistener
= new Android.view.View.OnClickListener () {
@Override
public void OnClick (View v) {
try {
int id = V.getid ();
int position = Id-view_index;
if (Position <0 | | position > Mkeys.length)
Return
Final String key = Mkeys[position];
Final String title = Mtitles[position];
Final String val = mvals[position];

if ("website". Equals (key)) {
Callbrowser (Val);
} else if ("email". Equals (key)) {
Callemail (Val);
}
} catch (Exception e) {
Do nothing!
}
}
};

}

The relevant layout file sections are as follows:
<textview android:id= "@+id/detail_developer"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/lbl_title_developer"
Android:textstyle= "Bold"
Android:background= "#008F00"
>
</TextView>
<com.xxx.yyy.linearlayoutforlistview android:id= "@+id/detail_lst_web_email"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:visibility= "Visible"
Android:clickable= "true"
>
</com.xxx.yyy.LinearLayoutForListView>

The following code fills in the data for this fake ListView:
Com.xxx.yyy.LinearLayoutForListView Lst_developer
= (Com.xxx.yyy.LinearLayoutForListView) Findviewbyid (R.id.detail_lst_web_email);
String keys[]={"website", "email"};
String titles[]={getresources (). getString (R.string.lbl_title_developer_website),
Getresources (). getString (R.string.lbl_title_developer_email)};
String vals[]={"http://abc.com", "[Email protected]"};
Developerinfoadapter developeradapter = new Developerinfoadapter (this, keys, titles, vals);
Lst_developer.setonclicklinstener (Developeradapter.myonclicklistener);
Lst_developer.setadapter (Developeradapter);

< finish >

A companion tour, a free dating site: www.jieberu.com

Push family, free tickets, scenic spots: www.tuituizu.com

Reproduced Android: How do I implement a ScrollView that contains a ListView?

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.