这篇文章主要介绍了仿饿了点餐界面2个ListView联动效果的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
如图是效果图:
是仿饿了的点餐界面
1.点击左侧的ListView,通过在在适配器中设置Item来改变颜色,再通过notifyDataSetInvalidated来刷新并用lv_home.setSelection(showTitle.get(arg2));来关联右侧的
2.右侧的主要是重写下onScroll的方法;来改变左侧ListView的颜色及背景
不过程序中还有个问题,望大神解答就是我右侧的ListView下拉时,上面的TextView能改变;但是上拉时,TextView的不能及时改变应为滑动时我只拿了firstVisibleItem来判断的
Demo的连接:http://download.csdn.net/detail/qq_29774291/9634011
如下是主程序代码:
1 package com.item.jiejie; 2 import java.util.ArrayList; 3 import java.util.List; 4 import com.item.jiejie.adapter.HomeAdapter; 5 import com.item.jiejie.adapter.MenuAdapter; 6 import com.item.jiejie.entity.FoodData; 7 import android.widget.AbsListView.OnScrollListener; 8 import android.os.Bundle; 9 import android.app.Activity; 10 import android.text.TextUtils; 11 import android.util.Log; 12 import android.view.View; 13 import android.view.Window; 14 import android.widget.AdapterView; 15 import android.widget.AdapterView.OnItemClickListener; 16 import android.widget.AbsListView; 17 import android.widget.ListView; 18 import android.widget.TextView; 19 /** 20 * 防饿了的ListView联动的Demo 21 * 有BUG 22 * @author Administrator 23 * 24 */ 25 public class MainActivity extends Activity { 26 /**左侧菜单*/ 27 private ListView lv_menu; 28 /**右侧主菜*/ 29 private ListView lv_home; 30 private TextView tv_title; 31 private MenuAdapter menuAdapter; 32 private HomeAdapter homeAdapter; 33 private int currentItem; 34 /** 35 * 数据源 36 */ 37 private List<FoodData> foodDatas; 38 private String data[] = {"热销榜","新品套餐","便当套餐","单点菜品","饮料类","水果罐头","米饭"}; 39 /** 40 * 里面存放右边ListView需要显示标题的条目position 41 */ 42 private ArrayList<Integer> showTitle; 43 @Override 44 protected void onCreate(Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 requestWindowFeature(Window.FEATURE_NO_TITLE); 47 setContentView(R.layout.activity_main); 48 setView(); 49 setData(); 50 } 51 private void setView() { 52 // TODO Auto-generated method stub 53 lv_menu = (ListView)findViewById(R.id.lv_menu); 54 tv_title = (TextView)findViewById(R.id.tv_titile); 55 lv_home = (ListView)findViewById(R.id.lv_home); 56 foodDatas = new ArrayList<FoodData>(); 57 for(int i =0;i < data.length; i++){ 58 foodDatas.add(new FoodData(i, data[0] + i, data[0])); 59 } 60 for(int i =0;i < data.length -1; i++){ 61 foodDatas.add(new FoodData(i, data[1] + i, data[1])); 62 } 63 for(int i =0;i < data.length-2; i++){ 64 foodDatas.add(new FoodData(i, data[2] + i, data[2])); 65 } 66 for(int i =0;i < data.length-3; i++){ 67 foodDatas.add(new FoodData(i, data[3] + i, data[3])); 68 } 69 for(int i =0;i < data.length-4; i++){ 70 foodDatas.add(new FoodData(i, data[4] + i, data[4])); 71 } 72 for(int i =0;i < data.length-3; i++){ 73 foodDatas.add(new FoodData(i, data[5] + i, data[5])); 74 } 75 for(int i =0;i < 6; i++){ 76 foodDatas.add(new FoodData(i, data[6] + i, data[6])); 77 } 78 showTitle = new ArrayList<Integer>(); 79 for(int i = 0; i < foodDatas.size(); i++){ 80 if( i ==0){ 81 showTitle.add(i ); 82 System.out.println(i + "dd"); 83 }else if (!TextUtils.equals(foodDatas.get(i).getTitle(), foodDatas.get(i - 1).getTitle())) { 84 showTitle.add(i ); 85 System.out.println(i + "dd"); 86 } 87 } 88 } 89 private void setData() { 90 // TODO Auto-generated method stub 91 tv_title.setText(foodDatas.get(0).getTitle()); 92 menuAdapter = new MenuAdapter(this); 93 homeAdapter = new HomeAdapter(this, foodDatas); 94 lv_menu.setAdapter(menuAdapter); 95 lv_home.setAdapter(homeAdapter); 96 lv_menu.setOnItemClickListener(new OnItemClickListener() { 97 @Override 98 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 99 long arg3) { 100 // TODO Auto-generated method stub 101 menuAdapter.setSelectItem(arg2); 102 menuAdapter.notifyDataSetInvalidated(); 103 lv_home.setSelection(showTitle.get(arg2)); 104 tv_title.setText(data[arg2]); 105 } 106 }); 107 lv_home.setOnScrollListener(new OnScrollListener() { 108 private int scrollState; 109 @Override 110 public void onScrollStateChanged(AbsListView view, int scrollState) { 111 // TODO Auto-generated method stub 112 //System.out.println("onScrollStateChanged" + " scrollState" + scrollState); 113 this.scrollState = scrollState; 114 } 115 @Override 116 public void onScroll(AbsListView view, int firstVisibleItem, 117 int visibleItemCount, int totalItemCount) { 118 // TODO Auto-generated method stub 119 if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { 120 return; 121 } 122 Log.d("jiejie", "onScroll" + " firstVisibleItem" + firstVisibleItem 123 +" visibleItemCount" + visibleItemCount + " totalItemCount" + totalItemCount); 124 int current =showTitle.indexOf(firstVisibleItem ); 125 System.out.println(current + "dd" + firstVisibleItem); 126 // lv_home.setSelection(current); 127 if(currentItem != current && current >=0){ 128 currentItem = current; 129 tv_title.setText(data[current]); 130 menuAdapter.setSelectItem(currentItem); 131 menuAdapter.notifyDataSetInvalidated(); 132 } 133 } 134 }); 135 } 136 }
左侧ListView的适配器代码:
1 package com.item.jiejie.adapter; 2 import com.item.jiejie.R; 3 import android.content.Context; 4 import android.graphics.Color; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.BaseAdapter; 8 import android.widget.TextView; 9 /** 10 * 左侧菜单ListView的适配器 11 * @author Administrator 12 * 13 */ 14 public class MenuAdapter extends BaseAdapter{ 15 private Context context; 16 private int selectItem = 0; 17 private String data[] = {"热销榜","新品套餐","便当套餐","单点菜品","饮料类","水果罐头","米饭"}; 18 public MenuAdapter(Context context) { 19 this.context = context; 20 } 21 public int getSelectItem() { 22 return selectItem; 23 } 24 public void setSelectItem(int selectItem) { 25 this.selectItem = selectItem; 26 } 27 @Override 28 public int getCount() { 29 // TODO Auto-generated method stub 30 return data.length; 31 } 32 @Override 33 public Object getItem(int arg0) { 34 // TODO Auto-generated method stub 35 return null; 36 } 37 @Override 38 public long getItemId(int arg0) { 39 // TODO Auto-generated method stub 40 return 0; 41 } 42 @Override 43 public View getView(int arg0, View arg1, ViewGroup arg2) { 44 // TODO Auto-generated method stub 45 ViewHolder holder = null; 46 if(arg1 == null) { 47 holder = new ViewHolder(); 48 arg1 = View.inflate(context, R.layout.item_menu, null); 49 holder.tv_name = (TextView)arg1.findViewById(R.id.item_name); 50 arg1.setTag(holder); 51 }else { 52 holder = (ViewHolder)arg1.getTag(); 53 } 54 if(arg0 == selectItem){ 55 holder.tv_name.setBackgroundColor(Color.WHITE); 56 holder.tv_name.setTextColor(context.getResources().getColor(R.color.text_green)); 57 }else { 58 holder.tv_name.setBackgroundColor(context.getResources().getColor(R.color.ll_coachback)); 59 holder.tv_name.setTextColor(context.getResources().getColor(R.color.text_deep)); 60 } 61 holder.tv_name.setText(data[arg0]); 62 return arg1; 63 } 64 static class ViewHolder{ 65 private TextView tv_name; 66 } 67 }
右侧的适配器代码:
1 package com.item.jiejie.adapter; 2 import java.util.List; 3 import com.item.jiejie.R; 4 import com.item.jiejie.entity.FoodData; 5 import android.content.Context; 6 import android.text.TextUtils; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.BaseAdapter; 10 import android.widget.TextView; 11 /** 12 * 右侧主界面ListView的适配器 13 * 14 * @author Administrator 15 * 16 */ 17 public class HomeAdapter extends BaseAdapter { 18 private Context context; 19 private List<FoodData> foodDatas; 20 public HomeAdapter(Context context, List<FoodData> foodDatas) { 21 this.context = context; 22 this.foodDatas = foodDatas; 23 } 24 @Override 25 public int getCount() { 26 // TODO Auto-generated method stub 27 if(foodDatas!=null){ 28 return foodDatas.size(); 29 }else { 30 return 10; 31 } 32 } 33 @Override 34 public Object getItem(int arg0) { 35 // TODO Auto-generated method stub 36 return null; 37 } 38 @Override 39 public long getItemId(int arg0) { 40 // TODO Auto-generated method stub 41 return 0; 42 } 43 @Override 44 public View getView(int arg0, View arg1, ViewGroup arg2) { 45 // TODO Auto-generated method stub 46 ViewHold holder = null; 47 if(arg1 == null){ 48 arg1 = View.inflate(context, R.layout.item_home, null); 49 holder = new ViewHold(); 50 holder.tv_title = (TextView)arg1.findViewById(R.id.item_home_title); 51 holder.tv_name = (TextView)arg1.findViewById(R.id.item_home_name); 52 arg1.setTag(holder); 53 }else { 54 holder = (ViewHold)arg1.getTag(); 55 } 56 holder.tv_name.setText(foodDatas.get(arg0).getName()); 57 holder.tv_title.setText(foodDatas.get(arg0).getTitle()); 58 if(arg0 == 0){ 59 holder.tv_title.setVisibility(View.VISIBLE); 60 }else if (!TextUtils.equals(foodDatas.get(arg0).getTitle(), foodDatas.get(arg0 -1).getTitle())) { 61 holder.tv_title.setVisibility(View.VISIBLE); 62 }else { 63 holder.tv_title.setVisibility(View.GONE); 64 } 65 return arg1; 66 } 67 private static class ViewHold{ 68 private TextView tv_title; 69 private TextView tv_name; 70 } 71 }
以上所述是给大家介绍的仿饿了点餐界面2个ListView联动效果,希望对大家有所帮助,如果大家有任何疑问请给我留言。