Are you hungry? Menu two ListView linkage effect _android

Source: Internet
Author: User
Tags getcolor static class stub

As shown in the picture is the effect diagram

It's an empty menu.

1. Click on the left side of the ListView, by setting the item in the adapter to change the color, and then through the notifydatasetinvalidated to refresh and Lv_home.setselection (Showtitle.get (arg2)); To associate the right side of the

2. The main right side is to rewrite the lower onscroll method; To change the color and background of the left ListView.

However, there is a problem in the program, hope that the big God answer is my right side of the ListView, the top of the TextView can change, but on the pull, TextView can not be timely change should be sliding when I only take the Firstvisibleitem to judge

Demo Connection: http://download.csdn.net/detail/qq_29774291/9634011

The following is the main program code

Package Com.item.jiejie;
Import java.util.ArrayList;
Import java.util.List;
Import Com.item.jiejie.adapter.HomeAdapter;
Import Com.item.jiejie.adapter.MenuAdapter;
Import Com.item.jiejie.entity.FoodData;
Import Android.widget.AbsListView.OnScrollListener;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.text.TextUtils;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.Window;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.AbsListView;
Import Android.widget.ListView;
Import Android.widget.TextView; /** * ListView Linkage Demo * has bugs * * * @author Administrator */public class Mainactivity extends activity {/** left menu */Priva
Te ListView Lv_menu;
/** Right main dish/private ListView lv_home;
Private TextView Tv_title;
Private Menuadapter Menuadapter;
Private Homeadapter Homeadapter;
private int CurrentItem;
/** * Data Source * * Private list<fooddata> Fooddatas; Private String data[] = {"Hot List", "New package", "Bento Package "," a la carte Products "," Beverage category "," canned fruit "," Rice "};
/** * Inside Store right ListView need to display the title of the entry position * * Private arraylist<integer> showtitle;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title);
Setcontentview (R.layout.activity_main);
Setview ();
SetData (); private void Setview () {//TODO auto-generated method Stub lv_menu = (ListView) Findviewbyid (r.id.lv_menu); tv_title = (
TextView) Findviewbyid (r.id.tv_titile);
Lv_home = (ListView) Findviewbyid (r.id.lv_home);
Fooddatas = new arraylist<fooddata> (); for (int i =0;i < data.length i++) {fooddatas.add (new Fooddata (I, data[0] + i, data[0]));} for (int i =0;i < data.le Ngth-1; i++) {Fooddatas.add (new Fooddata (I, data[1] + i, data[1]));} for (int i =0;i < data.length-2; i++) {Fooddatas.add (new F
Ooddata (i, data[2] + i, data[2])); for (int i =0;i < data.length-3 i++) {fooddatas.add (new Fooddata (I, data[3] + i, data[3]));} for (int i =0;i < dat A.length-4; i++) {Fooddatas.add (new Fooddata (I, data[4] + i, data[4]));} for (int i =0;i < data.length-3; i++) {Fooddatas.add (new F
Ooddata (i, data[5] + i, data[5])); for (int i =0;i < 6; i++) {Fooddatas.add (new Fooddata (I, data[6] + i, data[6));} showtitle = new Arraylist<intege
R> ();
for (int i = 0; i < fooddatas.size (); i++) {if (I ==0) {showtitle.add (i);
SYSTEM.OUT.PRINTLN (i + "dd"); }else if (!
Textutils.equals (Fooddatas.get (i). GetTitle (), Fooddatas.get (i-1). GetTitle ())) {Showtitle.add (i);
SYSTEM.OUT.PRINTLN (i + "dd"); }} private void SetData () {//TODO auto-generated Method Stub tv_title.settext (Fooddatas.get (0). GetTitle ()); menuadapt
ER = new Menuadapter (this);
Homeadapter = new Homeadapter (this, fooddatas);
Lv_menu.setadapter (Menuadapter);
Lv_home.setadapter (Homeadapter); Lv_menu.setonitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (adapterview<?> arg0, View arg1, int arg2, long arg3) {//TODO auto-generated method stub MenuadapTer.setselectitem (ARG2);
Menuadapter.notifydatasetinvalidated ();
Lv_home.setselection (Showtitle.get (arg2));
Tv_title.settext (Data[arg2]);
}
}); Lv_home.setonscrolllistener (New Onscrolllistener () {private int scrollstate; @Override public void Onscrollstatechanged (abslistview view, int scrollstate) {//TODO auto-generated Method stub//system.out.println ("
Onscrollstatechanged "+" scrollstate "+ scrollstate);
This.scrollstate = scrollstate; @Override public void Onscroll (Abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {//To 
Do auto-generated method stub if (scrollstate = = AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {return; } log.d ("Jiejie", "onscroll" + "Firstvisibleitem" + Firstvisibleitem + "VisibleItemCount" + VisibleItemCount + "totalIt")
Emcount "+ totalitemcount);
int current =showtitle.indexof (firstvisibleitem);
SYSTEM.OUT.PRINTLN (current + "DD" + firstvisibleitem);
Lv_home.setselection (current); if (CurrentItem!= current &AMP;&Amp
Current >=0} {CurrentItem = current; Tv_title.settext (data[current)); Menuadapter.setselectitem (CurrentItem);
Menuadapter.notifydatasetinvalidated ();
}
}
}); }
}

Left-ListView Adapter code

Package com.item.jiejie.adapter;
Import COM.ITEM.JIEJIE.R;
Import Android.content.Context;
Import Android.graphics.Color;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.TextView; 
/** * Left Menu ListView Adapter * @author Administrator * * */public class Menuadapter extends baseadapter{private context;
private int selectitem = 0;
Private String data[] = {"Hot List", "New Package", "Bento package", "à la carte", "beverage category", "canned fruit", "Rice"}; Public Menuadapter {This.context = context, public int Getselectitem () {return selectitem;} public v OID Setselectitem (int selectitem) {this.selectitem = SelectItem; @Override public int GetCount () {//TODO Auto-generat
Ed method stubs return data.length; @Override public Object getitem (int arg0) {//TODO auto-generated method stub return null;} @Override public Long Geti Temid (int arg0) {//TODO auto-generated method stub return 0;} @Override public View getview (int arg0, View arg1, VIEWGR OUP ARG2) {//TODO auto-generated method stub viewholder holder = null; if (arg1 = = null) {holder = new Viewholder (); arg1 = Vie
W.inflate (context, r.layout.item_menu, null);
Holder.tv_name = (TextView) Arg1.findviewbyid (r.id.item_name);
Arg1.settag (holder);
}else {holder = (Viewholder) Arg1.gettag ();} if (arg0 = = SelectItem) {holder.tv_name.setBackgroundColor (color.white);
Holder.tv_name.setTextColor (Context.getresources (). GetColor (R.color.text_green)); }else {Holder.tv_name.setBackgroundColor (Context.getresources (). GetColor (R.color.ll_coachback)); holder.tv_
Name.settextcolor (Context.getresources (). GetColor (R.color.text_deep));
} holder.tv_name.setText (data[arg0]);
return arg1; Static class viewholder{private TextView Tv_name}}

Adapter code on the right

Package com.item.jiejie.adapter;
Import java.util.List;
Import COM.ITEM.JIEJIE.R;
Import Com.item.jiejie.entity.FoodData;
Import Android.content.Context;
Import Android.text.TextUtils;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.TextView; /** * Right Main interface ListView Adapter * * @author Administrator */public class Homeadapter extends Baseadapter {private Context Co
ntext
Private list<fooddata> Fooddatas; 
Public Homeadapter (context context, list<fooddata> Fooddatas) {this.context = context; this.fooddatas = Fooddatas; @Override public int GetCount () {//TODO auto-generated Method stub if (fooddatas!=null) {return fooddatas.size ();} 
else {return 10;} @Override public Object getitem (int arg0) {//TODO auto-generated method stub return null;} @Override public Long Geti Temid (int arg0) {//TODO auto-generated method stub return 0;} @Override public View getview (int arg0, View arg1, VIEWGR OUP arg2) {//TODO auto-generated Method stub viewhold holder = null; if (arg1 = = null) {arg1 = View.inflate (context, r.layout.item_home, null); holder = new Viewhold (); holder.tv_title = (Text
View) Arg1.findviewbyid (r.id.item_home_title);
Holder.tv_name = (TextView) Arg1.findviewbyid (r.id.item_home_name);
Arg1.settag (holder);
}else {holder = (viewhold) Arg1.gettag ();} holder.tv_name.setText (Fooddatas.get (arg0). GetName ());
Holder.tv_title.setText (Fooddatas.get (arg0). GetTitle ()); if (arg0 = = 0) {holder.tv_title.setVisibility (view.visible);} else if (! Textutils.equals (Fooddatas.get (arg0). GetTitle (), Fooddatas.get (arg0-1). GetTitle ()) {holder.tv_
Title.setvisibility (view.visible);
}else {holder.tv_title.setVisibility (view.gone);} return arg1; private static class viewhold{private TextView tv_title; private TextView tv_name;}

The above is a small set to introduce the imitation of Hungry dinner point 2 listview linkage effect, 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.