BorderlessButtons
sample:https://developer.android.google.cn/samples/BorderlessButtons/index.html
Demo:http://git.oschina.net/Android_Sample/BorderlessButton01
这个sample有2个重点,
第一:无边界Button:与普通Button的区别就是没有灰色的背景色,
第二:divider:可以设置布局中每个子view的分割线
BorderlessButtons
使用style,style="?android:buttonBarButtonStyle"
<Button
style="?android:buttonBarButtonStyle"
android:id="@+id/ok_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/ok" />
除了Button添加style之外,layoutstyle="?android:buttonBarStyle"
(也可以不加)
divider
我们都知道ListView有divider,其实只要是布局中子布局或view之间都是有divider的,
xml属性 | 说明 |
---|---|
android:divider= | 定义divider |
android:showDividers= | divider在layout的显示位置divider:beginneng/middle/end/none |
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:divider="?android:dividerVertical"
android:dividerPadding="10dp"
android:showDividers="middle">
自定义divider
android:divider="@drawable/divider"
divider.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size
android:width="20dp"
android:height="2dp"/>
<solid android:color="@color/colorAccent"/>
</shape>
ActionBarCompat-ListPopupMenu
给ListView
的item中的view添加PopupMenu
,点击删除该item。
Android Sample:https://developer.android.google.cn/samples/ActionBarCompat-ListPopupMenu/index.html
Demo: http://git.oschina.net/AndroidUI/ActionBarCompat-ListPopupMenu01
效果图
PopupMenu的创建
主要是PopupMenu
是如何创建的
final PopupAdapter adapter = (PopupAdapter) listView.getAdapter();
final String text = (String) view.getTag();
PopupMenu menu = new PopupMenu(getActivity(), view);
menu.getMenuInflater().inflate(R.menu.menu_popup, menu.getMenu());
menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu1:
adapter.remove(text);
break;
}
return true;//true、false都可以
}
});
menu.show();
Adapter
public class PopupAdapter extends ArrayAdapter<String> {
public PopupAdapter(ArrayList<String> list) {
super(getActivity(), R.layout.item_list_fragment, R.id.tv1, list);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
ImageView iv_delete = (ImageView) view.findViewById(R.id.iv_delete);
iv_delete.setTag(getItem(position));
iv_delete.setOnClickListener(PopupFragment2.this);
return view;
}
}
一般的adapter
上面demo中Adapter放在fragment/activity中,iv_delete.setOnClickListener(PopupFragment2.this);
,但是如果Adapter不放在fragment/activity中,就无法使用iv_delete.setOnClickListener(PopupFragment2.this);
,这就需要回调处理。
adapter
public class MyAdapter extends BaseAdapter {
private List<String> list;
public MyAdapter(List<String> list) {
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
holder.tv = (TextView) convertView.findViewById(R.id.tv);
holder.iv = (ImageView) convertView.findViewById(R.id.iv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(list.get(position));
if (onDeleteClickListener != null) {
holder.iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onDeleteClickListener.onItemClick(v, position);
}
});
}
return convertView;
}
static class ViewHolder {
TextView tv;
ImageView iv;
}
interface OnDeleteClickListener {
void onItemClick(View view, int position);
}
private OnDeleteClickListener onDeleteClickListener;
public void setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener) {
this.onDeleteClickListener = onDeleteClickListener;
}
}
MainActivity中的处理:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
adapter.setOnDeleteClickListener(new MyAdapter.OnDeleteClickListener() {
@Override
public void onItemClick(View view, int position) {
onClick(view, position);
}
});
}
private void onClick(final View view, final int position) {
view.post(new Runnable() {
@Override
public void run() {
showPopuMenu(view, position);
}
});
}
private void showPopuMenu(final View v, final int position) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, v);
popupMenu.getMenuInflater().inflate(R.menu.popup, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_remove:
list.remove(position);
adapter.notifyDataSetChanged();
return true;
}
return false;
}
});
popupMenu.show();
}