ListView first fixed other can slide sideways

Source: Internet
Author: User

ListView first fixed other can slide sideways

: http://download.csdn.net/detail/menglele1314/8640023

Custom Horizontalscrollview

public class ChScrollView extends Horizontalscrollview {

Mainactivity activity;

Public ChScrollView (context context, AttributeSet attrs, int defstyle) {
Super (context, attrs, Defstyle);
Activity = (mainactivity) context;
}


Public ChScrollView (context context, AttributeSet Attrs) {
Super (context, attrs);
Activity = (mainactivity) context;
}

Public ChScrollView (Context context) {
Super (context);
Activity = (mainactivity) context;
}

@Override
public boolean ontouchevent (Motionevent ev) {
Make 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);
}
}
}

/**
*
* ListView with sliding header and fixed column
*/
public class Mainactivity extends Activity {
Private ListView Mlistview;
Easy to test, direct write public
Public Horizontalscrollview Mtouchview;
Load all the Hscrollview
Private list<chscrollview> mhscrollviews = new arraylist<chscrollview> ();
Private ImageView Leftok,rightok,leftno,rightno;
Private GridView GridView;
Private list<book> datas = new arraylist<book> ();
private list<string> name;

Public Horizontalscrollview Gtouchview;
Load all the Hscrollview
Private list<ghscrollview> ghscrollviews = new arraylist<ghscrollview> ();
Private ImageView Leftview;
Private ImageView Rightview;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

Initgridview ();

Initlistview ();

}

private void Initgridview () {
SetData ();
Leftview = (ImageView) Findviewbyid (r.id.iv_left_ok_i);
Rightview = (ImageView) Findviewbyid (r.id.iv_right_ok_i);
GridView = (GridView) Findviewbyid (R.id.grid);
Ghscrollview Gscroll = (ghscrollview) Findviewbyid (r.id.grid_title);
Adding head swipe Events
Ghscrollviews.add (Gscroll);
int size = Name.size ();
Displaymetrics dm = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
float density = dm.density;
int gridviewwidth = (int) ((size *) * density);
Linearlayout.layoutparams params = new Linearlayout.layoutparams (
Gridviewwidth, LinearLayout.LayoutParams.WRAP_CONTENT);
Gridview.setlayoutparams (params); Width
Gridview.setcolumnwidth (80);
Gridview.setnumcolumns (size); Number of columns

Gridviewadapter adapter = new Gridviewadapter (this, name);
Gridview.setadapter (adapter);
}

private void Initlistview () {
Leftok = (ImageView) Findviewbyid (R.ID.IV_LEFT_OK);
Leftno = (ImageView) Findviewbyid (r.id.iv_left_no);
Rightok = (ImageView) Findviewbyid (R.ID.IV_RIGHT_OK);
Rightno = (ImageView) Findviewbyid (r.id.iv_right_no);
ChScrollView Headerscroll = (ChScrollView) Findviewbyid (r.id.item_scroll_title);
Adding head swipe Events
Mhscrollviews.add (Headerscroll);
Mlistview = (ListView) Findviewbyid (r.id.scroll_list);
for (int i = 0; i <; i++) {
Datas.add ("book" + I, "CCC" + I, "ddd" + I, "AAA" + I,
"BBB" + I, "eee" + I, "fff" + i));
}
ListAdapter adapter = new ListAdapter (this, mhscrollviews, Mlistview,
Datas);
Mlistview.setadapter (adapter);
}

public void ongcrollchanged (int l, int t, int oldl, int Oldt) {
Displaymetrics dm = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
int width = dm.widthpixels;
LOG.I ("Asfdsfas", l + "= =" +t+ "= =" +oldl+ "= =" +oldt+ "= =" +width);
if (L = = 0) {
Leftview.setvisibility (View.gone);
} else {
Leftview.setvisibility (view.visible);
}
if (L > width) {
Rightview.setvisibility (View.gone);
} else {
Rightview.setvisibility (view.visible);
}
for (Ghscrollview scrollview:ghscrollviews) {
Prevent repetitive sliding
if (Gtouchview! = ScrollView)
Scrollview.smoothscrollto (L, T);
}
}

