Android ActionBar with 3 tab viewpager issue - android

i had create 3 tab with view pager. There is an issue is that in all 3 tab i had save and exit button but when i swipe page one two time and pres on save or exit button it will not work becuse when move to second fragment if call third fragment view.
public class TestActivity extends ActionBarActivity implements TabListener, OnPageChangeListener {
private String TAG = "FrmLsAdd: ";
private static String[] TestAr = new String[3];
private AppSectionsPagerAdapter mAppSectionsPagerAdapter;
private ViewPager mViewPager;
private ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.farmer_add);
TestAr[0] = "Create 1";
TestAr[1] = "Create 2";
TestAr[2] = "Create 3";
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
if (Utils.hasHoneycomb()) {
actionBar.setTitle("Farmer");
}
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(this);
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(mAppSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
String TAG = "Apdtr: ";
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment mFragment = new Fragment();
switch (i) {
case 0:
AppLog.logString(TAG + "case 0");
mFragment = Fragment1.newInstance(i);
break;
case 1:
AppLog.logString(TAG + "case 1");
mFragment = Fragment2.newInstance(i);
break;
case 2:
AppLog.logString(TAG + "case 2");
mFragment = Fragment3.newInstance(i);
break;
default:
AppLog.logString(TAG + "Default");
throw new IllegalArgumentException("not this many fragments: " + i);
}
return mFragment;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return TestAr[(position)];
}
}
public static class Fragment1 extends Fragment {
private String TAG = "Frg1: ";
private DatabaseHandler db;
private static Fragment1 f;
static Fragment1 newInstance(int num) {
f = new Fragment1();
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
AppLog.logString(TAG + "onCreate");
db = new DatabaseHandler(getActivity());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.farmer_add1, container, false);
Bundle args = getArguments();
AppLog.logString(TAG + "onCreateView");
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
AppLog.logString(TAG + "onActivityCreated");
((Button) getActivity().findViewById(R.id.btnSave))
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
AppLog.logString(TAG + "onbtnSave");
}
});
((Button) getActivity().findViewById(R.id.btnCancel))
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog Alert_Dialog = new Dialog(getActivity());
getActivity().finish();
}
});
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
db.close();
super.onDestroy();
}
}
public static class Fragment2 extends Fragment {
private String TAG = "Frg2: ";
private DatabaseHandler db;
private static Fragment2 f;
static Fragment2 newInstance(int num) {
f = new Fragment2();
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
AppLog.logString(TAG + "onCreate");
db = new DatabaseHandler(getActivity());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.farmer_add2, container, false);
Bundle args = getArguments();
AppLog.logString(TAG + "onCreateView");
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
AppLog.logString(TAG + "onActivityCreated");
((Button) getActivity().findViewById(R.id.btnSave))
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
AppLog.logString(TAG + "onbtnSave");
}
});
((Button) getActivity().findViewById(R.id.btnCancel))
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
public static class Fragment3 extends Fragment {
private String TAG = "Frg3: ";
private DatabaseHandler db;
private static Fragment3 f;
static Fragment3 newInstance(int num) {
f = new Fragment3();
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
AppLog.logString(TAG + "onCreate");
db = new DatabaseHandler(getActivity());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.farmer_add3, container, false);
Bundle args = getArguments();
AppLog.logString(TAG + "onCreateView");
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
AppLog.logString(TAG + "onActivityCreated");
((Button) getActivity().findViewById(R.id.btnSave))
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
((Button) getActivity().findViewById(R.id.btnCancel))
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
db.close();
super.onDestroy();
}
}
}
#Override
public void onTabReselected(Tab arg0, android.support.v4.app.FragmentTransaction arg1) {
}
#Override
public void onTabSelected(Tab arg0, android.support.v4.app.FragmentTransaction arg1) {
// mViewPager.setCurrentItem(arg0.getPosition());
mViewPager.setCurrentItem(this.getSupportActionBar().getSelectedNavigationIndex());
}
#Override
public void onTabUnselected(Tab arg0, android.support.v4.app.FragmentTransaction arg1) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
getSupportActionBar().setSelectedNavigationItem(arg0);
}
}

Related

options menu is calling for 2 fragments in view pager how to handle???

