我有一个包含四个标签的活动,这些标签由一个片段填充。在那个片段中我有四个开关。在运行时期间,我通过取消注册OncheckedChangeListener,然后更改其状态来以编程方式更改其状态,但我没有得到如何重新注册侦听器
这是代码
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
public static Fragment halFragment,bed1Fragment,bed2Fragment,kitFragment;
public static boolean hal_light1_flag=false,hal_light2_flag=false,hal_socket_flag=false,hal_fan_flag=false;
public static int hal_fanSpeed=2;
public static String location = "Hall";
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
halFragment = new hallFragment();
bed1Fragment = new hallFragment();
bed2Fragment = new hallFragment();
kitFragment = new kitchenFragment();
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
connection.tcpClient.setDestination("hal");
runThread();
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int j = tab.getPosition();
switch(j){
case 0:
Log.d("tabs","Location hall");
location = "Hall";
connection.tcpClient.setDestination("hal");
break;
case 1:
Log.d("tabs","Location bedroom1");
location = "Bedroom1";
connection.tcpClient.setDestination("be1");
break;
case 2:
Log.d("tabs","Location bedroom2");
location = "Bedroom2";
connection.tcpClient.setDestination("be2");
break;
case 3:
Log.d("tabs","Location kitchen");
location = "Kitchen";
connection.tcpClient.setDestination("kit");
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(halFragment, "HALL");
adapter.addFragment(bed1Fragment, "ROOM1");
adapter.addFragment(bed2Fragment, "ROOM2");
adapter.addFragment(kitFragment, "KITCHEN");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
private void runThread() {
i = 0;
new Thread() {
public void run() {
while (i++ < 1000) {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(connection.tcpClient.status_flag) {
Log.d("status", "got Status");
connection.tcpClient.status_flag = false;
updateUI();
}
}
});
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
public void updateUI(){
((Switch)halFragment.getView().findViewById(R.id.lightSwitch)).setOnCheckedChangeListener(null);
((Switch)halFragment.getView().findViewById(R.id.lightSwitch)).setChecked(MainActivity.hal_light1_flag);
((Switch)halFragment.getView().findViewById(R.id.lightSwitch)).setOnCheckedChangeListener(/*what should be inserted here*/);
这里是片段代码
public class hallFragment extends Fragment implements CompoundButton.OnCheckedChangeListener, SeekBar.OnSeekBarChangeListener{
public static Switch light,cfl,socket,fan;
public SeekBar speed;
View view;
public static CompoundButton.OnCheckedChangeListener lightListener,cflListener,socketListener,fanListener;
SeekBar.OnSeekBarChangeListener speedListener;
int k;
public hallFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_hall, container, false);
light = (Switch) view.findViewById(R.id.lightSwitch);
cfl = (Switch) view.findViewById(R.id.cflSwitch);
socket = (Switch) view.findViewById(R.id.socketSwitch);
fan = (Switch) view.findViewById(R.id.fanSwitch);
speed = (SeekBar) view.findViewById(R.id.speedSeekbar);
lightListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
if(bChecked){
Log.d("button",MainActivity.location +" light is on");
connection.tcpClient.sendMessage("li1_1");
}
else{
Log.d("Button",MainActivity.location +" light is off");
connection.tcpClient.sendMessage("li1_0");
speed.setProgress(0);
}
}
};
cflListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
if(bChecked){
Log.d("button",MainActivity.location + " cfl is on");
connection.tcpClient.sendMessage("li2_1");
}
else{
Log.d("Button",MainActivity.location + " cfl is off");
connection.tcpClient.sendMessage("li2_0");
}
}
};
socketListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
if(bChecked){
Log.d("button",MainActivity.location + " socket is on");
connection.tcpClient.sendMessage("soc_1");
}
else{
Log.d("Button",MainActivity.location + " socket is off");
connection.tcpClient.sendMessage("soc_0");
}
}
};
fanListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
if(bChecked){
Log.d("button",MainActivity.location + " fan is on");
connection.tcpClient.sendMessage("fan_1");
}
else{
Log.d("Button",MainActivity.location + " fan is off");
connection.tcpClient.sendMessage("fan_0");
}
}
};
speedListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
k = i;
Log.d("SeekBar","Progress Changed");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.d("SeekBar","started Touching");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.d("SeekBar","Speed changed to " + Integer.toString(k));
connection.tcpClient.sendMessage("fs_"+Integer.toString(k));
}
};
light.setOnCheckedChangeListener(lightListener);
cfl.setOnCheckedChangeListener(cflListener);
socket.setOnCheckedChangeListener(socketListener);
fan.setOnCheckedChangeListener(fanListener);
speed.setOnSeekBarChangeListener(speedListener);
return view;
}
}
答案 0 :(得分:0)
((Switch)halFragment.getView().findViewById(R.id.lightSwitch))
.setOnCheckedChangeListener(halFragment.lightListener);