gpt4 book ai didi

java - 带有 TabLayout 和 ViewPager 的 fragment

转载 作者:太空宇宙 更新时间:2023-11-04 12:31:06 24 4
gpt4 key购买 nike

目前我有带有 2 个选项卡(带有两个 fragment )的 MainActivity 和带有 3 个菜单项的导航 View 。我目前只有一项带有选项卡的 Activity 。在 MainActivity 中,我初始化了 Toolbar、NavigationView、ViewPager、TabLayout。我这里还有一个适配器实例,它为选项卡创建 fragment 。

当我选择菜单项之一时,我希望它打开一个带有其他两个选项卡(带有其他两个 fragment )的新 fragment 。

如何用fragment来实现呢?或者更好地使用额外的 Activity?

NavigationView

MainActivity with two tabs

activity_main_xml:

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

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


<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark"

/>

<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"

app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight = "6dp"

app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@android:color/white"
/>

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


<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"

/>

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






<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/navigationView"
android:layout_gravity="start"
app:headerLayout="@layout/navigation_header"
app:menu="@menu/menu_navigation"

/>

主要 Activity :

package ru.alexbykov.sailesstat;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
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.MenuItem;

import ru.alexbykov.sailesstat.statistic.adapter.TheSalesTabsFragmentAdapter;

public class MainActivity extends AppCompatActivity {


private static final int LAYOUT = R.layout.activity_main;

DrawerLayout drawerLayout;
Toolbar toolbar;
ViewPager viewPager;



@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppDefault);
super.onCreate(savedInstanceState);
setContentView(LAYOUT);


setupToolBar();
setupNavigationView();
setupTabs();


}

private void setupToolBar() {


toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(R.string.app_name);

}

private void setupNavigationView() {

drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);

ActionBarDrawerToggle togle =
new ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.view_navigation_open,
R.string.view_navigation_close);

// drawerLayout.setDrawerListener(togle);

togle.syncState();


NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {


drawerLayout.closeDrawers();

switch (item.getItemId())
{


}




return true;
}
});


}

/* private void startTendersFragment() {


fTrans = getSupportFragmentManager().beginTransaction();;
TendersFragment tendersFragment = new TendersFragment();

fTrans
.add(R.id.frameLayout, tendersFragment)
.addToBackStack(null)
.commit();


}*/




private void setupTabs() {


viewPager = (ViewPager) findViewById(R.id.viewPager);
TheSalesTabsFragmentAdapter adapter = new TheSalesTabsFragmentAdapter(this, getSupportFragmentManager());
viewPager.setAdapter(adapter);

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

}




}

SalesTabsFragmentAdapter

   package ru.alexbykov.sailesstat.statistic.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.HashMap;
import java.util.Map;

import ru.alexbykov.sailesstat.statistic.fragments.AbstractTabFragment;
import ru.alexbykov.sailesstat.statistic.fragments.fragmentsTheSale.ManagersFragment;
import ru.alexbykov.sailesstat.statistic.fragments.fragmentsTheSale.PlanFragment;

/**
* Created by Alexey on 09.06.2016.
*/
public class TheSalesTabsFragmentAdapter extends FragmentPagerAdapter {




//for use strings
private Context context;
private Map<Integer, AbstractTabFragment> tabs;



public TheSalesTabsFragmentAdapter(Context context, FragmentManager fm) {
super(fm);

this.context = context;
initTabs();
}

@Override
public Fragment getItem(int position) {
return tabs.get(position);
}

@Override
public int getCount() {
return tabs.size();
}

@Override
public CharSequence getPageTitle(int position) {

return tabs.get(position).getTitle();
}

private void initTabs() {

tabs = new HashMap<>();

tabs.put(0, PlanFragment.getInstance(context));
tabs.put(1, ManagersFragment.getInstance(context));

}
}

ManagersFragment

package ru.alexbykov.sailesstat.statistic.fragments;


import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.View;

public class AbstractTabFragment extends Fragment {


private String title;
protected Context context;
protected View view;


public void setTitle(String title) {
this.title = title;
}

public void setContext(Context context) {

this.context=context;

}

public String getTitle() {
return title;
}
}

计划 fragment

package ru.alexbykov.sailesstat.statistic.fragments.fragmentsTheSale;


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

import ru.alexbykov.sailesstat.R;
import ru.alexbykov.sailesstat.statistic.fragments.AbstractTabFragment;

/**
* A simple {@link Fragment} subclass.
*/
public class PlanFragment extends AbstractTabFragment {

private static final int LAYOUT = R.layout.fragment_plan;





public static PlanFragment getInstance(Context context) {


/* Bundle bundle = new Bundle();
fragment.setArguments(bundle);*/


PlanFragment fragment = new PlanFragment();
fragment.setContext(context);
fragment.setTitle(context.getString(R.string.tab_plan_stat_fragment));

return fragment;


}

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

view = inflater.inflate(LAYOUT, container, false);
return view;
}








}

AbstractTabFragment

package ru.alexbykov.sailesstat.statistic.fragments;


import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.View;

public class AbstractTabFragment extends Fragment {


private String title;
protected Context context;
protected View view;


public void setTitle(String title) {
this.title = title;
}

public void setContext(Context context) {

this.context=context;

}

public String getTitle() {
return title;
}
}

最佳答案

首先 fragment 并不是像导航 View 那样的最佳实践。我建议使用按钮来切换 Activity ,而根本不使用 fragment 。如果我理解正确的话,您的主页上有内容,我将在其中添加三个按钮,每个按钮都旨在在 NavigationView 的 leiu 中启动另一个 Activity 。每个 Activity 都有自己的按钮,这些按钮将引导您进入您想要进行的任何 Activity 。

关于java - 带有 TabLayout 和 ViewPager 的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37838936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com