My Appはナビゲーションドロワーと4つのフラグメントで構成されています。 このアプリの古いバージョンではアクティビティが使用されているため、そのアクティビティをフラグメントでトランスフォームする必要があります。今、すべてのためにAndroid force画面の向きが変更されたときにフラグメントの再描画が行われます
ユーザーがテキストと背景色を設定することができますし、DialogActivity
を呼び出す方法は、すべてのFragments
と管理のためのFragments
内部ダイアログで呼ばonActivityResults
を管理する唯一のMainActivity
であることをうまく動作しますが、1 Fragment
に私は80 Buttons
を持っていますユーザーの変更。
画面の向きが横向きに変更されると問題が発生します。 Buttons
を押してテキストと色を設定した場合はポートレートがですが、ランドスケープでの画面の向きを変更すると、バックグラウンドのような「影」のようなものが表示され、私が持っているボタンは、私は再び画面を回転させると、変更されたボタンが見えるようになりました。 奇妙なことに、フラグメントの背景には、正しい更新が付いたボタンが表示されますが、上には表示されません...(写真を投稿すると説明が難しい)
私が変更した古いボタンは、 ...私はDBに保存しますが、風景と、私はCHE他のボタンを更新することはできません
CODE:
MainActivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
static String clickedButtonViewId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
if (findViewById(R.id.content_frame) != null){
getSupportFragmentManager().beginTransaction()
.add(R.id.content_frame, new OrarioFragment()).commit();
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager fragmentManager = getSupportFragmentManager();
if (id == R.id.nav_orario) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new OrarioFragment())
.commit();
} else if (id == R.id.nav_calendario) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new CalendarioFragment())
.commit();
} else if (id == R.id.nav_voti) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new VotiFragment())
.commit();
} else if (id == R.id.nav_registrazioni) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new RegistrazioniFragment())
.commit();
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void addMateria(View v){
/* Prendo il nome della risorsa cosi nel ricompilare il progetto non perdo *
* tutti i riferimenti ai bottoni salvati nel database */
clickedButtonViewId = getResources().getResourceEntryName(v.getId());
//StartActivityForResult perche mi aspetto la materia inserita dall'altra activity
Intent myIntent = new Intent(MainActivity.this, ActivityAddMateria.class);
startActivityForResult(myIntent, 1);
//onStop();
}
//Take back data from ActivityAddMateria
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 1) {
if (resultCode == RESULT_OK) {
MySQLiteHelper db = new MySQLiteHelper(this);
//Cambio subito il Button
int resId = getResources().getIdentifier(clickedButtonViewId, "id", getPackageName());
final Button clickedtextView = (Button) findViewById(resId);
String result = data.getStringExtra("result"); //Take the materia from Dialog
int color = data.getIntExtra("color", 1); //Take the color from Dialog
//Controllo se il Button è già presente nel db se presente aggiorno se non presente inserisco
boolean modifica = db.Exists(clickedButtonViewId);
//Se voglio ripristinare il bottone di default
if (color == getResources().getColor(R.color.blue_orario)) {
//Ripristino la grafica di Default
Drawable style = setButtonColor(color);
clickedtextView.setBackground(style);
clickedtextView.setText("New");
//Se la materia è nel database la cancello
if (modifica) {
db.deleteSingleMateria(clickedButtonViewId);
}
} else {
//Quando inserisco un normale bottone colorato
if (!modifica) {
//Materia da inserire in un nuovo spazio
db.addMateriaToDb(new Materia(clickedButtonViewId, result, color));
} else {
//Materia già presente nel Button quindi aggiorno la materia
db.updateMateria(new Materia(clickedButtonViewId, result, color));
Toast.makeText(getApplicationContext(), "Materia modificata!",
Toast.LENGTH_LONG).show();
}
//Inserisco la materia nel DB dei voti_media
db.addMateriaVotiFromOrario(new MaterieVoti(result, 0.0));
clickedtextView.setText(result);
//clickedtextView.setBackgroundColor(color);
//clickedtextView.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
Drawable style = setButtonColor(color);
clickedtextView.setBackground(style);
}
}
if (resultCode == RESULT_CANCELED) {
//Nessuna materia inserita
}
}
}//onActivityResult
EDIT
[OK]を私は問題を発見しました。私は力のため
if (findViewById(R.id.content_frame) != null){
getSupportFragmentManager().beginTransaction()
.add(R.id.content_frame, new OrarioFragment()).commit();
}
そしてMainActivityが再作成された画面の向きの変更と古い断片上同じフラグメントをロードするために示されるべき最初のフラグメントをコード行を有するMainActivityで
私は.add()を使用します。
これで、この問題を回避し始めるときに、どのようにフラグメントを表示するように設定できますか?
引き出しの管理に間違っていますか?
コードを表示します。 – Bryan
@ブライアンはコードを追加しただけでもっと分かります。 – Dario
あなたのレイアウトも共有できますか? – JRG