2016-07-25 2 views
1

Ich habe 5 Relative Layouts in Android Studio-Projekt erstellt (das wird mehr als 40) mit gleicher Größe. Ich erstelle auch Schaltflächen, so dass wenn jemand auf Lektion 1 Schaltfläche als nur Layout1 (ID des ersten Layouts) Layout zeigt auf dem Bildschirm und anderen Layout automatisch ausblenden, wenn der Benutzer klickt auf Lektion 2 Button als Layout2 (ID des zweiten Layout) zeigt und ruht sich automatisch aus, und so weiter ...Verstecken Sie alle Relative Layout mit einer Funktion außer einer, die wir wählen

Kann jemand der Logik sagen, dass ich das verwenden kann, um dies zu tun. Und wenn es eine andere beste Weg ist, sagen diese pls zu tun .... Das ist mein Lessons.java Datei

public class Lessons extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    //Relative Layout's Variables Declaration here 
    RelativeLayout r[]; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_lessons); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

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

    } 

    @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.lessons, 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) { 
      Toast.makeText(Lessons.this, "Settings", Toast.LENGTH_SHORT).show(); 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 


     switch (id){ 
      case R.id.lesson_1: 
       hideRelativeLayout(R.id.layout1); 
       break; 
      case R.id.lesson_2: 
       hideRelativeLayout(R.id.layout2); 
       break; 
     /*Buttons onSelect Event upto 40*/ 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
    protected void hideRelativeLayout(int current){ 
     /*Code that Shows only current Layout and hide rest of all layouts*/ 

    } 
} 

und das ist mein activity_lessons.xml Datei

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:id="@+id/relLay" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.kaliattacks.evilandroid.hackinginhindi.Lessons" 
    tools:showIn="@layout/app_bar_lessons"> 


    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:id="@+id/layout1" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"> 
     <!--This is Layout 1--> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="This is Layer 1" 
      android:id="@+id/textView1" 
      android:layout_centerVertical="true" 
      android:layout_centerHorizontal="true" /> 
     </RelativeLayout> 
     <!--This is Second Layout--> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/layout2" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="This is another layer" 
       android:id="@+id/textView2" 
       android:layout_centerVertical="true" 
       android:layout_centerHorizontal="true" /> 

     </RelativeLayout> 
    <!--This is Third Layout--> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/layout3" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="This is Third Layout Text" 
      android:id="@+id/textView3" 
      android:layout_centerVertical="true" 
      android:layout_centerHorizontal="true" /> 

    </RelativeLayout> 
    <!--This is Fourth Layout--> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/layout4" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="This is Fourth Layout Text" 
      android:id="@+id/textView4" 
      android:layout_centerVertical="true" 
      android:layout_centerHorizontal="true" /> 

    </RelativeLayout> 
    <!--Fifth Layout--> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/layout5" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="This is Fifth Layout Text" 
      android:id="@+id/textView5" 
      android:layout_centerVertical="true" 
      android:layout_centerHorizontal="true" /> 

    </RelativeLayout> 
</RelativeLayout> 

Antwort

2

Sie sich alle RelativeLayout id in einem Integer ArrayList wie

setzen können
ArrayList<Integer> relativeLayoutIdList = new ArrayList<Integer>(); 

dann in onCreate()

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // add all relativelayout id to arraylist 
    relativeLayoutIdList.add(R.id.layout1); 
    relativeLayoutIdList.add(R.id.layout2); 
    ... 

} 

und in Ihrem hideRelativeLayout

protected void hideRelativeLayout(int current){ 
    for (int i = 0, len = relativeLayoutIdList.size(); i < len; i++){ 
     if(relativeLayoutIdList.get(i) == current){ 
      // visible current relativelayout 
      (RelativeLayout) findViewById(relativeLayoutIdList.get(i)).setVisibility(View.VISIBLE); 
     }else{ 
      // hide all rest relativelayout 
      (RelativeLayout) findViewById(relativeLayoutIdList.get(i)).setVisibility(View.GONE); 
     } 
    } 
} 

vorschlagen: Ich sehe Sie NavigationView verwenden, können Sie mutilple Fragment in NavigationView statt schaffen viele RelativeLayout https://guides.codepath.com/android/Fragment-Navigation-Drawer

+0

ich das erste gebunden haben verwenden, aber Wenn ich meine App auf Android als Error starte, wird meine App nicht mehr funktionieren und danke für den Link für Fragment -Navigationsschublade :) – Sunny

1

1) Zuerst können alle Ansichten in Ihrem relLay verstecken

RelativeLayout layout = (RelativeLayout) findViewById(R.id.relLay); 
for (int i = 0; i < layout.getChildCount(); i++) { 
    View child = layout.getChildAt(i); 
    child.setVisibility(View.INVISIBLE); 
} 

2) zeigen dann das gewünschte Layout

RelativeLayout activeLayout = (RelativeLayout) findViewById(R.id.layoutX); 
activeLayout.setVisibility(View.VISIBLE); 

So wird Ihre letzte Funktion

public void hidelAllLayoutsExcept (RelativeLayout activeLayout) {

RelativeLayout layout = (RelativeLayout) findViewById(R.id.relLay); 
    for (int i = 0; i < layout.getChildCount(); i++) { 
     View child = layout.getChildAt(i); 
     child.setVisibility(View.INVISIBLE); 
    } 

RelativeLayout activeLayout = (RelativeLayout) findViewById(R.id.layoutX); 
activeLayout.setVisibility(View.VISIBLE); 

}

Verwandte Themen