I know there is a lot of different issues about PageViewers but I can't find solution to my problem.
The problem is that in my app i have one PageViewin another lets say that in PageViewA i have PageView B. In A i keep some informations in TextViews and B. B keeps ImageView and should display images for my fragments. A has few pages and on every page I have right data on TextViews but only one(the first visible one) contains images. When i switch pages none of B ViewPager have images but have right TextViews.
Adapter for A and getFragments() method:
class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> pages;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.pages = fragments;
}
#Override
public Fragment getItem(int position) {
return this.pages.get(position);
}
#Override
public int getCount() {
return this.pages.size();
}
}
private ArrayList<Fragment> getFragments(){
ArrayList<Fragment> fList = new ArrayList<Fragment>();
for(int i=0;i<diary.getList().get(index).events.size();i++){
fList.add(EventPageFragment.newInstance("",index,i));
}
return fList;
}
CreateView and adapter in EventPageFragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
int index = getArguments().getInt(INDEX);
int eventIndex = getArguments().getInt(EVENT);
View v = inflater.inflate(R.layout.page_eventview, container, false);
TextView title = (TextView)v.findViewById(R.id.event_past_title);
TextView date = (TextView)v.findViewById(R.id.event_past_date);
TextView description = (TextView)v.findViewById(R.id.event_past_desc);
title.bringToFront();
images = getFragments(index,eventIndex);
pager = (ViewPager)v.findViewById(R.id.event_pager);
pageAdapter = new MyPageAdapter(getFragmentManager(),images);
pager.setAdapter(pageAdapter);
pageAdapter.notifyDataSetChanged();
pager.invalidate();
title.setText(diary.getList().get(index).events.get(eventIndex).getTitle());
date.setText(getResources().getString(R.string.date)+": "+diary.getList().get(index).events.get(eventIndex).getDate()[0]+"/"+
diary.getList().get(index).events.get(eventIndex).getDate()[1]+"/"+
diary.getList().get(index).events.get(eventIndex).getDate()[2]);
description.setText(getResources().getString(R.string.description)+": "+diary.getList().get(index).events.get(eventIndex).getDescription());
return v;
}
class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> images;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.images = fragments;
}
#Override
public Fragment getItem(int position) {
return this.images.get(position);
}
#Override
public int getCount() {
return this.images.size();
}
}
private ArrayList<Fragment> getFragments(int index, int event){
ArrayList<Fragment> fList = new ArrayList<Fragment>();
for(int i=0;i<diary.getList().get(index).events.get(event).photoReference.size();i++){
fList.add(PhotoFragment.newInstance("",index,event,i));
}
return fList;
}
And finally PhotoFragment that represents images in B:
public static final PhotoFragment newInstance(String message, int personIndex, int eventIndex, int photoIndex)
{
PhotoFragment f = new PhotoFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
bdl.putInt(INDEX, personIndex);
bdl.putInt(EVENT, eventIndex);
bdl.putInt(PHOTO, photoIndex);
f.setArguments(bdl);
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
int index = getArguments().getInt(INDEX);
int eventIndex = getArguments().getInt(EVENT);
int photoIndex = getArguments().getInt(PHOTO);
View v = inflater.inflate(R.layout.page_photo_fragment, container, false);
ImageView image = (ImageView)v.findViewById(R.id.event_photo_image);
image.setImageBitmap(PhotoManager.decodeFile(diary.getList().get(index).events.get(eventIndex).photoReference.get(photoIndex),300));
return v;
}
}
I have no idea why only one EventPage loads images from PhotoFragment
Related
I have an Activity with 2 fragments.
Declared a ParentTabFragment in my Activity in which there is a TabLayout and a ViewPager.
There's a method in my ParentTabFragment called addPage which gets called in the Activity.
The addPage has another method called addFrag which is called from a ViewPagerAdapter creating the views in the ChildTabFragment dynamically.
There is a for loop which runs in the Activity, so the Fragments are created according to the number of items in the customList, creating the amount of tabs and pages in the Fragments.
In my ChildTabFragment I load a customList on tab selection event,
when I select a Tab in the ParentTabFragment, I call a getTabID() method from the ChildTabFragment and send the id of the Tab due to which I load the customList according to the id in the ParentTabFragment.
Problem is when I swipe or select tabs, duplicate/false data gets created in the customList.
I get the proper Id of the selected tab but when I try to pass it in the method, random id is called.
In my Activity Class:
for (int j =0;j<it.size();j++){
parentTabFragment.addPage(it.get(j).getTabMenuItem(), it.get(j).getTabMenuId() , selectedMenu);
}
ParentTabFragment:
public void addPage(String pagename, String pageId, String selectedMenu) {
Bundle bundle = new Bundle();
bundle.putString("data", pagename);
//bundle.putString("pageID", pageId);
childTabFragment = new ChildTabFragment();
childTabFragment.setArguments(bundle);
adapter.addFrag(childTabFragment, pagename, pageId);
adapter.notifyDataSetChanged();
if (selectedMenu == null) {
viewPager.setCurrentItem(0);
} else {
for (int i = 0; i < tabLayout.getTabCount(); i++) {
if (tabLayout.getTabAt(i).getText().equals(selectedMenu)) {
viewPager.setCurrentItem(i);
}
}
}
if (adapter.getCount() > 0) tabLayout.setupWithViewPager(viewPager);
setupTabLayout();
}
public void setupTabLayout() {
selectedTabPosition = viewPager.getCurrentItem();
for (int i = 0; i < tabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setCustomView(adapter.getTabView(i));
}
}
ChildTabFragment:
public class ChildTabFragment extends Fragment {
String childname;
int menuItemId;
TextView textViewChildName, txtViewItemId;
ListView childTabMenuList;
private ArrayList<ChildTabMenu_POJO> childTabMenuPojoList = new ArrayList<>();
private ListView listView;
private ChildTabMenuAdapter childTabMenuAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.child_tab_fragment, container, false);
Bundle bundle = getArguments();
childname = bundle.getString("data");
getIDs(view);
return view;
}
private void getIDs(View view) {
childTabMenuList = (ListView) view.findViewById(R.id.childTabMenuList);
loadChildMenu();
}
private void loadChildMenu(menuItemId) {
ChildTabMenu_POJO menu_pojo4 = new ChildTabMenu_POJO(String.valueOf(menuItemId), "4");
childTabMenuPojoList.add(menu_pojo4);
childTabMenuAdapter = new ChildTabMenuAdapter(childTabMenuPojoList, getActivity());
childTabMenuList.setAdapter(childTabMenuAdapter);
}
public void getTabID(int tabId) {
menuItemId = tabId;
Log.e("updatefrag", String.valueOf(menuItemId));
}
}
ViewPagerAdapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final ArrayList<Fragment> mFragmentList = new ArrayList<>();
private final ArrayList<Adapter_POJO> mFragmentTitleList = new ArrayList<>();
Context context;
ViewPager viewPager;
TabLayout tabLayout;
Adapter_POJO adapter_pojo;
public ViewPagerAdapter(FragmentManager manager, Context context, ViewPager viewPager,
TabLayout tabLayout) {
super(manager);
this.context = context;
this.viewPager = viewPager;
this.tabLayout = tabLayout;
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public int getItemCount() {
return mFragmentTitleList.size();
}
public void addFrag(Fragment fragment, String title, String pageId) {
mFragmentList.add(fragment);
adapter_pojo = new Adapter_POJO(title,pageId);
mFragmentTitleList.add(adapter_pojo);
}
public View getTabView(final int position) {
View view = LayoutInflater.from(context).inflate(R.layout.view_pager_adapter_layout, null);
TextView tabItemName = (TextView) view.findViewById(R.id.textViewTabItemName);
tabItemName.setText(mFragmentTitleList.get(position).adapterMenuItem);
tabItemName.setTextColor(context.getResources().getColor(android.R.color.background_light));
return view;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position).adapterMenuItem;
}
public String getPageId(int position){
return mFragmentTitleList.get(position).adapterMenuId;
}
}
If i understand your question so you need to create newInstance constructor for creating fragment with arguments
public static ChildTabFragment newInstance(int page, String title) {
FirstFragment fragmentFirst = new FirstFragment();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
And Store instance variables based on arguments passed in your ChildTabFragment .
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
page = getArguments().getInt("someInt", 0);
title = getArguments().getString("someTitle");
}
And now update the view in your onCreateView method
TextView tvLabel = (TextView) view.findViewById(R.id.tvLabel);
tvLabel.setText(page + " -- " + title);
And the main thing you should returns the ChildTabFragment to display for that page from your ViewPagerAdapter like this.
#Override
public Fragment getItem(int position) {
return ChildTabFragment.newInstance(position,"page "+String.valueOf(position));
}
I have two fragments (Same class- PlayerFragment) and I set the ViewPager with this method:
private void setupViewPager(ViewPager viewPager) {
FragmentManager fManager = getSupportFragmentManager();
ViewPagerAdapter adapter = new ViewPagerAdapter(fManager);
Bundle args1 = new Bundle();
args1.putInt("Id",1);
PlayerFragment pf1 = new PlayerFragment();
pf1.setArguments(args1);
adapter.addFrag(pf1, "Players 1");
Bundle args2 = new Bundle();
args2.putInt("Id",2);
PlayerFragment pf2 = new PlayerFragment();
pf2.setArguments(args2);
adapter.addFrag(pf2, "Players 2");
viewPager.setAdapter(adapter);
}
I have two tabs but only the first tab shows me content, the other tab is empty.
And the first tabs shows me the contentent of the second one.
static 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);
}
}
In the activity where I use the fragments I have this code:
final ViewPager viewPager = (ViewPager) findViewById(R.id.vpTeam);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabsTeam);
tabLayout.setupWithViewPager(viewPager);
Thanks!
Edit: This is the PlayerFragment
public class PlayerFragment extends Fragment {
private ProgressDialog progressDialog;
private List<Player> myPlayers = new ArrayList<>();
private int myTeamId = 14;
public PlayerFragment(){
}
public static PlayerFragment newInstance(int someInt) {
PlayerFragment myFragment = new PlayerFragment();
Bundle args = new Bundle();
args.putInt("TeamId", someInt);
myFragment.setArguments(args);
return myFragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
Bundle args = getArguments();
if(args!=null) {
myTeamId = args.getInt("TeamId");
}
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_player, container, false);
new BackgroundAsyncTask().execute();
return view;
}
}
Can you post the code of PlayerFragment? I'm pretty sure that the error is that you have the variables in that class as static. Also try to use the "new instance()" way for creating fragments --> Best practice for instantiating a new Android Fragment
I used a ViewPager with FragmentPagerAdapter in my app. I created 4 fragments. Also I used EventBus library to refresh current fragment's background color. When I changed the page, onEvent method was triggered, then all 4 fragments' background color was changed. But I want to change only current fragment's background color. How can I fix this bug? Sorry for my bad English.
ViewPager as below :
public class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return this.fragments.size();
}
}
MyPageViewActivity as below :
public class PageViewActivity extends FragmentActivity {
MyPagerAdapter pageAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
List<Fragment> fragments = getFragments();
pageAdapter = new MyPagerAdapter(getSupportFragmentManager(), fragments);
pager.setAdapter(pageAdapter);
pager.setOffscreenPageLimit(4);
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
EventBus.getDefault.post(new TriggerEvent(position));
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
fList.add(MyFragment.newInstance("1"));
fList.add(MyFragment.newInstance("2"));
fList.add(MyFragment.newInstance("3"));
fList.add(MyFragment.newInstance("4"));
return fList;
}
}
finally MyFragment.class
public class MyFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
RelativeLayout relativeLayout;
String msg;
public static final MyFragment newInstance(String message) {
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
msg = getArguments().getString(EXTRA_MESSAGE);
relativeLayout = (RelativeLayout) v.findViewById(R.id.relativelayout);
return v;
}
public void onEvent(TriggerEvent trigger) {
relativeLayout.setBackgroundColor(randomColor());
}
}
try this,
public void onEvent(TriggerEvent trigger) {
if(isCurrentFragment){
relativeLayout.setBackgroundColor(randomColor());
}
}
I have one FragmentActivity, with one ViewPager and one FragmentStatePagerAdapter. When i change the screen orientation screen on viewPager, i can see the viewPager but not the fragments. I test all post on Stackoverflow.
FragmentStatePagerAdapter
public class PageAdapter extends FragmentStatePagerAdapter{
private static final String[] CONTENT = new String[] { "All", "Installed"};
List<Application> applicationList;
public PageAdapter(FragmentManager fm,List<Application> applicationList) {
super(fm);
this.applicationList = applicationList;
}
#Override
public Fragment getItem(int position) {
return ListAppFragment.newInstance(CONTENT[position % CONTENT.length],applicationList);
}
#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toUpperCase();
}
#Override
public int getCount() {return CONTENT.length;}
}
Fragment
public class ListAppFragment extends Fragment {
public static final String APPS_LIST = "APPS_LIST";
public static final String VIEW_TYPE = "VIEW_TYPE";
private GoogleCardAdapter mGoogleCardAdapter;
private ListView listView;
private Utils utils;
String typeView;
public static ListAppFragment newInstance(String typeView,List<Application> applicationList) {
ListAppFragment listAppFragment = new ListAppFragment();
Bundle bundle = new Bundle();
bundle.putString(VIEW_TYPE, typeView);
bundle.putParcelableArrayList(APPS_LIST, (ArrayList<Application>) applicationList);
listAppFragment.setArguments(bundle);
return listAppFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
utils = new Utils((Context)getActivity().getApplicationContext());
typeView = getArguments().getString(VIEW_TYPE);
View v = inflater.inflate(R.layout.list_app_fragment, container, false);
listView = (ListView)v.findViewById(R.id.activity_googlecards_listview);
ArrayList<Application> applicationList = getArguments().getParcelableArrayList(APPS_LIST);
setApplicationList(applicationList, typeView);
return v;
}
private void setApplicationList(List<Application> applicationList,String typeView) {
if(typeView=="Installed"){
applicationList = utils.getAppsAlreadyInstalled(applicationList);
}
mGoogleCardAdapter = new GoogleCardAdapter(getActivity().getApplicationContext(),applicationList);
AlphaInAnimationAdapter alphaInAnimationAdapter = new AlphaInAnimationAdapter(new AlphaInAnimationAdapter(mGoogleCardAdapter));
alphaInAnimationAdapter.setInitialDelayMillis(300);
alphaInAnimationAdapter.setAbsListView(listView);
listView.setAdapter(alphaInAnimationAdapter);
}
}
Call to adapter in FragmentActivity:
public void getApplicationListFromApi(List<Application> applicationList) {
this.applicationList = applicationList;
pageAdapter= new PageAdapter(getSupportFragmentManager(),this.getApplicationList());
pager.setAdapter(pageAdapter);
//Bind the title indicator to the adapter
TitlePageIndicator titleIndicator = (TitlePageIndicator)findViewById(R.id.titles);
titleIndicator.setViewPager(pager);
}
The problem is "android support v4" library, i am using version r7 (in Maven repository is outdated), and this not work. I changed with version r13 and work fine (refactor proyect to Gradle).
I´m new in working with ViewPager. So i took a tutorial and it all works now. After the tutorial, I have a ViewPager with a TextView on it.
Now, I want to add an ImageView to this TextView. Where do I have to start?
Here´s my code:
public static final MyFragment newInstance(String message){
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
View v = inflater.inflate(R.layout.activity_my_fragment, container, false);
TextView messageTextView = (TextView)v.findViewById(R.id.tvTest);
messageTextView.setText(message);
return v;
}
and
public class Bilderbuch extends FragmentActivity {
MyPageAdapter pageAdapter;
int id;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bilderbuch);
id = getIntent().getIntExtra("kinderid", 0);
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
}
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
DBHelper dbh = new DBHelper(this);
Cursor c = dbh.selectEreignis(id);
int i = 0;
while(c.moveToNext())
{
fList.add(MyFragment.newInstance(c.getString(c.getColumnIndex("ereignisname"))));
i++;
}
fList.add(MyFragment.newInstance("Fragment 1"));
fList.add(MyFragment.newInstance("Fragment 2"));
fList.add(MyFragment.newInstance("Fragment 3"));
return fList;
}
private class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return this.fragments.size();
}
}
You have to write a new Fragment. In to the layout of the Fragment you must add an ImageView. Than you cant make something like below in your MyPagerAdapter:
private class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new MyFragment();
case 1:
return new ImageFragment(R.drawable.my_image); // The second view will return your ImageFragment
default:
return null;
}
}
#Override
public int getCount() {
return this.fragments.size();
}
}
you have "activity_my_fragment.xml" inisde it put this code , with a image src .
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/your_image_name_inside_resDrawable_folder"
/>
Edited :
use this article : http://architects.dzone.com/articles/android-tutorial-using