I am using viewpager fragment pager adapter, it loads two fragments at a time how to handle option menu it is getting called for both the fragments at a time
public class RTHomeTabFragment extends Fragment implements TabListener,OnBackButtonPressInterfaceForFragment{
public ActionBarActivity parentActivity;
private ActionBar actionBar;
ViewPager mViewPager;
public static int currentPage;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RTMessagingUtils.log(getClass().getName(), "onCreateView");
View view = inflater.inflate(R.layout.viewpager_tabs, container,false);
parentActivity = (ActionBarActivity) getActivity();
actionBar = parentActivity.getSupportActionBar();
mViewPager = (ViewPager) view.findViewById(R.id.pager);
/* actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);*/
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setCustomView(null);
actionBar.removeAllTabs();
// List<Fragment> fragments = getFragments();
pagerAdapter adapter = new pagerAdapter(getChildFragmentManager(), getFragments());
mViewPager.setAdapter(adapter);
currentPage=mViewPager.getCurrentItem();
Log.d("TestFragments", "currentPage======"+currentPage);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
currentPage=mViewPager.getCurrentItem();
Fragment fragment=getFragments().get(position);
fragment.setHasOptionsMenu(true);
Log.d("TestFragments", "currentPage======"+currentPage);
}
});
for (int i = 0; i < adapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(adapter.getPageTitle(i)).setTabListener(this));
}
return view;
}
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
// add fragments here
fList.add(new Test1());
fList.add(new Test2());
fList.add(new Test3());
return fList;
}
class pagerAdapter extends FragmentPagerAdapter{
private List<Fragment> fragments;
public pagerAdapter(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();
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.db_jio_chat_chats).toUpperCase(l);
case 1:
return getString(R.string.db_jio_chat_groups).toUpperCase(l);
case 2:
return getString(R.string.db_jio_chat_contacts).toUpperCase(l);
/*case 3:
return getString(R.string.db_jio_chat_my_profile).toUpperCase(l);
case 4:
return getString(R.string.db_jio_chat_location).toUpperCase(l);
case 5:
return getString(R.string.db_jio_chat_about).toUpperCase(l);*/
}
return null;
}
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
// setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
#Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
mViewPager.setCurrentItem(arg0.getPosition());
}
#Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
#Override
public void onResume() {
super.onResume();
RTMessagingUtils.log(getClass().getName(), "onResume");
}
#Override
public void onPause() {
super.onPause();
RTMessagingUtils.log(getClass().getName(), "onPause");
}
}
Fragment1
public class Test1 extends Fragment {
private ListView listView;
public ActionBarActivity parentActivity;
ArrayList<String> list = new ArrayList<String>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.userlist, container, false);
listView = (ListView) view.findViewById(R.id.listView1);
parentActivity = (ActionBarActivity) getActivity();
final ActionBar actionBar = parentActivity.getSupportActionBar();
actionBar.setIcon(R.drawable.ic_launcher);
actionBar.setTitle("UserList");
actionBar.setSubtitle(null);
list.add("Android");
list.add("Android");
list.add("Android");
list.add("Android");
list.add("Android");
list.add("Android");
list.add("Android");
list.add("Android");
list.add("Android");
list.add("Android");
list.add("Android");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Test2 test2 = new Test2();
// ((MainActivity) parentActivity).replaceFragment(test2, "chat");
}
});
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
Log.d("Test1", "******************onCreateOptionsMenu****************");
super.onCreateOptionsMenu(menu, inflater);
}
}
Fragment2
public class Test2 extends Fragment {
public ActionBarActivity parentActivity;
private ListView listView;
ArrayList<String> list = new ArrayList<String>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
parentActivity = (ActionBarActivity) getActivity();
final ActionBar actionBar = parentActivity.getSupportActionBar();
// actionBar.setIcon(R.drawable.jio_chat_myprofile_add_contact);
actionBar.setTitle("user namednfjgnlxnclkvbnxc");
actionBar.setSubtitle("user status");
View view = inflater.inflate(R.layout.conversation, container, false);
listView = (ListView) view.findViewById(R.id.listView1);
list.add("Android");
list.add("converstion");
list.add("converstion");
list.add("converstion");
list.add("converstion");
list.add("converstion");
list.add("converstion");
list.add("converstion");
list.add("converstion");
list.add("converstion");
list.add("converstion");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.new_message_menu, menu);
}
}
To have separate menus in separate fragments you simply have to use
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.new_message_menu, menu);
}
in fragments and not in there parent activity

How can I communicate/pass data through different Fragments with Swipe Tab?

