У меня есть приложение. Я использую макет вкладки «Основное действие» с пейджером просмотра. Я пытался загрузить фрагмент в каждую 3 вкладки на моем пейджере просмотра. Но мой фрагмент не отображается в пейджере просмотра. Я делал это раньше несколько раз, но теперь он не работает !! Не знаю, в чем моя ошибка.
mainactivity.java:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
ImageView hamgerMenu;
DrawerLayout drawer;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
setupviews();
}
private void setupviews() {
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragments(new FragmentFriends(), "فعالیت ها");
adapter.addFragments(new FragmentFriends(), "فعالیت ها");
adapter.addFragments(new FragmentFriends(), "فعالیت ها");
adapter.addFragments(new FragmentFriends(), "فعالیت ها");
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(2);
tabLayout.setupWithViewPager(viewPager);
hamgerMenu = (ImageView) findViewById(R.id.img_mainactivity_hambermenu);
hamgerMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawer.openDrawer(Gravity.RIGHT);
}
});
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(Gravity.RIGHT)) {
drawer.closeDrawer(Gravity.RIGHT);
} 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(Gravity.RIGHT);
return true;
}
}
viewpagerAdapter:
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragments;
ArrayList<String> titles;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
fragments=new ArrayList<>();
titles=new ArrayList<>();
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
public void addFragments(Fragment fragment,String title){
fragments.add(fragment);
titles.add(title);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
фрагмент:
public class FragmentFriends extends Fragment {
View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
inflater.inflate(R.layout.friends_fragment,container,false);
return view;
}
}
mainactivity.xml:
<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:layoutDirection = "rtl"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:fitsSystemWindows = "true"
tools:openDrawer = "start">
<include
layout = "@layout/app_bar_main"
android:layout_width = "match_parent"
android:layout_height = "match_parent" />
<android.support.design.widget.NavigationView
android:id = "@+id/nav_view"
android:layoutDirection = "rtl"
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android:layout_gravity = "right"
android:fitsSystemWindows = "true"
app:headerLayout = "@layout/nav_header_main"
app:menu = "@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
appbarmain.xml:
<android.support.design.widget.CoordinatorLayout 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:layoutDirection = "rtl"
tools:context = ".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width = "match_parent"
android:layoutDirection = "rtl"
android:elevation = "0dp"
android:layout_height = "wrap_content"
android:theme = "@style/AppTheme.AppBarOverlay">
<RelativeLayout
android:id = "@+id/toolbar"
android:layout_width = "match_parent"
android:layout_height = "?attr/actionBarSize"
android:background = "?attr/colorPrimary"
android:layoutDirection = "rtl">
<ImageView
android:id = "@+id/img_mainactivity_hambermenu"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentRight = "true"
android:layout_centerVertical = "true"
android:layout_marginRight = "8dp"
android:src = "@drawable/ic_hambermenu" />
<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerHorizontal = "true"
android:layout_centerVertical = "true"
android:text = "@string/app_name"
android:textColor = "@color/white"
android:textSize = "18dp" />
</RelativeLayout>
<android.support.design.widget.TabLayout
android:id = "@+id/tabLayout"
android:background = "@color/colorPrimary"
app:tabIndicatorColor = "@color/white"
app:tabSelectedTextColor = "@color/colorAccent"
app:tabTextColor = "@color/white"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"/>
</android.support.design.widget.AppBarLayout>
<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" />
</android.support.design.widget.CoordinatorLayout>
contentmain.xml:
<LinearLayout 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:orientation = "vertical"
android:layout_height = "match_parent"
app:layout_behavior = "@string/appbar_scrolling_view_behavior"
tools:context = ".MainActivity"
tools:showIn = "@layout/app_bar_main">
<android.support.v4.view.ViewPager
android:layoutDirection = "ltr"
android:id = "@+id/viewPager"
android:layout_width = "match_parent"
android:layout_height = "match_parent"/>
</LinearLayout>
просто сделай это изменение, это сработает
view = inflater.inflate(R.layout.friends_fragment,container,false);