public void onscrollchanged (int l, int t, int oldl, int Oldt) {
Displaymetrics dm = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
int width = dm.widthpixels;
if (L = = 0) {
Leftok.setvisibility (View.gone);
Leftno.setvisibility (view.visible);
} else {
Leftok.setvisibility (view.visible);
Leftno.setvisibility (View.gone);
}
if (l = = width) {
Rightok.setvisibility (View.gone);
Rightno.setvisibility (view.visible);
} else {
Rightok.setvisibility (view.visible);
Rightno.setvisibility (View.gone);
}
for (ChScrollView scrollview:mhscrollviews) {
Prevent repetitive sliding
if (Mtouchview! = ScrollView)
Scrollview.smoothscrollto (L, T);
}
}

private void SetData () {
name = new Arraylist<string> ();
Name.add ("Shenzhen");
Name.add ("Shanghai");
Name.add ("Guangzhou");
Name.add ("Beijing");
Name.add ("Wuhan");
Name.add ("Jinan");
Name.add ("Nanjing");
Name.add ("Tianjin");
Name.add ("Chongqing");
}

}

Adapter

public class ListAdapter extends Baseadapter {

Private list<book> Book;
Private Layoutinflater Inflater;
Private list<chscrollview> mhscrollviews;
Private ListView Mlistview;
Private context context;

Public ListAdapter (context context, list<chscrollview> mhscrollviews,
ListView Mlistview, list<book> book) {
This.context = context;
This.inflater = Layoutinflater.from (context);
This.mhscrollviews = Mhscrollviews;
This.mlistview = Mlistview;
This.book = Book;
}

@Override
public int GetCount () {
TODO auto-generated Method Stub
return Book.size ();
}

@Override
Public book getItem (int position) {
return Book.get (position);
}

@Override
public long getitemid (int position) {
TODO auto-generated Method Stub
return position;
}

@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {

Viewholder holder = null;
if (Convertview = = null) {
Convertview = inflater.inflate (R.layout.list_item, NULL);
The first time you initialize it, load it in.
Addhviews ((ChScrollView) Convertview.findviewbyid (R.id.item_scroll));
Holder = new Viewholder ();
Holder.item_title = (TextView) convertview
. Findviewbyid (R.id.item_title);
Holder.item_data1 = (TextView) convertview
. Findviewbyid (R.ID.ITEM_DATA1);
Holder.item_data2 = (TextView) convertview
. Findviewbyid (R.ID.ITEM_DATA2);
Holder.item_data3 = (TextView) convertview
. Findviewbyid (R.ID.ITEM_DATA3);
HOLDER.ITEM_DATA4 = (TextView) convertview
. Findviewbyid (R.ID.ITEM_DATA4);
Holder.item_data5 = (TextView) convertview
. Findviewbyid (R.ID.ITEM_DATA5);
HOLDER.ITEM_DATA6 = (TextView) convertview
. Findviewbyid (R.ID.ITEM_DATA6);
Convertview.settag (holder);
} else {
Holder = (viewholder) convertview.gettag ();
}

Final book book = GetItem (position);
Holder.item_title.setText (Book.getname ());
Holder.item_data1.setText (Book.geta ());
Holder.item_data2.setText (Book.getb ());
Holder.item_data3.setText (Book.getc ());
Holder.item_data4.setText (book.getd ());
Holder.item_data5.setText (Book.gete ());
Holder.item_data6.setText (BOOK.GETF ());
Holder.item_title.setOnClickListener (New Onclicklistener () {

@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
Toast.maketext (context, "xxx" + book.getname (), 1). Show ();
}
});

return convertview;
}

Private class Viewholder {
Private TextView Item_title;
Private TextView item_data1;
Private TextView item_data2;
Private TextView item_data3;
Private TextView item_data4;
Private TextView Item_data5;
Private TextView item_data6;
}

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 fullscreen, swipe down and one of the data is not added at the beginning
if (scrollx! = 0) {
Mlistview.post (New Runnable () {
@Override
public void Run () {
When the ListView refresh is complete, move the bar to the final position
Hscrollview.scrollto (SCROLLX, 0);
}
});
}
}
Mhscrollviews.add (Hscrollview);
}

}

......

: http://download.csdn.net/detail/menglele1314/8640023

ListView first fixed other can slide sideways

Related Article

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.