Hi I have a Swipe Tab Fragment Activity that contains Three Fragments. I created my own adapter that extends FragmentStatePageAdapter.
Now here I'll explain how each tab (Fragments) works.
In Tab 1 and Tab 2, I have a list with practically the same type, when I mean type, I mean they are Symptoms (fever, pain in the eyes, etc.).
Now Tab 3 serves as a tray. Whenever I select an item from the listview in either Tab 1 or Tab 2, I want to transfer that item to Tab 3 and remove that Item from the List where it was selected.
I have tried implementing an Interface but it didn't work for me since I added the fragments on the Main FragmentActivity through an Adapter. MY questions is
How can I communicate the three Fragments? Meaning how can I pass data between these fragment.
Now if you want to take a look at what I've done for now, here it is:
I depicted this as Tab 2 in my question
public class FragmentGeneralSymptoms extends ListFragment implements OnItemClickListener {
ArrayList<String> symptomList;
String name;
String selected;
Tray tray;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_generalsymptoms, container,
false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Bundle args = getArguments();
name = args.getString("key");
tray = (Tray) getActivity();
new LoadSymptoms().execute();
}
class LoadSymptoms extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
KnowledgeBaseHelper helper = new KnowledgeBaseHelper(getActivity());
ProfileHandler handler = new ProfileHandler(getActivity());
Profiles profile = handler.getProfile(name);
String age = profile.getAge() + "";
String gender = profile.getGender();
symptomList = helper.getSymptomList(name, age, gender);
return null;
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
setListAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.general_list, symptomList));
}
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
selected = symptomList.get(arg2);
tray.respond(selected);
}
}
My Tab 3
public class FragmentTray extends ListFragment{
private ArrayList<String> selectedList;
public FragmentTray(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_tray, container, false);
}
public void addToTray(String symptom){
selectedList.add(symptom);
setListAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.general_list, selectedList));
}
public void removeSymptom(String string){
selectedList.remove(selectedList.indexOf(string));
}
}
My MainActivity which Extends FragmentActivity
public class MainActivity extends FragmentActivity implements TabListener, OnPageChangeListener, Tray {
private String selectedProfileName;
ActionBar actionBar;
ViewPager viewPager;
#Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.main_activity);
Bundle args = getIntent().getExtras();
selectedProfileName = args.getString("key_name");
viewPager = (ViewPager) findViewById(R.id.main_pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(this);
InitializeActionBar();
}
public void InitializeActionBar() {
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tabBodyPart = actionBar.newTab();
tabBodyPart.setText("Body Parts");
tabBodyPart.setTabListener(MainActivity.this);
ActionBar.Tab tabGeneralSymptoms = actionBar.newTab();
tabGeneralSymptoms.setText("General Symptoms");
tabGeneralSymptoms.setTabListener(MainActivity.this);
ActionBar.Tab tabSelectedSymptoms = actionBar.newTab();
tabSelectedSymptoms.setText("Selected Symptoms");
tabSelectedSymptoms.setTabListener(MainActivity.this);
actionBar.addTab(tabBodyPart);
actionBar.addTab(tabGeneralSymptoms);
actionBar.addTab(tabSelectedSymptoms);
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Fragment fragment = null;
Bundle bundle = new Bundle();
bundle.putString("key", selectedProfileName);
if (arg0 == 0) {
fragment = new FragmentBodyPart();
fragment.setArguments(bundle);
}
if (arg0 == 1) {
fragment = new FragmentGeneralSymptoms();
fragment.setArguments(bundle);
}
if (arg0 == 2) {
fragment = new FragmentTray();
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
actionBar.setSelectedNavigationItem(arg0);
}
#Override
public void respond(String string) {
// TODO Auto-generated method stub
FragmentTray frag2 = new FragmentTray();
frag2.addToTray(string);
}
}
Finally my Interface
public interface Tray {
public void respond(String string);
}
what I usually do is:
1.- Define the interface. It's done, fine.
2.- Your FragmentGeneralSymptoms must have an initializated listener. Yuou're that but you forgot to set the listener in the class.
public class FragmentGeneralSymptoms extends ListFragment implements OnItemClickListener {
ArrayList<String> symptomList;
String name;
String selected;
Tray tray;
(your code)
public void setTrayListener(Tray listener)
{
tray = listener;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
selected = symptomList.get(arg2);
if(tray != null)
{
symptomList.remove(selected); // if you want to remove this element from this list
tray.respond(selected);
}
} }
3.- Your FragmentTray class must implement the interface:
public class FragmentTray extends ListFragment implements Tray{
private ArrayList<String> selectedList;
public FragmentTray(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_tray, container, false);
}
public void addToTray(String symptom){
selectedList.add(symptom);
setListAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.general_list, selectedList));
}
public void removeSymptom(String string){
selectedList.remove(selectedList.indexOf(string));
}
public void respond(String name)
{
// do stuffs!!!!
selectedList.add(new Symtom(name)); // for example
System.out.println("Inside the respond method respond in FragmentTray class");
}
}
4.- And last but not least. When you build the fragments you must register the listener:
class MyAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragmentList;
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
fragmentList = new ArrayList<Fragment>();
Fragment fragmentBodyPart = new FragmentBodyPart();
Fragment fragmentTray = new FragmentTray();
Fragment fragmentGeneralSymptoms = new FragmentGeneraSymptoms();
// this is important*********************************
fragmentGeneraSmptoms.setTrayListener(fragmentTray);
fragmentList.add(fragmentBodyPart);
fragmentList.add(fragmentGeneralSymptoms);
fragmentList.add(fragmentTray);
}
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Fragment fragment = null;
Bundle bundle = new Bundle();
bundle.putString("key", selectedProfileName);
if (arg0 == 0) {
fragment = (Fragment) fragmentList.get(0);
fragment.setArguments(bundle);
}
if (arg0 == 1) {
fragment = (Fragment) fragmentList.get(1);
fragment.setArguments(bundle);
}
if (arg0 == 2) {
fragment = (Fragment) fragmentList.get(2);
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return fragmentList.size();
}
}
I hope that it helps.

