仿美团二级菜单
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了仿美团二级菜单相关的知识,希望对你有一定的参考价值。
最近要做一个仿美团似的二级菜单 在csdn上找了demo 下载下来有错误不能运行 自己又整理了一下 终于可以运行实现效果了。布局xml就不贴出代码了,demo可以去csdn去下载:(后续补上链接)
1)分析思路,最外围的是一个Activity,顶部包含了一个View的容器,这个容器主要是装载ToggleButton来实现诸如美团里面的“美食,全城,理我最近,刷选”这一行。这一行一点就会弹出对应的下来菜单。
2)下拉菜单是如何实现的呢?,这里我们利用了PopupWindow来实现这一弹出式窗口。然后我们在弹出式窗口里面再定义我们的下来列表项,是单列还是二级菜单,都是由里面来定。
3)不同的菜单,需要一级或者需要二级,在这里根据我的需求而变动。我们在PopupWindow上面加一个自定义的LeftView,或者是MiddleView,RightView。主要是一个ToggleButton,你弹出一个窗口,你就定制一个窗口。
3)视图里面嵌入ListView,就形成了列表项。
package com.example.meituan.view; import java.util.ArrayList; import com.example.meituandemo2.R; import android.app.Activity; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.ToggleButton; import android.widget.PopupWindow; import android.widget.PopupWindow.OnDismissListener; /** * 最主要就是如何处理当我们点击这些ToggleButton的时候要弹出或者收起这些PopupWindow * 它包含ToggleButton容器和PopupWindow,是控制弹出窗口的核心类 * @author Administrator * */ public class ExpandTabView extends LinearLayout implements OnDismissListener { private static final String TAG = "ExpandTabView"; private ToggleButton selectedButton; private Context mContext; private int displayWidth; private int displayHeight; private final int SMALL = 0; private int selectPosition; private PopupWindow popupWindow; private ArrayList<String> mTextArray = new ArrayList<String>(); private ArrayList<RelativeLayout> mViewArray = new ArrayList<RelativeLayout>(); private ArrayList<ToggleButton> mToggleButton = new ArrayList<ToggleButton>(); public ExpandTabView(Context context) { super(context); init(context); } public ExpandTabView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } /** * 根据选择的位置设置tabitem显示的值 */ public void setTitle(String valueText, int position) { if (position < mToggleButton.size()) { mToggleButton.get(position).setText(valueText); } } /** * 根据选择的位置获取tabitem显示的值 */ public String getTitle(int position) { if (position < mToggleButton.size() && mToggleButton.get(position).getText() != null) { return mToggleButton.get(position).getText().toString(); } return ""; } /** * 设置tabitem的个数和初始值 * @param textArray 标题数组 * @param viewArray 控件数组 */ public void setValue(ArrayList<String> textArray, ArrayList<View> viewArray) { if (mContext == null) { return ; } LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); Log.d(TAG,"setValue"); mTextArray = textArray; for (int i = 0; i < viewArray.size(); i++) { final RelativeLayout r = new RelativeLayout(mContext); int maxHeight = (int) (displayHeight * 0.7); RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, maxHeight); rl.leftMargin = 10; rl.rightMargin = 10; r.addView(viewArray.get(i), rl); mViewArray.add(r); r.setTag(SMALL); ToggleButton tButton = (ToggleButton) inflater.inflate(R.layout.toggle_button, this, false); addView(tButton); View line = new TextView(mContext); line.setBackgroundResource(R.drawable.choosebar_line); if (i < viewArray.size() - 1) { LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(2, LinearLayout.LayoutParams.MATCH_PARENT); addView(line, lp); } mToggleButton.add(tButton); tButton.setTag(i); tButton.setText(mTextArray.get(i)); r.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.d("RelativeLayout","view:"+v); onPressBack(); } }); r.setBackgroundColor(mContext.getResources().getColor(R.color.popup_main_background)); tButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Log.d("tButton","setOnClickListener(l)"); // initPopupWindow(); ToggleButton tButton = (ToggleButton) view; if (selectedButton != null && selectedButton != tButton) { selectedButton.setChecked(false); } selectedButton = tButton; selectPosition = (Integer) selectedButton.getTag(); startAnimation(); if (mOnButtonClickListener != null && tButton.isChecked()) { mOnButtonClickListener.onClick(selectPosition); } } }); }// for.. } private void startAnimation() { Log.d(TAG,"startAnimation"); if (popupWindow == null) { Log.d(TAG,"startAnimation(),new popupWindow now"); popupWindow = new PopupWindow(mViewArray.get(selectPosition), displayWidth, displayHeight); popupWindow.setAnimationStyle(R.style.PopupWindowAnimation); popupWindow.setFocusable(false); popupWindow.setOutsideTouchable(true); } Log.d(TAG,"startAnimation(),selectedButton:"+selectedButton+",isChecked:"+selectedButton.isChecked()+ ",popupWindow.isShowing:"+popupWindow.isShowing()); if (selectedButton.isChecked()) { if (!popupWindow.isShowing()) { showPopup(selectPosition); } else { popupWindow.setOnDismissListener(this); popupWindow.dismiss(); hideView(); } } else { if (popupWindow.isShowing()) { popupWindow.dismiss(); hideView(); } } } private void showPopup(int position) { View tView = mViewArray.get(selectPosition).getChildAt(0); if (tView instanceof ViewBaseAction) { ViewBaseAction f = (ViewBaseAction) tView; f.show(); } if (popupWindow.getContentView() != mViewArray.get(position)) { popupWindow.setContentView(mViewArray.get(position)); } popupWindow.showAsDropDown(this, 0, 0); } /** * 如果菜单成展开状态,则让菜单收回去 */ public boolean onPressBack() { Log.d(TAG,"onPressBack"); if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); hideView(); if (selectedButton != null) { selectedButton.setChecked(false); } return true; } else { return false; } } private void hideView() { Log.d(TAG, "hide()"); View tView = mViewArray.get(selectPosition).getChildAt(0); if (tView instanceof ViewBaseAction) { ViewBaseAction f = (ViewBaseAction) tView; f.hide(); } } private void init(Context context) { mContext = context; displayWidth = ((Activity) mContext).getWindowManager().getDefaultDisplay().getWidth(); displayHeight = ((Activity) mContext).getWindowManager().getDefaultDisplay().getHeight(); setOrientation(LinearLayout.HORIZONTAL); } @Override public void onDismiss() { Log.d(TAG,"onDismiss,selectPosition:"+selectPosition); showPopup(selectPosition); popupWindow.setOnDismissListener(null); } private OnButtonClickListener mOnButtonClickListener; /** * 设置tabitem的点击监听事件 */ public void setOnButtonClickListener(OnButtonClickListener l) { mOnButtonClickListener = l; } /** * 自定义tabitem点击回调接口 */ public interface OnButtonClickListener { public void onClick(int selectPosition); } }
package com.example.meituan.view; public interface ViewBaseAction { /** * 菜单隐藏操作 */ public void hide(); /** * 菜单显示操作 */ public void show(); }
package com.example.meituan.view; import com.example.meituan.adapter.TextAdapter; import com.example.meituan.adapter.TextAdapter.OnItemClickListener; import com.example.meituandemo2.R; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.Toast; /** * 是弹出里面嵌套的类,实现不同的列表菜单。 * @author Administrator * */ public class ViewLeft extends RelativeLayout implements ViewBaseAction{ private ListView mListView; private final String[] items = new String[] { "item1", "item2", "item3", "item4", "item5", "item6" };//显示字段 private final String[] itemsVaule = new String[] { "1", "2", "3", "4", "5", "6" };//隐藏id private OnSelectListener mOnSelectListener; private TextAdapter adapter; private String mDistance; private String showText = "item1"; private Context mContext; public String getShowText() { return showText; } public ViewLeft(Context context) { super(context); init(context); } public ViewLeft(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } public ViewLeft(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { mContext = context; LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.view_distance, this, true); setBackgroundDrawable(getResources().getDrawable(R.drawable.choosearea_bg_left)); mListView = (ListView) findViewById(R.id.listView); adapter = new TextAdapter(context, items, R.drawable.choose_item_right, R.drawable.choose_eara_item_selector); adapter.setTextSize(17); if (mDistance != null) { for (int i = 0; i < itemsVaule.length; i++) { if (itemsVaule[i].equals(mDistance)) { adapter.setSelectedPositionNoNotify(i); showText = items[i]; break; } } } mListView.setAdapter(adapter); adapter.setOnItemClickListener(new TextAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position) { if (mOnSelectListener != null) { showText = items[position]; mOnSelectListener.getValue(itemsVaule[position], items[position]); } } }); } public void setOnSelectListener(OnSelectListener onSelectListener) { mOnSelectListener = onSelectListener; } public interface OnSelectListener { public void getValue(String distance, String showText); } @Override public void hide() { } @Override public void show() { } }
package com.example.meituan.view; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import com.example.meituan.adapter.TextAdapter; import com.example.meituan.adapter.TextAdapter.OnItemClickListener; import com.example.meituandemo2.R; import android.content.Context; import android.graphics.Region; import android.util.AttributeSet; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.widget.LinearLayout; import android.widget.ListView; /** * 是弹出里面嵌套的类,实现不同的列表菜单。 * @author Administrator * */ public class ViewMiddle extends LinearLayout implements ViewBaseAction { private ListView regionListView; private ListView plateListView; private ArrayList<String> groups = new ArrayList<String>(); private LinkedList<String> childrenItem = new LinkedList<String>(); private SparseArray<LinkedList<String>> children = new SparseArray<LinkedList<String>>(); private TextAdapter plateListViewAdapter; private TextAdapter earaListViewAdapter; private OnSelectListener mOnSelectListener; private int tEaraPosition = 0; private int tBlockPosition = 0; private String showString = "不限"; public ViewMiddle(Context context) { super(context); init(context); } public ViewMiddle(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public void updateShowText(String showArea, String showBlock) { if (showArea == null || showBlock == null) { return; } for (int i = 0; i < groups.size(); i++) { if (groups.get(i).equals(showArea)) { earaListViewAdapter.setSelectedPosition(i); childrenItem.clear(); if (i < children.size()) { childrenItem.addAll(children.get(i)); } tEaraPosition = i; break; } } for (int j = 0; j < childrenItem.size(); j++) { if (childrenItem.get(j).replace("不限", "").equals(showBlock.trim())) { plateListViewAdapter.setSelectedPosition(j); tBlockPosition = j; break; } } setDefaultSelect(); } private void init(Context context) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.view_region, this, true); regionListView = (ListView) findViewById(R.id.listView); plateListView = (ListView) findViewById(R.id.listView2); setBackgroundDrawable(getResources().getDrawable( R.drawable.choosearea_bg_mid)); for(int i=0;i<10;i++){ groups.add(i+"行"); LinkedList<String> tItem = new LinkedList<String>(); for(int j=0;j<15;j++){ tItem.add(i+"行"+j+"列"); } children.put(i, tItem); } earaListViewAdapter = new TextAdapter(context, groups, R.drawable.choose_item_selected, R.drawable.choose_eara_item_selector); earaListViewAdapter.setTextSize(17); earaListViewAdapter.setSelectedPositionNoNotify(tEaraPosition); regionListView.setAdapter(earaListViewAdapter); earaListViewAdapter .setOnItemClickListener(new TextAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position) { if (position < children.size()) { childrenItem.clear(); childrenItem.addAll(children.get(position)); plateListViewAdapter.notifyDataSetChanged(); } } }); if (tEaraPosition < children.size()) childrenItem.addAll(children.get(tEaraPosition)); plateListViewAdapter = new TextAdapter(context, childrenItem, R.drawable.choose_item_right, R.drawable.choose_plate_item_selector); plateListViewAdapter.setTextSize(15); plateListViewAdapter.setSelectedPositionNoNotify(tBlockPosition); plateListView.setAdapter(plateListViewAdapter); plateListViewAdapter .setOnItemClickListener(new TextAdapter.OnItemClickListener() { @Override public void onItemClick(View view, final int position) { showString = childrenItem.get(position); if (mOnSelectListener != null) { mOnSelectListener.getValue(showString); } } }); if (tBlockPosition < childrenItem.size()) showString = childrenItem.get(tBlockPosition); if (showString.contains("不限")) { showString = showString.replace("不限", ""); } setDefaultSelect(); } public void setDefaultSelect() { regionListView.setSelection(tEaraPosition); plateListView.setSelection(tBlockPosition); } public String getShowText() { return showString; } public void setOnSelectListener(OnSelectListener onSelectListener) { mOnSelectListener = onSelectListener; } public interface OnSelectListener { public void getValue(String showText); } @Override public void hide() { // TODO Auto-generated method stub } @Override public void show() { // TODO Auto-generated method stub } }
package com.example.meituan.view; import com.example.meituan.adapter.TextAdapter; import com.example.meituan.adapter.TextAdapter.OnItemClickListener; import com.example.meituandemo2.R; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.Toast; /** * 是弹出里面嵌套的类,实现不同的列表菜单。 * @author Administrator * */ public class ViewRight extends RelativeLayout implements ViewBaseAction{ private ListView mListView; private final String[] items = new String[] { "item1", "item2", "item3", "item4", "item5", "item6" };//显示字段 private final String[] itemsVaule = new String[] { "1", "2", "3", "4", "5", "6" };//隐藏id private OnSelectListener mOnSelectListener; private TextAdapter adapter; private String mDistance; private String showText = "item1"; private Context mContext; public String getShowText() { return showText; } public ViewRight(Context context) { super(context); init(context); } public ViewRight(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } public ViewRight(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { mContext = context; LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.view_distance, this, true); setBackgroundDrawable(getResources().getDrawable(R.drawable.choosearea_bg_right)); mListView = (ListView) findViewById(R.id.listView); adapter = new TextAdapter(context, items, R.drawable.choose_item_right, R.drawable.choose_eara_item_selector); adapter.setTextSize(17); if (mDistance != null) { for (int i = 0; i < itemsVaule.length; i++) { if (itemsVaule[i].equals(mDistance)) { adapter.setSelectedPositionNoNotify(i); showText = items[i]; break; } } } mListView.setAdapter(adapter); adapter.setOnItemClickListener(new TextAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position) { if (mOnSelectListener != null) { showText = items[position]; mOnSelectListener.getValue(itemsVaule[position], items[position]); } } }); } public void setOnSelectListener(OnSelectListener onSelectListener) { mOnSelectListener = onSelectListener; } public interface OnSelectListener { public void getValue(String distance, String showText); } @Override public void hide() { } @Override public void show() { } }
package com.example.meituandemo; import java.util.ArrayList; import com.example.meituan.view.ExpandTabView; import com.example.meituan.view.ViewLeft; import com.example.meituan.view.ViewMiddle; import com.example.meituan.view.ViewRight; import com.example.meituandemo2.R; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Toast; /** * 主活动页面。 * * @author Administrator * */ public class MainActivity extends Activity { private ExpandTabView expandTabView; private ViewLeft viewLeft; private ViewMiddle viewMiddle; private ViewRight viewRight; private ArrayList<View> mViewArray = new ArrayList<View>(); private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initVaule(); initListener(); } private void initListener() { viewLeft.setOnSelectListener(new ViewLeft.OnSelectListener() { @Override public void getValue(String distance, String showText) { onRefresh(viewLeft, showText); } }); viewMiddle.setOnSelectListener(new ViewMiddle.OnSelectListener() { @Override public void getValue(String showText) { onRefresh(viewMiddle, showText); } }); viewRight.setOnSelectListener(new ViewRight.OnSelectListener() { @Override public void getValue(String distance, String showText) { onRefresh(viewRight, showText); } }); } private void onRefresh(View view, String showText) { expandTabView.onPressBack(); int position = getPositon(view); if (position >= 0 && !expandTabView.getTitle(position).equals(showText)) { expandTabView.setTitle(showText, position); } Toast.makeText(MainActivity.this, showText, Toast.LENGTH_SHORT).show(); } private int getPositon(View tView) { for (int i = 0; i < mViewArray.size(); i++) { if (mViewArray.get(i) == tView) { return i; } } return -1; } @Override public void onBackPressed() { if (!expandTabView.onPressBack()) { finish(); } } private void initVaule() { Log.d(TAG, "initValue"); mViewArray.add(viewLeft); mViewArray.add(viewMiddle); mViewArray.add(viewRight); ArrayList<String> mTextArray = new ArrayList<String>(); mTextArray.add("全部分类"); mTextArray.add("全城"); mTextArray.add("只能排序"); expandTabView.setValue(mTextArray, mViewArray);// 将三个下拉列表设置进去 // expandTabView.setTitle(viewLeft.getShowText(), 0); // expandTabView.setTitle(viewMiddle.getShowText(), 1); // expandTabView.setTitle(viewRight.getShowText(), 2); } private void initView() { expandTabView = (ExpandTabView) findViewById(R.id.expandtab_view); viewLeft = new ViewLeft(this); viewMiddle = new ViewMiddle(this); viewRight = new ViewRight(this); } }
package com.example.meituan.adapter; import java.util.List; import com.example.meituandemo2.R; import android.content.Context; import android.graphics.drawable.Drawable; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; public class TextAdapter extends ArrayAdapter<String> { private Context mContext; private List<String> mListData; private String[] mArrayData; private int selectedPos = -1; private String selectedText = ""; private int normalDrawbleId; private Drawable selectedDrawble; private float textSize; private OnClickListener onClickListener; private OnItemClickListener mOnItemClickListener; public TextAdapter(Context context, List<String> listData, int sId, int nId) { super(context, R.string.no_data, listData); mContext = context; mListData = listData; selectedDrawble = mContext.getResources().getDrawable(sId); normalDrawbleId = nId; init(); } private void init() { onClickListener = new OnClickListener() { @Override public void onClick(View view) { selectedPos = (Integer) view.getTag(); setSelectedPosition(selectedPos); if (mOnItemClickListener != null) { mOnItemClickListener.onItemClick(view, selectedPos); } } }; } public TextAdapter(Context context, String[] arrayData, int sId, int nId) { super(context, R.string.no_data, arrayData); mContext = context; mArrayData = arrayData; selectedDrawble = mContext.getResources().getDrawable(sId); normalDrawbleId = nId; init(); } /** * 设置选中的position,并通知列表刷新 */ public void setSelectedPosition(int pos) { if (mListData != null && pos < mListData.size()) { selectedPos = pos; selectedText = mListData.get(pos); notifyDataSetChanged(); } else if (mArrayData != null && pos < mArrayData.length) { selectedPos = pos; selectedText = mArrayData[pos]; notifyDataSetChanged(); } } /** * 设置选中的position,但不通知刷新 */ public void setSelectedPositionNoNotify(int pos) { selectedPos = pos; if (mListData != null && pos < mListData.size()) { selectedText = mListData.get(pos); } else if (mArrayData != null && pos < mArrayData.length) { selectedText = mArrayData[pos]; } } /** * 获取选中的position */ public int getSelectedPosition() { if (mArrayData != null && selectedPos < mArrayData.length) { return selectedPos; } if (mListData != null && selectedPos < mListData.size()) { return selectedPos; } return -1; } /** * 设置列表字体大小 */ public void setTextSize(float tSize) { textSize = tSize; } @Override
以上是关于仿美团二级菜单的主要内容,如果未能解决你的问题,请参考以下文章
Android 实现仿美团首页左右滑动GridView+ViewPager菜单栏的功能