2016-06-10 5 views
0

Ich möchte ein Fragment auf einem CardView-Klick in einer Aktivität anzeigen, aber das Fragment erscheint hinter allen CardViews in der Aktivität. Unten ist der Code:Fragment, das hinter der Aktivität erscheint

SelectDesignersActivity.java

 package com.houssup.userapp; 

    import android.content.Intent; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.support.design.widget.FloatingActionButton; 
    import android.support.design.widget.Snackbar; 
    import android.support.v7.widget.CardView; 
    import android.view.View; 
    import android.support.design.widget.NavigationView; 
    import android.support.v4.view.GravityCompat; 
    import android.support.v4.widget.DrawerLayout; 
    import android.support.v7.app.ActionBarDrawerToggle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.support.v7.widget.Toolbar; 
    import android.view.Menu; 
    import android.view.MenuItem; 

    public class SelectDesignersActivity extends AppCompatActivity 
      implements NavigationView.OnNavigationItemSelectedListener,DesignersTabFragment.OnFragmentInteractionListener { 

     CardView cardView; 
     @Override 
     protected void onCreate(final Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_five_designers); 
      cardView = (CardView)findViewById(R.id.card_view1); 
      cardView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (savedInstanceState == null) { 
         // Let's first dynamically add a fragment into a frame container 

         getSupportFragmentManager().beginTransaction(). 
           replace(R.id.fragment_container, new DesignersTabFragment(), "SOMETAG"). 
           commit(); 
         // Now later we can lookup the fragment by tag 
        } 

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

     @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.select_designers, 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(); 

      if (id == R.id.nav_camera) { 
       // Handle the camera action 
      } else if (id == R.id.nav_gallery) { 

      } else if (id == R.id.nav_slideshow) { 

      } else if (id == R.id.nav_manage) { 

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

     @Override 
     public void onFragmentInteraction(Uri uri) { 

     } 
    } 

activity_five_designers.xml

 <?xml version="1.0" encoding="utf-8"?> 
    <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     tools:openDrawer="start"> 

     <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/fragment_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 


     <include 
      layout="@layout/app_bar_five_designers" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 


     </include> 

     <android.support.design.widget.NavigationView 
      android:id="@+id/nav_view" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:fitsSystemWindows="true" 
      app:headerLayout="@layout/nav_header_five_designers" 
      app:menu="@menu/activity_five_designers_drawer" /> 


    </android.support.v4.widget.DrawerLayout> 

DesignersTabFragment.java

 package com.houssup.userapp; 

    import android.content.Context; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.support.v4.app.Fragment; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 


    /** 
    * A simple {@link Fragment} subclass. 
    * Activities that contain this fragment must implement the 
    * {@link DesignersTabFragment.OnFragmentInteractionListener} interface 
    * to handle interaction events. 
    * Use the {@link DesignersTabFragment#newInstance} factory method to 
    * create an instance of this fragment. 
    */ 
    public class DesignersTabFragment extends Fragment { 
     // TODO: Rename parameter arguments, choose names that match 
     // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
     private static final String ARG_PARAM1 = "param1"; 
     private static final String ARG_PARAM2 = "param2"; 

     // TODO: Rename and change types of parameters 
     private String mParam1; 
     private String mParam2; 

     private OnFragmentInteractionListener mListener; 

     public DesignersTabFragment() { 
      // Required empty public constructor 
     } 

     /** 
     * Use this factory method to create a new instance of 
     * this fragment using the provided parameters. 
     * 
     * @param param1 Parameter 1. 
     * @param param2 Parameter 2. 
     * @return A new instance of fragment DesignersTabFragment. 
     */ 
     // TODO: Rename and change types and number of parameters 
     public static DesignersTabFragment newInstance(String param1, String param2) { 
      DesignersTabFragment fragment = new DesignersTabFragment(); 
      Bundle args = new Bundle(); 
      args.putString(ARG_PARAM1, param1); 
      args.putString(ARG_PARAM2, param2); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      if (getArguments() != null) { 
       mParam1 = getArguments().getString(ARG_PARAM1); 
       mParam2 = getArguments().getString(ARG_PARAM2); 
      } 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      // Inflate the layout for this fragment 
      return inflater.inflate(R.layout.fragment_designers_tab, container, false); 
     } 

     // TODO: Rename method, update argument and hook method into UI event 
     public void onButtonPressed(Uri uri) { 
      if (mListener != null) { 
       mListener.onFragmentInteraction(uri); 
      } 
     } 

     @Override 
     public void onAttach(Context context) { 
      super.onAttach(context); 
      if (context instanceof OnFragmentInteractionListener) { 
       mListener = (OnFragmentInteractionListener) context; 
      } else { 
       throw new RuntimeException(context.toString() 
         + " must implement OnFragmentInteractionListener"); 
      } 
     } 

     @Override 
     public void onDetach() { 
      super.onDetach(); 
      mListener = null; 
     } 

     /** 
     * This interface must be implemented by activities that contain this 
     * fragment to allow an interaction in this fragment to be communicated 
     * to the activity and potentially other fragments contained in that 
     * activity. 
     * <p> 
     * See the Android Training lesson <a href= 
     * "http://developer.android.com/training/basics/fragments/communicating.html" 
     * >Communicating with Other Fragments</a> for more information. 
     */ 
     public interface OnFragmentInteractionListener { 
      // TODO: Update argument type and name 
      void onFragmentInteraction(Uri uri); 
     } 
    } 

ich das FrameLayout für das Fragment in activity_five_designers aufgenommen haben. XML. Aber das Fragment erscheint hinter allen Kartenansichten. Ich möchte nur das Fragment mit der Navigationsleiste der Aktivität zeigen. Jede Hilfe wird geschätzt.

+0

Wenn Sie zwischen app_bar_five_designers und fragment_container ändern, was passiert? – Chol

Antwort

0

mit diesem Layout Versuchen:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 


    <include 
     layout="@layout/app_bar_five_designers" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

    </include> 

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 


    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_five_designers" 
     app:menu="@menu/activity_five_designers_drawer" /> 


</android.support.v4.widget.DrawerLayout> 

Das erste Kind im Hintergrund sein wird, und die letzte wird

+0

Obwohl das Problem gelöst, aber jetzt die Symbolleiste nicht erscheint, die ich für die Navigationsschublade benötigen – user6111220

+0

Wenn Fragment nicht angezeigt wird, setzen Sie Sichtbarkeit auf INVISIBLE für das FrameLayout – Chol

+0

Nein, eigentlich auf die Aktivität wird angezeigt, aber ich will das gleiche Symbolleiste auf dem Fragment auch mit dem Dreifach-Symbol für die gleiche Navigation Schublade !! – user6111220

0

Sie im Vordergrund stehen müssen Ihr Frame-Layout unter Symbolleiste, um erklären, um Ihre zu sehen Symbolleiste

<include 
     layout="@layout/app_bar_five_designers" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout>