Fragment上下文菜单示例

本示例学习使用Fragment上下文件菜单,通过改造官网源码而写的更好学习的示例
难度系数30%.

本文中所讲的技术项目源码下载地址:http://download.csdn.net/detail/far_sight/7930799

项目结构

:

activity_main.xml内容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="#ffff0000"
        android:padding="@dimen/padding_medium"
        android:text="下面是一个带了菜单的Fragment"
        tools:context=".MainActivity" />

    <LinearLayout
        android:id="@+id/myLinearlayout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>


fragment_context_menu.xml文件的内容为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">
    <Button android:id="@+id/long_press"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="长按我,可以显示一个菜单">
        <requestFocus />
    </Button>
</LinearLayout>

FragmentContextMenu.java文件内容:

/***
 * 本示例学习使用Fragment上下文件菜单,通过改造官网源码而写的更好学习的示例
 * 难度系数30%
 */
package com.fs.act;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
public class FragmentContextMenu extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
        ContextMenuFragment fragment = new ContextMenuFragment();
        this.getFragmentManager().beginTransaction().add(R.id.myLinearlayout, fragment).commit();
    }

    public class ContextMenuFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View root = inflater.inflate(R.layout.fragment_context_menu, container, false);
            //fragment_context_menu.xml中的按钮注册长按事件,该事件发生后会触发Fragment中的
            //函数onCreateContextMenu被回调用
            this.registerForContextMenu(root.findViewById(R.id.long_press));
            return root;
        }
        //出现菜单的回调函数
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
            super.onCreateContextMenu(menu, v, menuInfo);
            menu.add(Menu.NONE, 111, Menu.NONE, "Menu A");
            menu.add(Menu.NONE, 222, Menu.NONE, "Menu B");
        }
        //监听菜单项点击事件的回调用函数
        @Override
        public boolean onContextItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case 111:
                    Log.i("ContextMenu", "Item 1a was chosen");
                    return true;
                case 222:
                    Log.i("ContextMenu", "Item 1b was chosen");
                    return true;
            }
            return super.onContextItemSelected(item);
        }
    }
}

运行结果为:



 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值