从MainActivity访问片段元素和属性

时间:2015-05-07 19:15:40

标签: android android-fragments

我想根据网络服务信息更改片段中的按钮状态和来源,但是无法从MainActivity中获取片段的元素。

以下是示例片段代码:

   private final String deviceID = "Phone";


    WebSocketConnection mConnection = Connection.getInstance();

    ImageButton LigthBtn = null;

    Boolean LigthBtnState = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.room1fragment, container, false);
        LigthBtn  = (ImageButton)view.findViewById(R.id.lightBtn);
        LigthBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (LigthBtnState) {
                    JSONObject jsonObj =new JSONObject();
                    JSONObject jsonMessage = new JSONObject();
                    try {
                        jsonMessage.put("from",deviceID);
                        jsonMessage.put("message","LIGTH OFF");
                        jsonMessage.put("to","1");

                        jsonObj.put("Type","Message");
                        jsonObj.put("Data",jsonMessage);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    mConnection.sendTextMessage(jsonObj.toString());
                    LigthBtn.setImageResource(R.drawable.ligth_off);
                    LigthBtnState = false;
                }
                else {
                    JSONObject jsonObj =new JSONObject();
                    JSONObject jsonMessage = new JSONObject();
                    try {
                        jsonMessage.put("from",deviceID);
                        jsonMessage.put("message","LIGTH ON");
                        jsonMessage.put("to","1");

                        jsonObj.put("Type","Message");
                        jsonObj.put("Data",jsonMessage);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    mConnection.sendTextMessage(jsonObj.toString());
                    LigthBtn.setImageResource(R.drawable.ligth_on);
                    LigthBtnState = true;
                }

            }
        });





        return view;
    }

主要活动在这里:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    start();


    tabsviewPager = (ViewPager) findViewById(R.id.tabspager);

    mTabsAdapter = new Tabsadapter(getSupportFragmentManager());

    tabsviewPager.setAdapter(mTabsAdapter);

    getSupportActionBar().setHomeButtonEnabled(false);
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Tab room1Tab = getSupportActionBar().newTab().setText("Room1")
            .setTabListener(this);
    Tab room2Tab = getSupportActionBar().newTab().setText("Room2")
            .setTabListener(this);
    Tab room3Tab = getSupportActionBar().newTab().setText("Room3")
            .setTabListener(this);

    getSupportActionBar().addTab(room1Tab);
    getSupportActionBar().addTab(room2Tab);
    getSupportActionBar().addTab(room3Tab);

    // This helps in providing swiping effect for v7 compat library
    tabsviewPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // TODO Auto-generated method stub
            getSupportActionBar().setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    });
    if (!isMyServiceRunning()) {
        Intent serviceIntent = new Intent(
                "com.example.androidusersmarthome.NotificationService");
        serviceIntent.setPackage("com.example.androidusersmarthome");
        context.startService(serviceIntent);// 
    }
}

private boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager
            .getRunningServices(Integer.MAX_VALUE)) {
        if (NotificationService.class.getName().equals(
                service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

void displayMessage(String text) {
    Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();

}

@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab selectedtab, FragmentTransaction arg1) {
    // TODO Auto-generated method stub
    tabsviewPager.setCurrentItem(selectedtab.getPosition()); // update tab
                                                                // position
                                                                // on tap
}

@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}

我想更改例如LightBtnState和来自mainActivity的LightBtn图像按钮源。有任何方法可以做到这一点。谢谢。

0 个答案:

没有答案