0

私は、生徒に関する情報を表示するための画面の一部と、タブレイアウトとビューページャーで残りの部分を持つアプリケーションを持っています。私の問題は、タブレイアウトはデザインエディタに表示されますが、ビューページャーは表示されません。何も書かれていないコードが表示されています。私は物事が間違っているところを教えてくれますか?ViewPagerがxmlに表示されず、エミュレータで動作していませんか?

CAMarksFragment.java

package com.learn.app; 

import android.content.Context; 
import android.content.SharedPreferences; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.design.widget.TabLayout; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 

import android.support.v4.view.ViewPager; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


public class CAMarksFragment extends Fragment { 
TabLayout tabLayout; 
ViewPager viewPager; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 
@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

    return inflater.inflate(R.layout.fragment_camarks, container, false); 
} 
@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    //you can set the title for your toolbar here for different fragments different titles 
    getActivity().setTitle("CA Marks"); 
    viewPager=(ViewPager)getActivity().findViewById(R.id.viewPager); 
    viewPager.setAdapter(new CustomAdapter(getActivity().getSupportFragmentManager(),getActivity().getApplicationContext())); 
    tabLayout=(TabLayout)getActivity().findViewById(R.id.tabLayout); 
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      viewPager.setCurrentItem(tab.getPosition()); 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 
      viewPager.setCurrentItem(tab.getPosition()); 
     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 
      viewPager.setCurrentItem(tab.getPosition()); 
     } 
    }); 
} 

@Override 
public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    SharedPreferences pref = getActivity().getApplicationContext().getSharedPreferences("AppStatus", 0); // 0 - for private mode 
    SharedPreferences.Editor editor = pref.edit(); 
    editor.putBoolean("Home", false); 
    editor.commit(); 



} 

private class CustomAdapter extends FragmentPagerAdapter { 
    private String[] fragments={"Theory","Practical"}; 
    public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) { 
     super(supportFragmentManager); 
    } 

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

    @Override 
    public int getCount() { 
     return fragments.length; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return fragments[position]; 
    } 
} 
} 

fragments_camarks.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
tools:context="com.learn.app.TestTimeTableFragment"> 
<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/tablebackgrounds" 
    > 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:weightSum="100" 
     > 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:id="@+id/ca_all" 
      android:background="@drawable/allbackgrounds" 
      > 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" 


       > 
       <ImageButton 
        android:id="@+id/user_profile_photo" 
        android:layout_width="120dp" 
        android:layout_height="120dp" 
        android:layout_marginTop="10dp" 
        android:layout_centerHorizontal="true" 
        android:background="@drawable/profile_circular_border_imageview" 
        android:padding="20dp" 
        android:scaleType="centerCrop" 
        android:src="@drawable/defaultpic" /> 
       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" 

        > 
        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:textSize="20dp" 
         android:text="Hewitt" 
         android:textStyle="bold" 
         android:textColor="#ffffff" 
         android:id="@+id/camarks_name" 
         android:padding="10dp" 
         /> 
        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:textSize="14dp" 
         android:id="@+id/camarks_dept" 
         android:text="Master of Computer Application" 
         android:textColor="#ffffff" 
         android:padding="10dp" 
         /> 
        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:textSize="14dp" 
         android:id="@+id/camarks_rollno" 
         android:text="15mx13" 
         android:textColor="#ffffff" 
         android:padding="10dp" 
         /> 

       </LinearLayout> 
      </LinearLayout> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textSize="20dp" 
       android:textColor="#ffffff" 
       android:textStyle="bold" 
       android:background="#80000000" 
       android:id="@+id/ca_title" 
       android:text="CA Marks" 
       android:gravity="center" 
       android:padding="10dp" 
       /> 
     </LinearLayout> 

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


     </android.support.design.widget.TabLayout> 
     <android.support.v4.view.ViewPager 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/viewPager" 
      android:layout_below="@id/tabLayout" 
      android:layout_centerHorizontal="true" 
      > 

     </android.support.v4.view.ViewPager> 
     </RelativeLayout> 
    </ScrollView> 
    </FrameLayout> 

Practical.java

package com.learn.app; 

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


public class Practical extends Fragment { 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 

     return inflater.inflate(R.layout.fragment_layout1, container, false); 
    } 
} 

Theory.java

package com.learn.app; 

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class Theory extends Fragment { 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

    return inflater.inflate(R.layout.fragment_layout, container, false); 
} 
} 

fragment_layoutは、その中のTextViewとのLinearLayoutが含まれています。 エミュレータのビューアでshowupが表示されず、tablayoutがuntouchableのままです。ここで間違っています。さらに進めてください。前もって感謝します。

エミュレータ出力

Tablayout and Viewpager not working as you could see in this image

答えて

0

多くのものがコードに間違っています。

XMLで:コードでviewpager

<android.support.v4.view.ViewPager 
      android:id="@+id/viewPager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@id/tabLayout" 
      android:layout_centerHorizontal="true"> 

ためandroid:layout_height="match_parent"を追加します。

変更をあなたのコードをよう:あなたはviewpagerインスタンスを取得するために、フラグメントのビューを使用する必要があります。また、あなたのコードがtablayout

@Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 

     View view = inflater.inflate(R.layout.fragment_camarks, container, false); 

     viewPager=(ViewPager)view.findViewById(R.id.viewPager); 
     tabLayout=(TabLayout)view.findViewById(R.id.tabLayout); 
     tabLayout.addTab(tabLayout.newTab().setText("Theory")); 
     tabLayout.addTab(tabLayout.newTab().setText("Practical")); 
     tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 
     viewPager.setAdapter(new CustomAdapter(getActivity().getSupportFragmentManager(),getActivity().getApplicationContext())); 
     tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
      @Override 
      public void onTabSelected(TabLayout.Tab tab) { 
       viewPager.setCurrentItem(tab.getPosition()); 
      } 

      @Override 
      public void onTabUnselected(TabLayout.Tab tab) { 

      } 

      @Override 
      public void onTabReselected(TabLayout.Tab tab) { 

      } 
     }); 
     tabLayout.setupWithViewPager(viewPager); 

     return view; 
    } 
    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     //you can set the title for your toolbar here for different fragments different titles 
     getActivity().setTitle("CA Marks"); 

    } 
+0

viewpagerためのいくつかの基本的な機能が欠けては、ご回答いただきありがとうございます。 Tablayoutが機能し、タブ間で選択することができました。今度は、各タブのレイアウトファイルを表示する必要があります。それで私を助けてくれますか?タブが選択されたときにビューページのレイアウトを変更する方法。 – hcoder

+0

アダプター 'getItem()'メソッドで既にviewpagerフラグメントを呼び出している場合は、次のようにアダプターを初期化してください: 'new CustomAdapter(getActivity()。getSupportFragmentManager()、getActivity())' – rafsanahmad007

+0

私はあなたの提案を試しました。しかし、OnTabSelectedListenerは動作していないように見えるので、画面が実用的または理論的な断片に置き換えられることはありません。タブが選択されるたびに、その特定のフラグメントを画面にロードする必要があります。今私が持っているのはタブ選択で変わらない空白の画面です。 : – hcoder

関連する問題