2017-01-13 339 views
0

我无法得到这个工作。基本应用程序的功能与它应有的一样,但其中一个布局文件上的ViewPager根本不显示任何片段。我多次查看代码,但找不到错误。当我尝试调试它时,我发现它甚至不会初始化选择选项卡时应该使用的片段类。 我的MainActivity:ViewPager没有显示片段

public class MainActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

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

    new GetData(this).execute(); 

    TabLayout tab_layout = (TabLayout) findViewById(R.id.tab_layout); 
    tab_layout.addTab(tab_layout.newTab().setText("CASUAL")); 
    tab_layout.addTab(tab_layout.newTab().setText("RANKED")); 
    tab_layout.setTabGravity(TabLayout.GRAVITY_FILL); 

    final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); 
    PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tab_layout.getTabCount()); 
    viewPager.setAdapter(adapter); 
    tab_layout.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) { 

     } 
    }); 

} 

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

    if (id == R.id.nav_stats) { 
     // Handle the camera action 
    } else if (id == R.id.nav_about) { 

    } 

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

我PagerAdapter:

public class PagerAdapter extends FragmentStatePagerAdapter { 
int mNumOfTabs; 

public PagerAdapter(FragmentManager fm, int NumOfTabs) { 
    super(fm); 
    this.mNumOfTabs = NumOfTabs; 
    } 

@Override 
public Fragment getItem(int position) { 

    switch (position) { 
     case 0: 
      CasualStats tab1 = new CasualStats(); 
      return tab1; 
     case 1: 
      RankedStats tab2 = new RankedStats(); 
      return tab2; 
     default: 
      return null; 
     } 
    } 

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

布局与ViewPager:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"><![CDATA[ 

        android:id="@+id/pager" 
        android:layout_width="match_parent" 
        android:layout_height="fill_parent" 
        android:layout_below="@id/tab_layout"/> 
]]> 

<android.support.design.widget.TabLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/tab_layout"> 

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

<view 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    class="android.support.v4.view.ViewPager" 
    android:layout_alignParentStart="true" 
    android:id="@+id/view_pager" 
    android:layout_below="@+id/tab_layout" /> 

</RelativeLayout> 

的,这是为了显示在ViewPager标签片段的类只是正常自动生成的Android Studio片段类。

其中一个片段:

public class CasualStats 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 CasualStats() { 
    // 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 CasualStats. 
*/ 
// TODO: Rename and change types and number of parameters 
public static CasualStats newInstance(String param1, String param2) { 
    CasualStats fragment = new CasualStats(); 
    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_casual_stats, 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 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); 
} 
} 

的XML:

<RelativeLayout 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" 
tools:context="com.app.anzel.r6s.CasualStats"> 

<!-- TODO: Update blank fragment layout --> 

<TextView 
    android:text="1st fragment" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginStart="28dp" 
    android:layout_marginTop="22dp" 
    android:id="@+id/textView" 
    android:visibility="visible" /> 
</RelativeLayout> 
+0

尝试这样的: 1.为您viewpager适配器。 2.在视图页面上设置适配器。 3.使用tabLayout.setupWithViewPager(your_viewpager); 4.确保使用getSupportFragmentManager或getchildFragmentManager(),如果viewpager在Activity中或在另一个片段resp中。 5.您的适配器必须重写公共CharSequence getPageTitle(int位置),以将每个片段的标题提供给选项卡布局。 –

+0

您是否尝试确保碎片确实不可见?我的意思是你似乎没有把任何东西放入textview中,尝试改变背景或在textview中设置一些文本来测试。 –

+0

更改了片段的背景颜色,但不显示它。 – Tachanka

回答

0

你缺少setupWithViewPager,你不需要setOnTabSelectedListener

tab_layout.setupWithViewPager(viewPager) 

让我们来看看这里: http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

您必须覆盖适配器中的getPageTitle,并返回标签标题。

最后,不要在XML使用<view ..class>,建立正常的XML视图:

<android.support.v4.view.ViewPager 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentStart="true" 
    android:id="@+id/view_pager" 
    android:layout_below="@+id/tab_layout"/> 
+0

这只是让我的标题标题消失,viewpager仍然不显示片段。 – Tachanka

+0

