gpt4 book ai didi

android - 动态隐藏 fragment (Viewpager 和 fragment )

转载 作者:行者123 更新时间:2023-11-29 18:01:29 24 4
gpt4 key购买 nike

我正在使用 viewpager 并滚动浏览 fragment 。我想隐藏有关偏好更改的 fragment

package app.converto;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import app.converto.settings.SettingsActivity;
import app.converto.ui.LengthFragment;
import app.converto.ui.TemperatureFragment;

public class ConverTo extends FragmentActivity /*implements OnClickListener*/ implements OnSharedPreferenceChangeListener {

SectionsPagerAdapter mSectionsPagerAdapter; //declaring pageradapter class
ViewPager mViewPager; // declaring viewpager class


private TemperatureFragment fragment2;
private LengthFragment fragment1;
private static final int RESULT_SETTINGS = 1;

@Override
public void onCreate(Bundle savedInstanceState) { //methods to call on app launch
super.onCreate(savedInstanceState);

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
setContentView(R.layout.converto_home);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
sharedPref.registerOnSharedPreferenceChangeListener(this);
//boolean prefTemperature = sharedPref.getBoolean("prefTemperature", true);



}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {

case R.id.menu_settings:
Intent i = new Intent(this, SettingsActivity.class);
startActivityForResult(i, RESULT_SETTINGS);
break;

}

return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {
case RESULT_SETTINGS:
showUserSettings();
break;

}

}


private void showUserSettings() {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(this);

StringBuilder builder = new StringBuilder();

builder.append(
sharedPrefs.getBoolean("pref_temperature", true));
builder.append(
sharedPrefs.getBoolean("pref_length", false));

builder.append(
sharedPrefs.getBoolean("pref_area", false));



// TextView settingsTextView = (TextView) findViewById(R.id.textUserSettings);

// settingsTextView.setText(builder.toString());
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {




public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int i) {
switch(i){
case 0:
fragment2 = new TemperatureFragment(ConverTo.this);

return fragment2;

case 1:
fragment1 = new LengthFragment(ConverTo.this);
return fragment1;
}
return null;
}

@Override
public int getCount() {
return 2;
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return getString(R.string.heading_temperature);
case 1: return getString(R.string.heading_length);
}
return null;
}
}


public void onSharedPreferenceChanged(SharedPreferences arg0, String key) {
if (key.equals("prefTemperature")) {
Toast.makeText(this, "here!", Toast.LENGTH_LONG).show();

FragmentManager fM = getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
fT.hide(fragment1);
fT.commit();
}
}

}

这是我得到的错误 04-07 21:30:14.313: E/AndroidRuntime(15363): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

这是我的temperatureFragment代码

package app.converto.ui;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import app.converto.R;
import app.converto.utils.Temperature;

@SuppressLint("ValidFragment")
public class TemperatureFragment extends Fragment implements OnClickListener, OnItemSelectedListener {

EditText rInputTemp;
public static String rSelectedUnit;
Spinner spinner;
List<String> rValues = new ArrayList<String>();
Temperature rTemperature;
Context rContext;
ListView rResultsList;

public TemperatureFragment(Context context) {
rContext = context;

}
public TemperatureFragment(){

}

public Context getContext(){
return rContext;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.view_temperature, container, false);
rInputTemp = (EditText) view.findViewById(R.id.inputTemp);



rInputTemp.setText("1");

rInputTemp.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
if(s.toString().equals("")){

}
else{


rTemperature = new Temperature(spinner.getSelectedItemPosition(), Double.parseDouble(s.toString()));
setDisplayText(rTemperature.getValues());
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});

rResultsList = (ListView) view.findViewById(R.id.results);
spinner = (Spinner) view.findViewById(R.id.temp_units);


spinner.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(), R.array.units_temperature,R.layout.spinner_layout);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
adapter.setDropDownViewResource(R.layout.spinner_layout);

return view;
}// end oncreate

public void onClick(View v) {



}
public void onItemSelected(AdapterView<?> p, View arg1, int pos,
long id) {
rSelectedUnit = p.getItemAtPosition(pos).toString();

rTemperature = new Temperature(spinner.getSelectedItemPosition(), Double.parseDouble(rInputTemp.getText().toString()));
setDisplayText(rTemperature.getValues());

}//spinneronItemSelected

public void onNothingSelected(AdapterView<?> p) {

}//spinner onNothingSelected

double roundfourDecimals(double d) {
DecimalFormat fourDForm = new DecimalFormat("#.####");
return Double.valueOf(fourDForm.format(d));
}



private void setDisplayText(List<String> values){


ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(rContext, R.layout.result,R.id.result,values);
rResultsList.setAdapter(adapter2);


}


}//end fragment 2

最佳答案

将我的评论移至答案:

您必须将 fT.commit() 更改为 fT.commitAllowingStateLoss(),因为当 Activity 运行时,此代码可能会运行在后台,在这种情况下,您不能 commit()

关于android - 动态隐藏 fragment (Viewpager 和 fragment ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15870336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com