本文主要是介绍仿饿了么美团点餐界面,listView的二级联动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目需求,需要在项目里加入点餐的功能,自己简单写了一个,
先看图,界面丑勿喷:
只是做了简单的效果,实现点击左边右边改变,滑动右边左边改变,代码量很少,可以参考下
这是项目目录:
布局文件
左边listView 适配器
public class LeftAdapter extends BaseAdapter{
private List<LeftMenu> dataList;
private Context contex;
private LayoutInflater inflater;
private int currentItem = 0;//当前item
private String click;
public LeftAdapter(Context context,List<LeftMenu> datas,int pos,String isClick) {
// TODO Auto-generated constructor stub
this.click = isClick;
this.contex = context;
this.dataList = datas;
this.currentItem = pos;
this.inflater = LayoutInflater.from(context);
}
public LeftAdapter(Context context,List<LeftMenu> datas) {
// TODO Auto-generated constructor stub
this.contex = context;
this.dataList = datas;
this.inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return dataList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.left_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
LeftMenu menu = dataList.get(position);
holder.textView.setText(menu.getLeftMenu());
if(click != ""){//如果点击不为空,就是点击了左边的item
if(currentItem == position){//如果当前条目等于position
holder.textView.setBackgroundColor(Color.BLUE);//设置选择状态的背景颜色
}
}else {
if(position == 0){
holder.textView.setBackgroundColor(Color.BLUE);//设置未选择状态的背景颜色
}
}
return convertView;
}
class ViewHolder{
TextView textView;
public ViewHolder(View convertView) {
// TODO Auto-generated constructor stub
textView = (TextView) convertView.findViewById(R.id.left_lv);
}
}
}
右边适配器
public class RightAdapter extends BaseAdapter{
private List<RightMenu> dataList;
private Context contex;
private LayoutInflater inflater;
private RightItemAdapter itemAdapter;
public RightAdapter(Context context,List<RightMenu> data) {
// TODO Auto-generated constructor stub
this.dataList =data;
this.contex = context;
this.inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return dataList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.right_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
itemAdapter = new RightItemAdapter(contex,dataList,position);
holder.cListView.setAdapter(itemAdapter);
RightMenu menu = dataList.get(position);
holder.textView.setText(menu.getRightTitle());
return convertView;
}
class ViewHolder{
CostomListView cListView;
TextView textView;
public ViewHolder(View convertView) {
// TODO Auto-generated constructor stub
textView = (TextView) convertView.findViewById(R.id.right_lv);
cListView = (CostomListView) convertView.findViewById(R.id.right_cos_list);
}
}
}
下载地址 http://download.csdn.net/download/zdd199401/9956885
这篇关于仿饿了么美团点餐界面,listView的二级联动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!