Android SDK Sample

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();
}

demo:https://gitee.com/AndroidUI/ListPopupMenu01.git

List of Sample Apps The list below provides a summary of the sample applications that are available with the Android SDK. Using the links on this page, you can view the source files of the sample applications in your browser. You can also download the source of these samples into your SDK, then modify and reuse it as you need. For more information, see Getting the Samples. API Demos A variety of small applications that demonstrate an extensive collection of framework topics. Backup and Restore A simple example that illustrates a few different ways for an application to implement support for the Android data backup and restore mechanism. Bluetooth Chat An application for two-way text messaging over Bluetooth. BusinessCard An application that demonstrates how to launch the built-in contact picker from within an activity. This sample also uses reflection to ensure that the correct version of the contacts API is used, depending on which API level the application is running under. Contact Manager An application that demonstrates how to query the system contacts provider using the ContactsContract API, as well as insert contacts into a specific account. Home A home screen replacement application. JetBoy A game that demonstrates the SONiVOX JET interactive music technology, with JetPlayer. Live Wallpaper An application that demonstrates how to create a live wallpaper and bundle it in an application that users can install on their devices. Lunar Lander A classic Lunar Lander game. Multiple Resolutions A sample application that shows how to use resource directory qualifiers to provide different resources for different screen configurations. Note Pad An application for saving notes. Similar (but not identical) to the Notepad tutorial. SampleSyncAdapter Demonstrates how an application can communicate with a cloud-based service and synchronize its data with data stored locally in a content provider. The sample uses two related parts of the Android framework — the account manager and the synchronization manager (through a sync adapter). Searchable Dictionary A sample application that demonstrates Android's search framework, including how to provide search suggestions for Quick Search Box. Snake An implementation of the classic game "Snake." Soft Keyboard An example of writing an input method for a software keyboard. Spinner A simple application that serves as an application-under-test for the SpinnerTest sample application. SpinnerTest An example test application that contains test cases run against the Spinner sample application. To learn more about the application and how to run it, please read the Activity Testing tutorial. TicTacToeLib An example of an Android library project that provides a game-play Activity to any dependent application project. For an example of how an application can use the code and resources in an Android library project, see the TicTacToeMain sample application. TicTacToeMain An example of an Android application that makes use of code and resources provided in an Android library project. Specifically, this application uses code and resources provided in the TicTacToeLib library project. Wiktionary An example of creating interactive widgets for display on the Android home screen. Wiktionary (Simplified) A simple Android home screen widgets example.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值