viewpager not displaying child viewpager

in my class I'm calling another class to diplay fragment in dialog
public class FragmentT extends Fragment implements AnimationListener {
ImageButton btn;
Context cxt;
RelativeLayout fragmentT;
View vPOp;
Animation animation;
ViewPager pager;
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if (container == null) {
return null;
}
cxt=getActivity();
this.activity=getActivity();
vPOp = inflater.inflate(R.layout.pop, container, false);
pager = (ViewPager) vPOp.findViewById(R.id.up);
btn = (ImageButton) vPop.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DialogFragmentWindow().show(getSupportFragmentManager(),"");
}
});
return vPop;
}
#Override
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
}
where DialogFragmentWindow is
public class DialogFragmentWindow extends DialogFragment {
PageAdapter pPageAdapter;
Context context;
ViewPager vp;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pop, container);
context = getActivity();
// this.activity=getActivity();
vp = (ViewPager) view.findViewById(R.id.pVF);
List<Fragment> fragments = getFragments();
FragmentAdapter pA = new FragmentAdapter(getChildFragmentManager(),fragments);
vp.setAdapter(pA);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return view;
}
private List getFragments() {
List<Fragment> fragmentPop = new Vector<Fragment>();
//fragmentPop.add(FragSc1.newInstance());
fragmentPop.add(Fragment.instantiate(context,
FragSc1.class.getName()));
fragmentPop.add(Fragment.instantiate(context,
FragSc2.class.getName()));
return fragmentPop;
}
}
so the problem is,neither do DialogFragmentWindow is accepting the argument getChildFragmentManager(),nor do the FragmentT class is taking,
new DialogFragmentWindow().show(getSupportFragmentManager(), "");
the viewpager i'm calling is in another layout so want to use getChildFragmentManager(),not able understand thwe prob,using chid fragment/viewpager for the first time.
Just try to do as like this. It may help you.
new DialogFragmentWindow().show(getSupportFragmentManager().beginTransaction(), "dialog");

Android findViewById not working in ViewPager

