Using horizontalpaging example in android studio,
i put two activities, Tab1.Java and Tab2.Java.
And i want to put textswitcher in both activity.
But, TextView(Tab1.this) in Tab1.Java is make an error.
And i think it has another problems.
How can i make this codes work?
MainActivity.Java
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_main);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
#Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
Context mContext;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return new Tab1(mContext);
case 1:
return new Tab2(mContext);
}
return null;
}
#Override
public int getCount() {
// Show 3 total pages.
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
}
return null;
}
}
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return rootView;
}
}}
Tab1.Java
public class Tab1 extends Fragment {
Context mContext;
public Tab1(Context context) {
mContext = context;
}
private TextSwitcher mSwitcher;
Button button1;
String textToShow[]={"st1","st2","st3","st4","st5","st6"};
int messageCount=textToShow.length;
int currentIndex=-1;
public View onCreateView(LayoutInflater inflater,
ViewGroup container,Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.activity_tab1, null);
super.onCreate(savedInstanceState);
button1=(Button)view.findViewById(R.id.button1);
mSwitcher = (TextSwitcher) view.findViewById(R.id.textSwitcher);
mSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
#Override
public View makeView() {
TextView myText = new TextView(Tab1.this);
myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
myText.setTextSize(36);
return myText; }});
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
currentIndex++;
// If index reaches maximum reset it
if (currentIndex == messageCount)
currentIndex = 0;
mSwitcher.setText(textToShow[currentIndex]);
}});
return view; }
}
activity_tab1.xml
<TextSwitcher
android:layout_marginTop="50dp"
android:id="#+id/textSwitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:singleLine="false"
android:typeface="normal" />
Related
I'm hoping someone can help me with this extremely annoying problem. I'm new to working with fragments. I've have spend two days trying to get buttons to work inside my fragment. My app is a tab activity with sliding fragments auto created by android studio. I can change fragments by sliding and using the tabs. But I cannot get the buttons to respond. My app does not crash and my Log.e doesn't get registered in Logcat. I have copied lots of examples from the internet, but nothing seems to work.
I have tried implementing View.OnClickListener and not implementing it but nothing works. I'll post two examples that should work, but they don't.
FRAGMENT without implementing View.OnClickListener
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_summary_loggs, container, false);
Button test = (Button) rootView.findViewById(R.id.testButton);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("DEBUGG", "BUTTON PRESSED");
}
});
return rootView;
FRAGMENT with implementing View.OnClickListener
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_summary_loggs, container, false);
Button test = (Button) rootView.findViewById(R.id.testButton);
test.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.testButton:
Log.e("DEBUGG", "BUTTON PRESSED");
break;
}
}
This is only two examples of many that I have tried and it is driving me crazy. On all the examples on internet they all get them to work. My buttons simply won't respond when I press them. I will be extremely thankful if you could help me with this.
Layoutfile
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="xxxxxx.SummaryLoggs">
<!-- TODO: Update blank fragment layout -->
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:text="#string/hello_blank_fragment3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:id="#+id/testButton"
android:layout_gravity="center" />
</FrameLayout>
ACTIVITY where i swipe fragments view
import...
public class AmLogger extends ActionBarActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
Handler customHandler = new Handler();
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_am_logger);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_am_logger, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
private int mPosition;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPosition = getArguments().getInt(ARG_SECTION_NUMBER);
Log.e("DEBUGG", "mPosition: " + mPosition);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_summary_loggs, container, false);
switch (mPosition) {
case 1:
rootView = inflater.inflate(R.layout.fragment_add_time, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_item_list, container, false);
break;
}
return rootView;
}
}
}
I think you're not creating 3 fragments. You should have 3 fragment classes in your activity one for each tab and also depending upon design for each fragment, you need to have 3 layout files. Then declare the button in your appropriate layout file and use it in your Fragment class as shown below.
public class AmLogger extends ActionBarActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
Handler customHandler = new Handler();
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_am_logger);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_am_logger, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
// AddTime fragment
return AddTime.newInstance(position + 1);
case 1:
// MyLogs fragment
return MyLogs.newInstance(position + 1);
case 2:
// SummaryLogs fragment
return SummaryLogs.newInstance(position + 1);
}
return null;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
//AddTime fragment
public static class AddTime extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static AddTime newInstance(int sectionNumber) {
AddTime fragment = new AddTime();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public AddTime() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_addtime, container, false);
return rootView;
}
}
//MyLogs Fragment
public static class MyLogs extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static MyLogs newInstance(int sectionNumber) {
MyLogs fragment = new MyLogs();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public MyLogs() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mylogs, container, false);
return rootView;
}
}
//SummaryLogs fragment
public static class SummaryLogs extends Fragment implements View.OnClickListener{
private static final String ARG_SECTION_NUMBER = "section_number";
public static SummaryLogs newInstance(int sectionNumber) {
SummaryLogs fragment = new SummaryLogs();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public SummaryLogs() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_summarylogs, container, false);
//Code to get the button from layout file
Button btn = (Button) rootView.findViewById(R.id.testButton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Implement the code to run on button click here
}
});
return rootView;
}
}
}
when i run this code,the toast in two different fragments are displayed on one tab. when i swipe to next tab nothing is displayed.
this is is my main tab activity:
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Offers", "Distance", "Happy Hours","Shop List" };
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
this is adapter class :
public class TabsPagerAdapter extends FragmentStatePagerAdapter {
Bundle bundle = new Bundle();
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
Fragment of = new OfferFragment();
bundle.putString("OfferFragment", "OfferFragment");
of.setArguments(bundle);
return of;
case 1:
Fragment df = new DistanceFragment();
bundle.putString("DistanceFragment", "DistanceFragment");
df.setArguments(bundle);
return df;
case 2:
Fragment hf = new HappyHoursFragment();
bundle.putString("HappyHoursFragment", "HappyHoursFragment");
hf.setArguments(bundle);
return hf;
case 3:
Fragment sf = new ShoplistFragment();
bundle.putString("ShoplistFragment", "ShoplistFragment");
sf.setArguments(bundle);
return sf;
}
return null;
}
#Override
public int getCount() {
return 4;
}
}
this is my first fragment :
public class OfferFragment extends Fragment {
private String name;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// public static final String ARG_OBJECT = "object";
View rootView = inflater.inflate(R.layout.offerfragment, container,
false);
Bundle args = getArguments();
TextView txtview = (TextView) rootView.findViewById(R.id.offer);
name = args.getString("OfferFragment");
txtview.setText(args.getString("OfferFragment"));
display();
return rootView;
}
private void display() {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), name, Toast.LENGTH_SHORT).show();
}
}
this is my second fragment :
public class DistanceFragment extends Fragment {
private String name;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.distancefragment, container, false);
Bundle args = getArguments();
TextView txtview = (TextView) rootView.findViewById(R.id.distance);
name = args.getString("DistanceFragment");
txtview.setText(args.getString("DistanceFragment"));
display();
return rootView;
}
private void display() {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), name, Toast.LENGTH_SHORT).show();
}
}
any suggestions are appreciated. am stuck with this.
This is because next fragment is created by pager
before showing toast check if that fragment is visible or not
if(fragmentName.this.isVisible())
{
// do your stuff here
}
Write onResume() in every fragment class.
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
//Display Toast message here
}
}
I would like to create a new fragment every time when I click its tab.
I tried to get the tab and to create a fragment. But it doesn't seem to work.
Here is my code.
Main Activity
public class MainActivity extends FragmentActivity {
private static ViewPager viewPager;
private static TabsPagerAdapter mAdapter;
private static ActionBar actionBar;
// Tab titles
private String[] tabs = { "TopRated", "Hots", "NEW"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
//actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(tabListener));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
static ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
};
TabsPagerAdapter
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new TopRatedFragment();
case 1:
return new HotStoryFragment();
case 2:
return new AllClassFragment();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
One of the fragments
public class HotStoryFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("Refresh_F","(2) HotStory");
View rootView = inflater.inflate(R.layout.fragment_hotstory, container, false);
TextView tvStudy = (TextView)rootView.findViewById(R.id.text);
return rootView;
}
I have a problem. As said i have ViewPager with 5 Fragments inside. On the last fragment i have two relative layouts with some widgets inside. On button click i have animations that changes relative layouts to visible.
My problem is that when i scroll back the view that is gone becomes visible all by himself... Anybody experienced something similar???
public class WelcomeActivity extends FragmentActivity{
private FragmentAdapter adapter;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.welcome_activity);
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new FragmentAdapter(getSupportFragmentManager(), viewPager);
viewPager.setAdapter(adapter);
}
public class FragmentAdapter extends FragmentPagerAdapter{
private ViewPager pager;
private Fragment mFragmentAtPos0;
private FragmentManager mFragmentManager;
public FragmentAdapter(FragmentManager fm, ViewPager p) {
super(fm);
this.pager = p;
this.mFragmentManager = fm;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ScreenWelcome(pager, this);
case 1:
return new ScreenTutorial1();
case 2:
return new ScreenTutorial2();
case 3:
return new ScreenTutorial3();
case 4:
return new ScreenTutorial4();
case 5:
return new FlipAnimationFragment();
default:
return null;
}
}
#Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
#Override
public int getCount() {
return 6;
}
public class FlipAnimationFragment extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.flip_animation, container, false);
layout = (RelativeLayout) view.findViewById(R.id.layout);
sign_up = (RelativeLayout) view.findViewById(R.id.relativeLayoutSignUp);
log_in = (RelativeLayout) view.findViewById(R.id.relativeLayoutLogIn);
create_account_page = (Button) view.findViewById(R.id.buttonCreateAccount);
log_in_page = (Button) view.findViewById(R.id.buttonSignUpLogIn);
log_in_page.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!pressed){
pressed = true;
layout.startAnimation(flip);
}
}
});
create_account_page.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!pressed){
pressed = true;
layout.startAnimation(flip);
}
}
});
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
flip = new FlipAnimator(sign_up, log_in, 240, 400);
}
I have two listfragment and one fragment in my application. I show you first fragment.
The application starts, Asynctask retrieves data and put them in arrayNews.
I think the problem is the update of listview or adapter in listfragment that does not refresh.
If I change the phone's orientation => listfragment (+ listview) appears correctly.
If I go to fragment 3 (fragment in 2 and 3 in memory), then come back on fragment 1 => it works correctly.
Sorry for my english ^^
public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
public static ArrayList<Data> arrayNews = new ArrayList<Data>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Fragment> listeFragments = new Vector<Fragment>();
listeFragments.add(Fragment.instantiate(this, ActualiteFragment.class.getName()));
listeFragments.add(Fragment.instantiate(this, DossierFragment.class.getName()));
listeFragments.add(Fragment.instantiate(this, ForumFragment.class.getName()));
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), listeFragments);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
{
#Override
public void onPageSelected(int position)
{
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++)
{
actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
}
if (arrayDossier.size() == 0 && arrayDossier.size() == 0)
{
chargement_donnees();
}
}
public void chargement_donnees()
{
AsyncTaskChargement chargNews = new AsyncTaskChargement();
chargNews.execute();
}
public class AsyncTaskChargement extends AsyncTask<Void, Integer, Void>
{
private ProgressDialog dialog = new ProgressDialog(MainActivity.this);
#Override
protected void onPreExecute()
{
super.onPreExecute();
dialog.setMessage("Chargement des données ...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
#Override
protected Void doInBackground(Void... arg0)
{
arrayNews = ContainerData_news.getFeeds();
return null;
}
#Override
protected void onPostExecute(Void result)
{
if (this.dialog.isShowing())
{
this.dialog.dismiss();
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_refresh:
chargement_donnees();
return true;
default:
return false;
}
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
}
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
mViewPager.setCurrentItem(tab.getPosition());
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
{
}
public class SectionsPagerAdapter extends FragmentPagerAdapter
{
List<Fragment> listeFragments;
public SectionsPagerAdapter(FragmentManager fm, List<Fragment> listeFragments)
{
super(fm);
this.listeFragments = listeFragments;
}
#Override
public Fragment getItem(int position)
{
return listeFragments.get(position);
}
#Override
public int getCount()
{
return listeFragments.size();
}
#Override
public CharSequence getPageTitle(int position)
{
switch (position)
{
case 0:
return getString(R.string.title_section1).toUpperCase();
case 1:
return getString(R.string.title_section2).toUpperCase();
case 2:
return getString(R.string.title_section3).toUpperCase();
}
return null;
}
}
public static class ActualiteFragment extends ListFragment
{
public ActualiteFragment()
{
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
setListAdapter(new AdapterListe(getActivity(), arrayNews));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.listfragment, container, false);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id)
{
...
}
}
}
I found an easy way to update such list
Steps would be.:
1. declare onCreateView in ListFragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.TheLayoutContainingAListView, container, false);
return view;
}
TheLayoutContainingAListView.xml should contain only a listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
The android:id must be "#android:id/list" . however you may try other things.. I didn't..
2. In onPostExecute of your AsyncTask (Asynctask should be in MainActivity), do everything that you want to do.. i.e. declare an adapter, and setAdapter
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
lv = (ListView) findViewById(android.R.id.list);
CustomAdapter adapt = new CustomAdapter(MainActivity.this,
android.R.id.list, fetch); //You can use ArrayAdapter.. i wanted a custom one
lv.setAdapter(adapt);
showFragment(YOUR Fragment, false);
super.onPostExecute(result);
}
showFragment is just a function i used to show a particular fragment and hide all others.. you can easily achieve it by FragmentTransaction show() and hide() methods.
Use youradapter.notifyDataSetChanged(); in onPostExecute to force a redraw of your list.