第五单元

主页面按钮点击切换碎片

public class MainActivity extends AppCompatActivity {
    private RadioButton button1;
    private RadioButton button2;
    private RadioButton button3;
    private RadioButton button4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = (RadioButton) findViewById(R.id.button_1);
        button2 = (RadioButton) findViewById(R.id.button_2);
        button3 = (RadioButton) findViewById(R.id.button_3);
        button4 = (RadioButton) findViewById(R.id.button_4);

        final OneFragment oneFragment = new OneFragment();
        final TwoFragment twoFragment = new TwoFragment();
        final ThreeFragment threeFragment = new ThreeFragment();
        final FourFragment fourFragment = new FourFragment();

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.fl_main, oneFragment, "one");
        transaction.add(R.id.fl_main, twoFragment, "two");
        transaction.add(R.id.fl_main, threeFragment, "three");
        transaction.add(R.id.fl_main, fourFragment, "four");
        transaction.hide(twoFragment);
        transaction.hide(threeFragment);
        transaction.hide(fourFragment);
        transaction.commit();

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.show(oneFragment);
                transaction.hide(twoFragment);
                transaction.hide(threeFragment);
                transaction.hide(fourFragment);
                transaction.commit();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.hide(oneFragment);
                transaction.show(twoFragment);
                transaction.hide(threeFragment);
                transaction.hide(fourFragment);
                transaction.commit();
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.hide(oneFragment);
                transaction.hide(twoFragment);
                transaction.show(threeFragment);
                transaction.hide(fourFragment);
                transaction.commit();
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.hide(oneFragment);
                transaction.hide(twoFragment);
                transaction.hide(threeFragment);
                transaction.show(fourFragment);
                transaction.commit();
            }
        });

    }
}

按钮一

public class OneFragment extends Fragment {
    private Context context;

    private ListView listView;
    private List<String> list = new ArrayList();
    private ArrayAdapter adapter;

    public OneFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_one, container, false);
        context = inflate.getContext();
        listView = inflate.findViewById(R.id.listView);
        for (int i = 0; i < 30; i++) {
            list.add("你是猴子请来的逗比-->"+i);
        }

        adapter = new ArrayAdapter(context,android.R.layout.simple_list_item_1,list);

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                PopupMenu popupMenu = new PopupMenu(context, view);
                popupMenu.inflate(R.menu.layout_list_menu);
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()){
                            case R.id.remove:
                                list.remove(position);
                                adapter.notifyDataSetChanged();
                                break;
                        }
                        return false;
                    }
                });
                popupMenu.show();
            }
        });


        return inflate;
    }

}

按钮二

public class TwoFragment extends Fragment {

    private Context context;

    public TwoFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_two, container, false);
        context = inflate.getContext();
        Button button = inflate.findViewById(R.id.button_pop);
        registerForContextMenu(button);
        return inflate;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        getActivity().getMenuInflater().inflate(R.menu.layout_context_menu, menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.send:
                NotificationManager manager = (NotificationManager) getActivity().getSystemService(Service.NOTIFICATION_SERVICE);
                Notification.Builder builder = new Notification.Builder(context);
                builder.setSmallIcon(R.mipmap.ic_launcher);

                RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_message);
                remoteViews.setTextViewText(R.id.text_message,"发送成功");
                remoteViews.setImageViewResource(R.id.image_message,R.mipmap.ic_launcher);
                builder.setCustomContentView(remoteViews);

                Intent intent = new Intent(context, Main2Activity.class);
                PendingIntent activity = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                builder.setAutoCancel(true);

                builder.setDefaults(Notification.DEFAULT_ALL);
                builder.setPriority(Notification.PRIORITY_MAX);
                builder.setContentIntent(activity);
                manager.notify(1,builder.build());
                break;
        }
        return super.onContextItemSelected(item);
    }
}

按钮三

public class ThreeFragment extends Fragment {

    
    private Context context;

    public ThreeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        final View inflate = inflater.inflate(R.layout.fragment_three, container, false);
        context = inflate.getContext();
        Button btn = inflate.findViewById(R.id.window_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupWindow popupWindow = new PopupWindow(context);
                View inflate1 = LayoutInflater.from(context).inflate(R.layout.layout_window, null);
                popupWindow.setContentView(inflate1);
                popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

                popupWindow.setOutsideTouchable(true);

                WindowManager.LayoutParams attributes = getActivity().getWindow().getAttributes();
                attributes.alpha = 0.5f;
                getActivity().getWindow().setAttributes(attributes);

                popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        WindowManager.LayoutParams attributes = getActivity().getWindow().getAttributes();
                        attributes.alpha = 1f;
                        getActivity().getWindow().setAttributes(attributes);
                    }
                });

                View view = LayoutInflater.from(context).inflate(R.layout.fragment_three, null);
                popupWindow.showAtLocation(view, Gravity.CENTER,0,0);
            }
        });
        return inflate;
    }

}

按钮四

public class FourFragment extends Fragment {


    public FourFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_four, container, false);
        Button button = inflate.findViewById(R.id.but_change);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Button button1 = getActivity().findViewById(R.id.window_btn);
               button1.setText("好好学习");
            }
        });
        return inflate;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值