无法从外部更改我片段的变量值 class
Unable to change the value of a variable of my fragment from an external class
我正在使用 fragments
但我无法从外部 class.
访问它的变量
目前,我有一个片段 fragmentView
,它有一个设置 button
。每当按下它时,它都会显示一个 UI 元素来定义不同的设置。我复制我拥有的代码:
片段
public static Boolean show = false;
private void initSettingsPanel() {
m_settingsBtn = (ImageButton) m_activity.findViewById(R.id.settingButton);
/* click settings panel button to open or close setting panel. */
m_settingsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
m_settingsLayout = (LinearLayout) m_activity.findViewById(R.id.settingsPanelLayout);
if (m_settingsLayout.getVisibility() == View.GONE) {
m_settingsLayout.setVisibility(View.VISIBLE);
if (m_settingsPanel == null) {
m_settingsPanel = new SettingsPanel(m_activity, show); //HERE I CALL THE EXTERNAL CLASS
}
} else {
m_settingsLayout.setVisibility(View.GONE);
}
}
});
}
设置面板
private Activity m_activity;
private static Boolean p_show;
private Switch p_switch;
public SettingsPanel(Activity activity, Boolean show
) {
p_show = show;
m_activity = activity;
initUIElements(); // Init switch
}
private void initUIElements() {
p_switch = (Switch) m_activity.findViewById(R.id.showSwitch);
setUIListeners();
}
private void setUIListeners() {
p_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
p_show = isChecked;
}
});
}
}
当前发生的情况是,当我激活开关时,我更改了 Pannel
的变量值,但它不会影响片段。为什么?有没有其他方法可以更改其变量的值而不发送给 SettingPanel
每个变量?这至少是一种正确的方法吗?
最后,我创建了一个 abstract class
以便 get
和 set
变量:
ShowVar
public abstract class ShowVar {
static private Boolean show = false;
public Boolean getShow() {
return show;
}
public void setShow(Boolean value) {
this.show = value;
}
}
从我的 SettingPanel
我有实例 ShowVar
每次设置变量的新值并更改 switch
设置面板
public class SettingsPanel {
public ShowVar showVar = new ShowVar() {
@Override
public void set_Show(Boolean show) {
super.setShow(show);
}
};
}
并且从我的片段中,我可以使用我的变量 m_settingsPanel
访问这些值
片段
m_settingsPanel.showVar.getShow()
您可以在您的 activity 和您的片段之间注册一个接口 -
public interface CallbackInterface {
//you can also return values from this interface
void performSomething(); //Add params to this method as per yourneed
}
在你的activityclass-
public YourActity extends AppCompactActivity implements CallbackInterface {
//your interface instance
private CallbackInterface mCallbackInterface;
@Override
void performSomething() {
// Write your code here.
}
}
}
在您的片段中,您可以覆盖
onAttach()
注册到接口--
@Override
public void onAttach(Context context) {
super.onAttach(context);
mCallbackInterface = (CallbackInterface) context;
}
瞧!现在您可以使用此接口将任何数据发送回您的 activity。
我正在使用 fragments
但我无法从外部 class.
目前,我有一个片段 fragmentView
,它有一个设置 button
。每当按下它时,它都会显示一个 UI 元素来定义不同的设置。我复制我拥有的代码:
片段
public static Boolean show = false;
private void initSettingsPanel() {
m_settingsBtn = (ImageButton) m_activity.findViewById(R.id.settingButton);
/* click settings panel button to open or close setting panel. */
m_settingsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
m_settingsLayout = (LinearLayout) m_activity.findViewById(R.id.settingsPanelLayout);
if (m_settingsLayout.getVisibility() == View.GONE) {
m_settingsLayout.setVisibility(View.VISIBLE);
if (m_settingsPanel == null) {
m_settingsPanel = new SettingsPanel(m_activity, show); //HERE I CALL THE EXTERNAL CLASS
}
} else {
m_settingsLayout.setVisibility(View.GONE);
}
}
});
}
设置面板
private Activity m_activity;
private static Boolean p_show;
private Switch p_switch;
public SettingsPanel(Activity activity, Boolean show
) {
p_show = show;
m_activity = activity;
initUIElements(); // Init switch
}
private void initUIElements() {
p_switch = (Switch) m_activity.findViewById(R.id.showSwitch);
setUIListeners();
}
private void setUIListeners() {
p_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
p_show = isChecked;
}
});
}
}
当前发生的情况是,当我激活开关时,我更改了 Pannel
的变量值,但它不会影响片段。为什么?有没有其他方法可以更改其变量的值而不发送给 SettingPanel
每个变量?这至少是一种正确的方法吗?
最后,我创建了一个 abstract class
以便 get
和 set
变量:
ShowVar
public abstract class ShowVar {
static private Boolean show = false;
public Boolean getShow() {
return show;
}
public void setShow(Boolean value) {
this.show = value;
}
}
从我的 SettingPanel
我有实例 ShowVar
每次设置变量的新值并更改 switch
设置面板
public class SettingsPanel {
public ShowVar showVar = new ShowVar() {
@Override
public void set_Show(Boolean show) {
super.setShow(show);
}
};
}
并且从我的片段中,我可以使用我的变量 m_settingsPanel
片段
m_settingsPanel.showVar.getShow()
您可以在您的 activity 和您的片段之间注册一个接口 -
public interface CallbackInterface {
//you can also return values from this interface
void performSomething(); //Add params to this method as per yourneed
}
在你的activityclass-
public YourActity extends AppCompactActivity implements CallbackInterface {
//your interface instance
private CallbackInterface mCallbackInterface;
@Override
void performSomething() {
// Write your code here.
}
}
}
在您的片段中,您可以覆盖
onAttach()
注册到接口--
@Override
public void onAttach(Context context) {
super.onAttach(context);
mCallbackInterface = (CallbackInterface) context;
}
瞧!现在您可以使用此接口将任何数据发送回您的 activity。