高仿iOS长按弹出菜单
2017-09-07 本文已影响432人
f1abf4d2779c
项目源码地址 MenuPopupView
效果图
sample.gif使用说明
Gradle
// project build.gradle
allprojects {
repositories {
jcenter()
maven {
url 'https://dl.bintray.com/shamschu/maven'
}
}
}
compile 'com.sc.framework.component:menupopupview:1.0.2'
1. 自定义MenuPopupAdapter
public class TestPopupAdapter extends MenuPopupAdapter<String> {
private List<String> mData;
public TestPopupAdapter(List<String> data) {
mData = data;
}
// 返回Item View视图对象
@Override
public View getView(View container, int position) {
View view = LayoutInflater.from(container.getContext()).inflate(R.layout.test_item, null);
TextView tv = (TextView) view.findViewById(R.id.label);
tv.setText(getItem(position));
return view;
}
/**
* 获取item数
*
* @return item数量
*/
@Override
public int getItemCount() {
return mData.size();
}
/**
* 获取position位置的数据对象
*
* @param position 位置索引
* @return 数据对象
*/
@Override
public String getItem(int position) {
return mData.get(position);
}
}
2. 创建PopupView并指定Adapter
List<String> menus = new ArrayList<>();
menus.add("default");
menus.add("copy");
MenuPopupAdapter<String> adapter = new TestPopupAdapter(menus);
PopupView popupView = new PopupView(this);
popupView.setAdapter(adapter);
3. 设置依附anchor方向并显示
// 显示在anchor底部
popupView.setPopupLocation(PopupLayout.PopupLocation.Bottom);
// 显示在anchor顶部
popupView.setPopupLocation(PopupLayout.PopupLocation.TOP);
// 显示PopupView
popupView.show(v);
4. 动态改变菜单并刷新
adapter.notifyDataSetChanged();
5. 设置分割线颜色
// 设置item的分割线颜色
popupView.setDriverColorResId(int colorResId);
// 设置左右指示器的分割线颜色
popupView.setIndicatorDriverColorResId(int colorResId);