仿饿了点餐界面2个ListView联动
Posted 王丰蛋哥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了仿饿了点餐界面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
如下是主程序代码
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联动的主要内容,如果未能解决你的问题,请参考以下文章