ListView of Android Program development implement horizontal scrolling (with headers and fixed columns) _android

Source: Internet
Author: User
Tags int size

Problem background: When doing a chart show, the ListView can slide up and down, but the leftmost column slides to the right, stays the same, and the table head stays the same when it slides down.

Useful two ListView implementations, but tested, as if there were some problems

This example is implemented by (ListView + Horizontalscrollview)

Effect Chart:

Activity Code

/** * * * with sliding header and fixed column ListView/public class Hlistactivity extends activity{private ListView mlistview; 
Convenient testing, directly written by public public Horizontalscrollview Mtouchview; 
Load all Hscrollview protected list<chscrollview> mhscrollviews =new arraylist<chscrollview> (); 
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
Setcontentview (R.layout.scroll); 
Initviews (); } private void Initviews () {list<map<string, string>> datas = new arraylist<map<string,string>> 
(); 
map<string, string> data = null; 
ChScrollView Headerscroll = (ChScrollView) Findviewbyid (r.id.item_scroll_title); 
Add Head Sliding event Mhscrollviews.add (headerscroll); 
Mlistview = (ListView) Findviewbyid (r.id.scroll_list); 
for (int i = 0; i < i++) {data = new hashmap<string, string> (); 
Data.put ("title", "Title_" + i); 
Data.put ("Data_" + 1, "Date_" + 1 + "_" +i); 
Data.put ("Data_" + 2, "Date_" + 2 + "_" +i); Data.put ("Data_ "+ 3," Date_ "+ 3 +" _ "+i); 
Data.put ("Data_" + 4, "Date_" + 4 + "_" +i); 
Data.put ("Data_" + 5, "Date_" + 5 + "_" +i); 
Data.put ("Data_" + 6, "Date_" + 6 + "_" +i); 
Datas.add (data); } simpleadapter adapter = new Scrolladapter (this, datas, R.layout.item, new string[] {"title", "Data_1", "data_2", "Da Ta_3 "," Data_4 "," Data_5 "," Data_6 ",}, new int[] {r.id.item_title, r.id.item_data1, R.id.item_data2, R.id.item_d 
Ata3, R.id.item_data4, R.id.item_data5, r.id.item_data6}); 
Mlistview.setadapter (adapter); } public void Addhviews (final ChScrollView Hscrollview) {if (!mhscrollviews.isempty ()) {int size = Mhscrollviews.size ( 
); 
ChScrollView ScrollView = Mhscrollviews.get (size-1); 
Final int scrollx = SCROLLVIEW.GETSCROLLX (); After the first Mansi, slide down, and a piece of data was not added at the beginning of the IF (scrollx!= 0) {mlistview.post (new Runnable () {@Override public void run () {//when LISTVI 
After the EW refresh is finished, move the strip to the final position Hscrollview.scrollto (SCROLLX, 0); 
} 
}); 
} mhscrollviews.add (Hscrollview); } public VoID onscrollchanged (int l, int t, int oldl, int Oldt) {for (ChScrollView scrollview:mhscrollviews) {//Prevent repeat sliding if (mtouch 
View!= ScrollView) Scrollview.smoothscrollto (L, T); 
} class Scrolladapter extends Simpleadapter {private list< extends Map<string,?>> datas; 
private int res; 
Private string[] from; 
Private int[] to; 
private context; Public Scrolladapter (context, list<? extends Map<string,?>> data, int resource, string[] from, int[ 
To) {Super (context, data, resource, from, to); 
This.context = context; 
This.datas = data; 
This.res = resource; 
This.from = from; 
This.to = to; 
@Override public View getview (int position, view Convertview, ViewGroup parent) {View v = convertview; 
if (v = = null) {v = layoutinflater.from (context). Inflate (res, NULL); 
The first initialization is loaded in Addhviews ((ChScrollView) V.findviewbyid (R.id.item_scroll)); 
View[] views = new View[to.length]; for (int i = 0; i < to.length i++) {View TV = V.finDviewbyid (To[i]);; 
Tv.setonclicklistener (Clicklistener); 
Views[i] = TV; 
} v.settag (views); 
} view[] Holders = (view[]) V.gettag (); 
int len = holders.length; 
for (int i = 0; i < len i++) {(TextView) holders[i]). SetText (This.datas.get (position). Get (From[i)). toString ()); 
} return v; }//test-clicked Event protected View.onclicklistener Clicklistener = new View.onclicklistener () {@Override public void oncli 
CK (View v) {toast.maketext (Hlistactivity.this, (TextView) v). GetText (), Toast.length_short). Show (); 
} 
};  }

Slide View Code

/** 
* Sliding code 
/public class ChScrollView extends Horizontalscrollview { 
hlistactivity activity; 
Public ChScrollView (context, AttributeSet attrs, int defstyle) { 
Super (context, attrs, defstyle); 
Activity = (hlistactivity) context; 
} 
Public ChScrollView (context, AttributeSet attrs) { 
Super (context, attrs); 
Activity = (hlistactivity) context; 
} 
Public ChScrollView { 
super (context); 
Activity = (hlistactivity) context; 
} 
@Override Public 
Boolean ontouchevent (motionevent ev) { 
//touch Assignment 
Activity.mtouchview = this; 
return super.ontouchevent (EV); 
} 
@Override 
protected void onscrollchanged (int l, int t, int oldl, int Oldt) { 
//when the current ChScrollView is touched, slide the other 
if (Activity.mtouchview = = this) { 
activity.onscrollchanged (L, T, OLDL, Oldt); 
} else{ 
super.onscrollchanged (L, T, OLDL, Oldt); 
} 
 

The above is a small part of the Android program to introduce the development of the ListView to achieve horizontal scrolling (with the head and fixed column), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.