0

Ich bin neu auf Android.Ich erstellte ein Tablayout und ein Viewpager mit zwei Fragmenten.Ich habe Elemente/Elemente in meinem Detail Fragment und ein TextView in meinem Maß Fragment, aber wenn ich Führen Sie den Code auf meinem Handy, nichts ist in meinem Fragment gezeigt. Ich habe nicht Java-Code von meinem Fragment.Alles funktioniert gut auf dem tablayout..ich kann zwischen den Tab wechseln, aber nichts wird in meinen Fragmenten gezeigt ... warum passiert das?Android-Fragmente nicht angezeigt Textansicht und interne Elemente

Meine Aktivität:

<?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" 
     tools:context="com.example.update"> 




       <android.support.design.widget.AppBarLayout 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        > 

        <!--<include android:layout_height="wrap_content" 
         android:layout_width="match_parent" 
         layout="@layout/toolbar_layout"/>--> 

        <include 
         android:id="@+id/update_screen_toolbar" 
         layout="@layout/toolbar_layout" 
         /> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:id="@+id/name_age_gender" 
         android:background="#d3d3d3" 
         android:layout_below="@+id/update_screen_toolbar"/> 



        <android.support.design.widget.TabLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:id="@+id/tabLayout" 
         app:tabMode="fixed" 
         app:tabGravity="fill"> 

        </android.support.design.widget.TabLayout> 


       </android.support.design.widget.AppBarLayout> 

       <android.support.v4.view.ViewPager 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:id="@+id/viewPager"> 
       </android.support.v4.view.ViewPager> 
    </RelativeLayout> 

JAVA-Code für die Aktivität

package com.example.update; 

import android.content.Intent; 
import android.support.design.widget.TabLayout; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.Toast; 



public class UpdateDetail extends AppCompatActivity { 

    TabLayout tabLayout; 
    ViewPager viewPager; 
    ViewPagerAdapter viewPagerAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_detail); 



     tabLayout=(TabLayout)findViewById(R.id.tabLayout); 
     viewPager=(ViewPager)findViewById(R.id.viewPager); 

     viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager()); 
     viewPagerAdapter.addFragments(new DetailFragment()," DETAIL"); 
     viewPagerAdapter.addFragments(new MeasureFragment(),"MEASURE"); 


     viewPager.setAdapter(viewPagerAdapter); 
     tabLayout.setupWithViewPager(viewPager); 
    } 


} 

Code für DetailFragment:

<FrameLayout 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" 
    tools:context="com.example.DetailFragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <Button 
      android:id="@+id/expandableButton4" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="#066da1" 
      android:drawableRight="@android:drawable/arrow_down_float" 
      android:onClick="expandableButton4" 
      android:paddingRight="10dp" 
      android:text="Expand/Collapse Android Example" 
      android:textColor="#fff" /> 

     <com.github.aakira.expandablelayout.ExpandableRelativeLayout 
      android:id="@+id/expandableLayout4" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/expandableButton4" 
      android:background="#90066da1" 
      android:padding="16dp" 
      app:ael_duration="400" 
      app:ael_expanded="false" 
      app:ael_interpolator="bounce" 
      app:ael_orientation="vertical"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Implements the expand and collapse by sliding logic for a top or a bottom view in a two children view or layout or any widgets composition. 
       Implements the expand and collapse by sliding logic for a top or a bottom view in a two children view or layout or any widgets composition." /> 
     </com.github.aakira.expandablelayout.ExpandableRelativeLayout> 

</FrameLayout> 
+0

http://www.androidhive.info/2015/09/android-material-design-working -with-tabs/durch diesen Link geht es definitiv funktioniert – siddhesh

Antwort

0

Versuchen Sie dieses:

public class FriendshipParentFragment extends Fragment { 


@BindView(R.id.tabs) 
TabLayout tabs; 

@BindView(R.id.viewpager) 
ViewPager viewPager; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment_friendship_parent, container, false); 
    ButterKnife.bind(this, view); 
    initTabs(); 
    return view; 
} 


private void initTabs(){ 

    viewPager.setAdapter(new TabsAdapter(getChildFragmentManager())); 
    tabs.setupWithViewPager(viewPager); 
    tabs.getTabAt(0).setText("Tab1"); 
    tabs.getTabAt(1).setText("Tab2"); 
    tabs.setTabTextColors(ContextCompat.getColor(getActivity(), R.color.colorAccent), ContextCompat.getColor(getActivity(), R.color.colorBottomNavigationPrimary)); 
    tabs.setSelectedTabIndicatorColor(ContextCompat.getColor(getActivity(), R.color.white)); 
    tabs.setSelectedTabIndicatorHeight(4); 
} 


public class TabsAdapter extends FragmentPagerAdapter { 

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

    @Override 
    public Fragment getItem(int position) { 
     switch (position){ 
      case 0: 
       return new FollowersFragment(); 
      case 1: 
       return new FollowFragment(); 
     } 
     return null; 
    } 

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

}

XML-Datei

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="owo.owocar.driver.OrdersParentFragment"> 


<android.support.design.widget.TabLayout 
    android:id="@+id/tabs" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:elevation="4dp" 
    app:tabGravity="fill" /> 

<android.support.v4.view.ViewPager 
    android:layout_width="match_parent" 
    android:id="@+id/viewpager" 
    android:layout_height="match_parent"></android.support.v4.view.ViewPager> 

+0

Warum entsteht so ein Problem? ... wenn ich nur ein Toolbat, ein Tab-Layout und eine Ansicht Pager verwendet .... alles hat gut funktioniert? – Jason

+0

@Jason kann Ihren Code nicht sofort ausführen. versuche meinen Ansatz es geht gut;) –

Verwandte Themen