欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

仿饿了吗点餐界面两个ListView联动效果

程序员文章站 2024-03-04 22:06:36
如图是效果图 是仿饿了的点餐界面 1.点击左侧的listview,通过在在适配器中设置item来改变颜色,再通过notifydatasetinvalidat...

如图是效果图

仿饿了吗点餐界面两个ListView联动效果

是仿饿了的点餐界面

1.点击左侧的listview,通过在在适配器中设置item来改变颜色,再通过notifydatasetinvalidated来刷新并用lv_home.setselection(showtitle.get(arg2));来关联右侧的

2.右侧的主要是重写下onscroll的方法;来改变左侧listview的颜色及背景

不过程序中还有个问题,望大神解答就是我右侧的listview下拉时,上面的textview能改变;但是上拉时,textview的不能及时改变应为滑动时我只拿了firstvisibleitem来判断的

demo的连接:

如下是主程序代码

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联动的demo
* 有bug
* @author administrator
*
*/
public class mainactivity extends activity {
/**左侧菜单*/
private listview lv_menu;
/**右侧主菜*/
private listview lv_home;
private textview tv_title;
private menuadapter menuadapter;
private homeadapter homeadapter;
private int currentitem;
/**
* 数据源
*/
private list<fooddata> fooddatas;
private string data[] = {"热销榜","新品套餐","便当套餐","单点菜品","饮料类","水果罐头","米饭"};
/**
* 里面存放右边listview需要显示标题的条目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.length -1; i++){
fooddatas.add(new fooddata(i, data[1] + i, data[1]));
}
for(int i =0;i < data.length-2; i++){
fooddatas.add(new fooddata(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 < data.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 fooddata(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<integer>();
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());
menuadapter = 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) {
// todo auto-generated method stub
if (scrollstate == abslistview.onscrolllistener.scroll_state_idle) { 
return; 
} 
log.d("jiejie", "onscroll" + " firstvisibleitem" + firstvisibleitem
+" visibleitemcount" + visibleitemcount + " totalitemcount" + totalitemcount);
int current =showtitle.indexof(firstvisibleitem );
system.out.println(current + "dd" + firstvisibleitem);
// lv_home.setselection(current);
if(currentitem != current && current >=0){
currentitem = current;
tv_title.settext(data[current]);
menuadapter.setselectitem(currentitem);
menuadapter.notifydatasetinvalidated();
}
}
});
}
}

左侧listview的适配器代码

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;
/**
* 左侧菜单listview的适配器
* @author administrator
*
*/
public class menuadapter extends baseadapter{
private context context;
private int selectitem = 0;
private string data[] = {"热销榜","新品套餐","便当套餐","单点菜品","饮料类","水果罐头","米饭"};
public menuadapter(context context) {
this.context = context;
}
public int getselectitem() {
return selectitem;
}
public void setselectitem(int selectitem) {
this.selectitem = selectitem;
}
@override
public int getcount() {
// todo auto-generated method stub
return data.length;
}
@override
public object getitem(int arg0) {
// todo auto-generated method stub
return null;
}
@override
public long getitemid(int arg0) {
// todo auto-generated method stub
return 0;
}
@override
public view getview(int arg0, view arg1, viewgroup arg2) {
// todo auto-generated method stub
viewholder holder = null;
if(arg1 == null) {
holder = new viewholder();
arg1 = view.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;
}
}

右侧的适配器代码

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;
/**
* 右侧主界面listview的适配器
* 
* @author administrator
* 
*/
public class homeadapter extends baseadapter {
private context context;
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 getitemid(int arg0) {
// todo auto-generated method stub
return 0;
}
@override
public view getview(int arg0, view arg1, viewgroup 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 = (textview)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;
}
}

以上所述是小编给大家介绍的仿饿了点餐界面2个listview联动效果,希望对大家有所帮助