2016-03-30 6 views
0

2つのframelayoutsを使ってコンテンツをロードしようとしています。私の問題は、両方のページが同時にデータを表示していることです。私は、メインのJavaファイルでsetVisibiltyメソッドを使いたいと思います。 1つのフレームがデータを表示しているとき、もう1つのフレームは自動的に非表示になります。誰も私にJavaコードを教えてもらえますか?ここでxmlファイルは次のとおりです。アンドロイドで2つのframelayoutとsetvisibilyメソッドを使いたい

<FrameLayout 
android:id="@+id/content_frame" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
</FrameLayout> 

<FrameLayout 
android:id="@+id/content_frametwo" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
</FrameLayout> 

私はここにあなたのjavaファイルを与えている: -

@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); 
} 

private void setFrameVisibility(boolean frameOneVisible){ 
    if (frameOneVisible){ 
     findViewById(R.id.content_frame).setVisibility(View.VISIBLE); 
     findViewById(R.id.content_frametwo).setVisibility(View.GONE); 
    } else { 
     findViewById(R.id.content_frame).setVisibility(View.GONE); 
     findViewById(R.id.content_frametwo).setVisibility(View.VISIBLE); 
    } 
} 

@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 = getFragmentManager(); 

    if (id == R.id.homepage) { 
     Intent homepage = new Intent (MainActivity.this, MainActivity.class); 
     startActivity(homepage); 
         // Handle the camera action 
    } else if (id == R.id.foodpage) { 
     //handle the food page here 
     fragmentManager.beginTransaction() 
       .replace(R.id.content_frame 
         , new FirstFragment()) 
       .commit(); 

    } else if (id == R.id.schedulepage) { 
     fragmentManager.beginTransaction() 
       .replace(R.id.content_frame 
       , new ScheduleFragment()) 
       .commit(); 

    } else if (id == R.id.emotionspage) { 
     fragmentManager.beginTransaction() 
       .replace(R.id.content_frame 
       , new EmotionsFragment()) 
       .commit(); 

    } else if (id == R.id.basicneedspage) { 
     fragmentManager.beginTransaction() 
       .replace(R.id.content_frametwo 
       , new BasicneedsFragment()) 
       .commit(); 

    } else if (id == R.id.exit) { 
     askBeforeExit(); 

    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 

public void onBackPressed() { 

    askBeforeExit(); 
} 

private void askBeforeExit(){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setCancelable(false); 
    builder.setTitle("Confirm Exit"); 
    builder.setMessage("Are you sure you want to quit?"); 
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      finish(); 
     } 
    }); 
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
     } 
    }); 
    AlertDialog alert=builder.create(); 
    alert.show(); 
} 

}

+0

使用ViewSwitcher。 http://android-er.blogspot.in/2013/11/example-of-viewswitcher.html – Abdullah

答えて

0

あなたはこのコードを使用してこれを実装できます

onCreateView内:

frameLayout1 = (FrameLayout) findViewById(R.id.frameLayout1); 
    frameLayout2 = (FrameLayout) findViewById(R.id.frameLayout2); 

ときあなたは可視性を変更したい、これまで:

frameLayout1.setVisibility(View.VISIBLE); 
    frameLayout2.setVisibility(View.GONE); 
+0

は私のjavaメインファイルでこの作品ですか? –

+0

はい、あなたの活動の中。 – SacreDeveloper

+0

私のために私のコードを編集できますか?前もって感謝します。 –

1

あなたはそれらの両方を設定する機能を作ることができるが、その後、あなたは常にその機能を使用していることを確認する必要があります。

private void setFrameVisibility(boolean frameOneVisible) { 
    if (frameOneVisible) { 
     findViewById(R.id.content_frame).setVisibility(View.VISIBLE); 
     findViewById(R.id.content_frametwo).setVisibility(View.GONE); 
    } else { 
     findViewById(R.id.content_frame).setVisibility(View.GONE); 
     findViewById(R.id.content_frametwo).setVisibility(View.VISIBLE); 
    } 
} 
+0

メインファイルに同じコードを貼り付けました。それでも両方のページを表示しています。 –

+0

あなたの「メイン」ファイルはどのようなクラスですか?そして、あなたはいつこの機能を呼びますか? – 0xDEADC0DE

+0

以下の完全なコードを提供します: - –

関連する問題