New and working on ViewPager, and encounter some difficulties that would like you to offer some advice...
ViewPagerAdapter class
public class ViewPagerAdapter extends FragmentPagerAdapter
{
private Context _context;
public ViewPagerAdapter(Context context, FragmentManager fm)
{
super(fm);
_context=context;
}
#Override
public Fragment getItem(int position)
{
Fragment f = new Fragment();
switch(position){
case 0:
f=App_Intro.newInstance(_context);
break;
case 1:
f=LayoutTwo.newInstance(_context);
break;
}
return f;
}
#Override
public int getCount()
{
return 2;
}
}
ViewPagerStyle1Activity class
public class ViewPagerStyle1Activity extends FragmentActivity
{
private ViewPager _mViewPager;
private ViewPagerAdapter _adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpView();
setTab();
}
private void setUpView()
{
_mViewPager = (ViewPager) findViewById(R.id.viewPager);
_adapter = new ViewPagerAdapter(getApplicationContext(),getSupportFragmentManager());
_mViewPager.setAdapter(_adapter);
_mViewPager.setCurrentItem(0);
}
private void setTab()
{
_mViewPager.setOnPageChangeListener(new OnPageChangeListener()
{
#Override
public void onPageScrollStateChanged(int position) {}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}
#Override
public void onPageSelected(int position)
{
// TODO Auto-generated method stub
switch(position)
{
case 0:
findViewById(R.id.first_tab).setVisibility(View.VISIBLE);
findViewById(R.id.second_tab).setVisibility(View.INVISIBLE);
break;
case 1:
findViewById(R.id.first_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab).setVisibility(View.VISIBLE);
break;
}
}
});
}
}
App_Intro class
public class App_Intro extends Fragment
{
public static Fragment newInstance(Context context)
{
App_Intro f = new App_Intro();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.app_introduce_scroll, null);
Button calpaidBtn = (Button) findViewById(R.id.calpaidBtn); //RED UNDERLINE ERROR
calpaidBtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent (Intent.ACTION_VIEW,
Uri.parse("https://abc.com"));
startActivity(intent);
return;
}
});
return root;
}
}
Question:
It underlines red in the App_Intro class for findViewById that "The method findViewById(int) is undefined for the type App_Intro"
How does this be solved? Actually how to put different activities into the ViewPager? Are there any examples?
Many thanks in advance!
Use the following :
root.findViewById(..)

onCreateView() method of ViewPager's childFragment not being called

MainActivity.java
public class MainActivity extends SherlockFragmentActivity {
static MyListfragment mf;
ViewPager mpager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mf = new MyListfragment();
ft.add(android.R.id.content,mf).commit();
}}
MyListfragment.java
public class MyListfragment extends SherlockListFragment {
MyDisplayfragment tempFrag;
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Connector.selection = position;
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
if(position==0)
{
tempFrag=new MyDisplayfragment();
tempFrag.setLayout(R.layout.law);
subFragmentAdapter mAdapter = new subFragmentAdapter(getChildFragmentManager());
tempFrag.setViewPagerAdapter(mAdapter);
}
ft.replace(android.R.id.content,tempFrag);
ft.addToBackStack(null);
ft.commit();
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;
setListAdapter(new ArrayAdapter<String>(getActivity(),
layout, Connector.list));
}}
MyDisplayfragment.java
public class MyDisplayfragment extends SherlockFragment {
int resource;
FragmentPagerAdapter mAdapter;
ViewPager v;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if(Connector.selection==-1)
resource=R.layout.displayfragment;
return inflater.inflate(resource, container, false);
}
public void setViewPagerAdapter(FragmentPagerAdapter x)
{
mAdapter=x;
}
public void setLayout(int layout)
{
resource=layout;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
ViewPager pager=(ViewPager) view.findViewById(R.id.mpager);
pager.setAdapter(mAdapter);
}}
subFragment.java
public final class subFragment extends SherlockFragment {
private int layout;
public static subFragment newInstance(int content) {
subFragment fragment = new subFragment();
fragment.layout=content;
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(layout,container,false);
}}
subFragmentAdapter.java
public class subFragmentAdapter extends FragmentPagerAdapter{
int[] LAYOUT={R.layout.cccc,R.layout.aaaa,R.layout.bbb};
private int mCount;
public subFragmentAdapter(FragmentManager fm) {
super(fm);
mCount = LAYOUT.length;
}
#Override
public Fragment getItem(int position) {
return subFragment.newInstance(LAYOUT[position]);
}
#Override
public int getCount() {
return mCount;
}
}
Connector.java
public class Connector {
static int selection = -1;
static String list[] = { "Law" };}
In the above project
1)MyListfragment.java is used for navigation
2)MyDisplayfragment.java is used to display according to selection made in MyListFragment
3)Connector.java is used to communicate between the above two fragments
4)subFragment.java is used to populate Viewpager with Fragments
5)subFragmentAdapter.java is th adapter for Viewpager
When i run this project the viewpager seems to be blank and doesnot show any of the child fragments and after debugging i saw that onCreateView() method of subFragment.java is not called.
My question is why the onCreateView() method is not being called
Please refer to Luksprog's comment for the answer.

Categories

Resources