我仍然无法获得viewpager来显示片段:( – Tachanka

+0

什么结果你有吗?任何错误? –

0

试试这个:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.TabLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/tab_layout"> 

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

<android.support.v4.view.ViewPager 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentStart="true" 
    android:id="@+id/view_pager" 
    android:layout_below="@+id/tab_layout" /> 

</RelativeLayout> 
在活动

在布局

TabLayout tab_layout = (TabLayout) findViewById(R.id.tab_layout); 
    tab_layout.addTab(tab_layout.newTab().setText("CASUAL")); 
    tab_layout.addTab(tab_layout.newTab().setText("RANKED")); 
    tab_layout.setTabGravity(TabLayout.GRAVITY_FILL); 

    final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); 
    PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tab_layout.getTabCount()); 
    viewPager.setAdapter(adapter); 
    viewPager.setOffscreenPageLimit(2); 
    tab_layout.post(new Runnable() { 
    @Override 
    public void run() { 
     tab_layout.setupWithViewPager(viewPager); 
     } 
    }); 
    tab_layout.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) { 

     } 
    }); 

Pager适配器:

public class PagerAdapter extends FragmentStatePagerAdapter { 
int mNumOfTabs; 

// an array of tab titles 
    private String tabTitles[] = new String[]{"CASUAL", "RANKED"}; 

public PagerAdapter(FragmentManager fm, int NumOfTabs) { 
    super(fm); 
    this.mNumOfTabs = NumOfTabs; 
    } 

@Override 
public Fragment getItem(int position) { 

    switch (position) { 
     case 0: 
      CasualStats tab1 = new CasualStats(); 
      return tab1; 
     case 1: 
      RankedStats tab2 = new RankedStats(); 
      return tab2; 
     default: 
      return null; 
     } 
    } 

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

    @Override 
    public CharSequence getPageTitle(int position) { 
     return tabTitles[position]; 
    } 
} 
+0

没有运气,与以前相同的结果,viewpager没有查看任何内容 – Tachanka

+0

add'viewPager.setOffscreenPageLimit(2);'调用'setupWithViewPager'后也调试你的代码,如果调用getItem int position)' – rafsanahmad007

+0

它将这种方法称为c hecked with“Log”也viewPager.setOffscreenPageLimit(2);没有帮助:( – Tachanka

0

我觉得有点愚蠢。我怀疑问题出在ViewPager上。经过3小时的代码调试后,我只是将ViewPager从布局中移动到另一个基本连接所有布局的布局。

+0

plz检查我的回答,希望它有帮助。 –

0

下面的示例代码与viewpager和制表符工作正常。试试看。

  1. 主要业务布局

    <android.support.design.widget.AppBarLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:theme="@style/AppTheme.AppBarOverlay"> 
    
        <android.support.v7.widget.Toolbar 
         android:id="@+id/toolbar" 
         android:layout_width="match_parent" 
         android:layout_height="?attr/actionBarSize" 
         android:background="?attr/colorPrimary" 
         app:popupTheme="@style/AppTheme.PopupOverlay" /> 
    
    </android.support.design.widget.AppBarLayout> 
    
    <android.support.design.widget.TabLayout 
        android:id="@+id/tab_container" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="?attr/actionBarSize" /> 
    
    <include layout="@layout/content_main" /> 
    
    <android.support.design.widget.FloatingActionButton 
        android:id="@+id/fab" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom|end" 
        android:layout_margin="@dimen/fab_margin" 
        app:srcCompat="@android:drawable/ic_dialog_email" /> 
    

然后viewpager容器布局

<?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:id="@+id/content_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginTop="?attr/actionBarSize" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    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.test.myapplication.MainActivity" 
    tools:showIn="@layout/app_bar_main"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/page_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#FF0000" /> 
</RelativeLayout> 

这里是主要的活动部分:

package com.test.myapplication; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.design.widget.TabLayout; 
import android.support.v4.app.*; 
import android.support.v4.app.Fragment; 
import android.support.v4.view.ViewPager; 
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 MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     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(); 
      } 
     }); 

     ViewPager pager = (ViewPager)findViewById(R.id.page_container); 
     pager.setAdapter(new Adapter(getSupportFragmentManager())); 
     TabLayout tabs = (TabLayout)findViewById(R.id.tab_container); 
     tabs.setupWithViewPager(pager, true); 
     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.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(); 

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


    public static class Adapter extends FragmentStatePagerAdapter { 
     private String[] titles = new String[] {"ONE", "TWO"}; 

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

     @Override 
     public Fragment getItem(int position) { 
      switch (position){ 
       case 0: 
        return new com.test.myapplication.Fragment(); 
       case 1: 
        return new Fragment1(); 
      } 
      return null; 
     } 

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

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