我制作了一个具有标签主机按钮洞察主要活动viewpager的图像应用程序,每个标签只填充片段0和最后一个索引viewpager加载正确的内部其他标签加载邻居片段数据
MainActivity.java
public class MainActivity extends AppCompatActivity {
private void setUpViewPager() {
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new EyeMakeupFragment(), "Eye Makeup");
adapter.addFrag(new EyeBrowMakeupFragment(),"EyeBrow Makeup");
adapter.addFrag(new LipstickGuideFragment(), "Lipstick Guide");
adapter.addFrag(new MakeupTipsFragment(), "Makeup Tips");
adapter.addFrag(new NailArtTutorialsFragment(), "Nail Art Tutorials");
adapter.addFrag(new WomenHairStylesFragment(), "Women Hair Styles");
viewPager.setAdapter(adapter);
}
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
EyeMakeupFragment.java
public class EyeMakeupFragment extends Fragment {
public static boolean isEyeMakeUpFragmentActive=false;
private ImageUtils imageUtils;
List<String> imagePaths = new ArrayList<>();
static GridViewAdapter adapter;
private ExpandableHeightGridView gridView=null;
private int columnWidth;
public EyeMakeupFragment() {
// Required empty public constructor
}
@Override
public void registerForContextMenu(View view) {
super.registerForContextMenu(view);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.eye_makeup_fragment, container, false);
isEyeMakeUpFragmentActive= true;
EyeBrowMakeupFragment.isEyeBrowMakeUpFragmentActive =false;
LipstickGuideFragment.isLipStickFragmentActive = false;
MakeupTipsFragment.isMakeupTipsFragment = false;
NailArtTutorialsFragment.isNailArtTutorialsFragmentActive =false;
WomenHairStylesFragment.isWomenHairStyleFragmentActive = false;
gridView = (ExpandableHeightGridView) rootView.findViewById(R.id.grid_view);
imageUtils = new ImageUtils(getContext());
// Initializing Grid View
InitializeGridLayout();
// loading all image paths from Drawable
imagePaths = imageUtils.getEyeMakeupImageForDisplay(getContext());
// Gridview adapter
adapter = new GridViewAdapter(getContext(),imagePaths, columnWidth, gridView);
// setting grid view adapter
gridView.setExpanded(true);
gridView.setAdapter(adapter);
setGridViewHeightBasedOnChildren(gridView,AppConstant.NUM_OF_COLUMNS);
return rootView;
}
@Override public void setUserVisibleHint(boolean isVisibleToUser) {
isEyeMakeUpFragmentActive = isVisibleToUser;
super.setUserVisibleHint(isVisibleToUser);
}
private void InitializeGridLayout() {
Resources r = getResources();
float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_FRACTION, AppConstant.GRID_PADDING, r.getDisplayMetrics());
columnWidth = (int) ((imageUtils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);
gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
gridView.setColumnWidth(columnWidth);
gridView.setStretchMode(GridView.NO_STRETCH);
gridView.setPadding((int) padding, (int) padding, (int) padding, (int) padding);
gridView.setHorizontalSpacing((int) padding);
gridView.setVerticalSpacing((int) padding);
}
/**
* Expendable GirdView ChildItem Function
* @param gridView
* @param columns
*/
public void setGridViewHeightBasedOnChildren(GridView gridView, int columns) {
ListAdapter listAdapter = gridView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int items = listAdapter.getCount();
int rows = 0;
View listItem = listAdapter.getView(0, null, gridView);
listItem.measure(0, 0);
totalHeight = listItem.getMeasuredHeight();
float x = 1;
if( items > columns ){
x = items/columns;
rows = (int) (x + 1);
totalHeight *= rows;
}
ViewGroup.LayoutParams params = gridView.getLayoutParams();
params.height = totalHeight;
gridView.setLayoutParams(params);
}
}
当标签按钮交换并加载当前片段数据时,任何人都可以帮助传递管理片段吗?
答案 0 :(得分:0)
我在viewPager.setAdapter之前解决了我的问题setOffscreenPageLiimit
viewPager.setOffscreenPageLimit(6) //this line solves behavior of fragment
then set viewPager.setAdapter(adapter);