(android)通过滑动菜单操作其他活动(listfragment)

时间:2013-10-01 17:13:26

标签: android android-fragments slidingmenu

通过使用滑动菜单库(列表片段)https://github.com/jfeinstein10/SlidingMenu,我如何通过它操纵其他活动?假设我想在主要活动中创建一个按钮,使其不可见button1.setVisibility(View.GONE);我应该在滑动菜单(列表片段)中做些什么来实现呢?到目前为止我只知道如何敬酒......

1 个答案:

答案 0 :(得分:0)

一种方法是从菜单发送广播:

Intent intent = new Intent();
intent.setAction("com.example.Broadcast");
intent.putExtra("visibility", View.GONE);
sendBroadcast(intent);

并在片段或活动的意图中处理它:

package com.example.BroadcastDetector;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Implement code here to be performed when 
        // broadcast is detected
    }
}

查看BroadcastReceiver示例的this site

但是,在实施SlindingMenu时,您通常会将菜单和内容片段放在同一个活动中。这样您就可以简单地引用当前显示的片段:

public class StartActivity extends SlidingFragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...

        Fragment contentFragment = InitialFragment()

        getSupportFragmentManager().beginTransaction()
           .replace(R.id.content_frame, contentFragment).commit();

        getSupportFragmentManager().beginTransaction()
           .replace(R.id.menu_frame, new MenuFragment()).commit();

        ...
    }

    public void switchContent(final Fragment fragment) {
        // replace fragment in content frame or perform changes on it
        ...
    }

有关详细信息,请查看this post