http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit(int)

just set it to 2

On Wednesday, October 17, 2012 7:30:54 PM UTC+2, Giosia Gentile wrote:
>
> it is one week I try to solve this problem whitout success. Please 
> help me. 
>
> I use the tabs navigation with viewpager. This is the class where I 
> put the tabs and the FragmentPagerAdapter class: 
> public class Detail extends SherlockFragmentActivity { 
>
>     ViewPager  mViewPager; 
>
>     TabsAdapter mTabsAdapter; 
>
>
>     @Override 
>     public void onCreate(Bundle savedInstanceState) { 
>
>         super.onCreate(savedInstanceState); 
>
>
>         setContentView(R.layout.activity_main); 
>
>      ... 
>
>
>
>      ActionBar bar = getSupportActionBar(); 
>      bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
>      bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); 
>
>      mViewPager = (ViewPager)findViewById(R.id.pager); 
>
>      // Add the tabs 
>      mTabsAdapter = new TabsAdapter(this, bar, mViewPager); 
>      mTabsAdapter.addTab(bar.newTab().setText(R.string.filmtab), 
>              FragmentFilm.class, null); 
>      mTabsAdapter.addTab(bar.newTab().setText(R.string.cinematab), 
>              FragmentCinema.class, null); 
>      mTabsAdapter.addTab(bar.newTab().setText(R.string.dintornitab), 
>              FragmentPdi.class, null); 
>
>
>
>      if (savedInstanceState != null) { 
>   
> bar.setSelectedNavigationItem(savedInstanceState.getInt("tab")); 
>      } 
>
>
>
>     } 
>
>
>      @Override 
>         protected void onSaveInstanceState(Bundle outState) { 
>             super.onSaveInstanceState(outState); 
>             outState.putInt("tab", 
> getSupportActionBar().getSelectedNavigationIndex()); 
>         } 
>
>
>      public static class TabsAdapter extends FragmentPagerAdapter 
>      implements ViewPager.OnPageChangeListener, ActionBar.TabListener 
> { 
>          private final Context mContext; 
>          private final ActionBar mBar; 
>          private final ViewPager mViewPager; 
>          private final ArrayList<TabInfo> mTabs = new 
> ArrayList<TabInfo>(); 
>
>          static final class TabInfo { 
>              private final Class<?> clss; 
>              private final Bundle args; 
>
>              TabInfo(Class<?> _class, Bundle _args) { 
>                  clss = _class; 
>                  args = _args; 
>              } 
>          } 
>
>          public TabsAdapter(Detail activity, ActionBar bar, ViewPager 
> pager) { 
>              super(activity.getSupportFragmentManager()); 
>              mContext = activity; 
>              mBar = bar; 
>              mViewPager = pager; 
>              mViewPager.setAdapter(this); 
>              mViewPager.setOnPageChangeListener(this); 
>          } 
>
>          public void addTab(ActionBar.Tab tab, Class<? extends 
> Fragment> clss, Bundle args) { 
>              TabInfo info = new TabInfo(clss, args); 
>              tab.setTag(info); 
>              tab.setTabListener(this); 
>              mTabs.add(info); 
>              mBar.addTab(tab); 
>              notifyDataSetChanged(); 
>          } 
>
>          @Override 
>          public int getCount() { 
>              return mTabs.size(); 
>          } 
>
>          @Override 
>          public Fragment getItem(int position) { 
>              TabInfo info = mTabs.get(position); 
>              return Fragment.instantiate(mContext, 
> info.clss.getName(), info.args); 
>          } 
>
>          @Override 
>          public void onPageScrolled(int position, float 
> positionOffset, int positionOffsetPixels) { 
>          } 
>
>          @Override 
>          public void onPageSelected(int position) { 
>              mBar.setSelectedNavigationItem(position); 
>          } 
>
>          @Override 
>          public void onPageScrollStateChanged(int state) { 
>          } 
>
>          @Override 
>          public void onTabSelected(Tab tab, FragmentTransaction ft) { 
>              Object tag = tab.getTag(); 
>              for (int i=0; i<mTabs.size(); i++) { 
>                  if (mTabs.get(i) == tag) { 
>                      mViewPager.setCurrentItem(i); 
>                  } 
>              } 
>          } 
>
>          @Override 
>          public void onTabUnselected(Tab tab, FragmentTransaction ft) 
> { 
>
>          } 
>
>          @Override 
>          public void onTabReselected(Tab tab, FragmentTransaction ft) 
> { 
>
>          } 
>      } 
> } 
>
> The 3 fragment classes are all the same I copy here just one; In the 
> fragment class I use async task for download the data I need to put in 
> the view, I do this in the onActivityCreated method: 
>
> public class FragmentFilm extends SherlockFragment 
> { 
>
>     private Detail act; 
>
>     private DetailedRec detail_film; 
>     private View view; 
>     private String a; 
>
>
>
>
>     @Override 
>     public View onCreateView(LayoutInflater inflater, ViewGroup 
> container, 
>             Bundle savedInstanceState) 
>     { 
>
>
>
>         setRetainInstance(true); 
>         view = inflater.inflate(R.layout.tab_film_info, container, 
> false); 
>
>         return view; 
>     } 
>
>     /* 
>     @Override 
>     public void onSaveInstanceState(Bundle outState) { 
>         super.onSaveInstanceState(outState); 
>
>
>     }*/ 
>
>
>     @Override 
>     public void onCreate(Bundle savedInstanceState) { 
>         super.onCreate(savedInstanceState); 
>
>         act =  (Detail) getActivity(); 
>
>
>     } 
>
>
>
>
>     @Override 
>     public void onActivityCreated(Bundle savedInstanceState) { 
>         super.onActivityCreated(savedInstanceState); 
>         new DownloadFilmDetailAsyncTask().execute(); 
>
>
>     } 
>
>
>
>     private class DownloadFilmDetailAsyncTask extends AsyncTask<Void, 
> DetailedRec, Void> 
>     { 
>
>         @Override 
>         protected void onPreExecute() { 
>
>             super.onPreExecute(); 
>
>
>         } 
>
>         @Override 
>         protected void onPostExecute(Void result) { 
>
>             super.onPostExecute(result); 
>
>             ProgressBar prBar = 
> (ProgressBar)getView().findViewById(R.id.progressbar_film); 
>             prBar.setVisibility(View.GONE); 
>             ScrollView lay = 
> (ScrollView)getView().findViewById(R.id.tab_filmsummary); 
>             lay.setVisibility(View.VISIBLE); 
>
>
>         } 
>
>         @Override 
>         protected Void doInBackground(Void... params) 
>         { 
>             try 
>             { 
>
>
>                 String locale = 
> getResources().getConfiguration().locale.getDisplayName(); 
>
>
>                 JSONObject objSend = new JSONObject(); 
>                 objSend.put("idFilm", act.getIdFilm()); 
>                 objSend.put("cinemaId",act.getIdCinema()); 
>                 int ind = locale.indexOf("("); 
>                 String locale_send = locale.substring(0, ind-1); 
>                 objSend.put("locale", locale_send); 
>                 ArrayList<String> otherCin = new 
> ArrayList<String>(Arrays.asList(act.getOtherCinemas())); 
>                 JSONArray othCin = new JSONArray(otherCin); 
>                 objSend.put("otherCinemas", othCin ); 
>                 JSONObject jsonObject = 
> sendAndGetJSONObject(JSON_SERVER+"JsonServer?op=getFilmbyId",objSend); 
>
>
>
>                 DetailedRec detail_rec = new DetailedRec(); 
>                 //FILM 
>                 detail_rec.setFilmId(jsonObject.getString("filmId")); 
>                 detail_rec.setName(jsonObject.getString("name")); 
>   
> detail_rec.setImageUrl(jsonObject.getString("imageUrl").replace("640", 
> "80")); 
>                 detail_rec.setActors(jsonObject.getString("actors")); 
>                 detail_rec.setGenre(jsonObject.getString("genre")); 
>   
> detail_rec.setDirector(jsonObject.getString("director")); 
>                 detail_rec.setPlot(jsonObject.getString("plot")); 
>                 detail_rec.setYear(jsonObject.getString("year")); 
>   
> detail_rec.setDuration(jsonObject.getString("duration")); 
>   
> detail_rec.setTrailer(jsonObject.getString("trailer")); 
>                 detail_rec.setRating(jsonObject.getString("rating")); 
>
>                 detail_film = detail_rec; 
>                 publishProgress(detail_rec); 
>
>
>             } 
>             catch (IOException ignored) 
>             { 
>             } 
>             catch (JSONException ignored) 
>             { 
>             } 
>             return null; 
>         } 
>
>         @Override 
>         protected void onProgressUpdate(DetailedRec... values) 
>         { 
>             for (final DetailedRec detail_rec : values) 
>             { 
>
>                 updateViews(detail_rec); 
>
>             } 
>
>         } 
>
>         private  JSONObject getJSONObject(String url) throws 
> IOException, MalformedURLException, JSONException 
>         { 
>             HttpURLConnection conn = (HttpURLConnection) new 
> URL(url).openConnection(); 
>
>
>             InputStream in = conn.getInputStream(); 
>
>             try 
>             { 
>                 StringBuilder sb = new StringBuilder(); 
>                 BufferedReader r = new BufferedReader(new 
> InputStreamReader(new DoneHandlerInputStream(in),"WINDOWS_1252")); 
>                 for (String line = r.readLine(); line != null; line = 
> r.readLine()) 
>                 { 
>                     sb.append(line); 
>                 } 
>                 return new JSONObject(sb.toString()); 
>             } 
>             finally 
>             { 
>                 in.close(); 
>             } 
>         } 
>
>
>         private  JSONObject sendAndGetJSONObject(String url,JSONObject 
> request) throws IOException, MalformedURLException, JSONException 
>         { 
>
>             HttpClient client = new DefaultHttpClient(); 
>   
> HttpConnectionParams.setConnectionTimeout(client.getParams(), 
> 10000); //Timeout Limit 
>             InputStream in=null; 
>             try{ 
>                  HttpPost post = new HttpPost(url); 
>                  StringEntity se = new 
> StringEntity(request.toString()); 
>                  se.setContentType((Header) new 
> BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
>                  post.setEntity(se); 
>                  HttpResponse response = client.execute(post); 
>                  HttpEntity entity = response.getEntity(); 
>                  in = entity.getContent(); 
>                  StringBuilder sb = new StringBuilder(); 
>                  BufferedReader r = new BufferedReader(new 
> InputStreamReader(new DoneHandlerInputStream(in),"WINDOWS_1252")); 
>                  for (String line = r.readLine(); line != null; line = 
> r.readLine()) 
>                  { 
>                     sb.append(line); 
>                  } 
>                  return new JSONObject(sb.toString()); 
>             }catch(Exception e){ 
>
>             } 
>             finally 
>             { 
>                 in.close(); 
>             } 
>
>             return null; 
>
>         } 
>
>
>     } 
>
>     private void updateViews(final DetailedRec detail_rec){ 
>         //FILM 
>         TextView filmName = (TextView) 
> getView().findViewById(R.id.movieTitle); 
>         filmName.setText(detail_rec.getName().trim()); 
>         TextView actors = (TextView) 
> getView().findViewById(R.id.movieActor); 
>         actors.setText(detail_rec.getActors().trim()); 
>         TextView genre = (TextView) 
> getView().findViewById(R.id.movieGenre); 
>         genre.setText(detail_rec.getGenre().trim()); 
>         TextView director = (TextView) 
> getView().findViewById(R.id.movieDirector); 
>         director.setText(detail_rec.getDirector().trim()); 
>         TextView plot = (TextView) 
> getView().findViewById(R.id.moviePlot); 
>         plot.setText(detail_rec.getPlot().trim()); 
>         TextView year = (TextView) 
> getView().findViewById(R.id.movieYear); 
>         year.setText(detail_rec.getYear().trim()); 
>         TextView duration = (TextView) 
> getView().findViewById(R.id.movieDuration); 
>         duration.setText(detail_rec.getDuration().trim()); 
>         ImageView image = (ImageView) 
> getView().findViewById(R.id.moviePoster); 
>
>         new 
> DownloadImagesTask(detail_rec.getImageUrl().trim().replace("80", 
> "100")).execute(image); 
>         // 
> image.setImageBitmap(downloadBitmap(detail_rec.getImageUrl().trim().replace("80",
>  
>
> "100"))); 
>         //Rating 
>         if(detail_rec.getRating().compareTo("N/A")!=0){ 
>             RatingBar rateBar = 
> (RatingBar)getView().findViewById(R.id.MovieRatingBar); 
>   
> rateBar.setRating(Float.parseFloat(detail_rec.getRating())); 
>         } 
>         //Trailer 
>         Button trailer = (Button)getView().findViewById(R.id.trailer); 
>         if(detail_rec.getTrailer().compareTo("")!=0){ 
>             trailer.setVisibility(View.VISIBLE); 
>             trailer.setOnClickListener(new OnClickListener() { 
>
>                 @Override 
>                 public void onClick(View v) { 
>                     int index = detail_rec.getTrailer().indexOf("v="); 
>                     String videoId=""; 
>                     if(index!=-1){ 
>                         videoId = 
> detail_rec.getTrailer().substring(index+2); //"Fee5vbFLYM4"; 
>                         Intent intent = new Intent(Intent.ACTION_VIEW, 
> Uri.parse("vnd.youtube:"+videoId)); 
>                         intent.putExtra("VIDEO_ID", videoId); 
>                         startActivity(intent); 
>                     } 
>
>
>                 } 
>             }); 
>         } 
>
>     } 
>
> } 
>
> Ok, in my application I want the fragment is load one time. I explain 
> so good: When I start the Detail class the fragment classes are 
> istantiate and the AsyncTask in all the 3 fragment start, now when the 
> user switch from one tab to other I want the fragment in tab 
> unselected don't lost the data and the view because now when I switch 
> from one tab to another and then return the first tab this is recreate 
> and the onActivityCreated method is called again! 
>
> Please help me, I search in all place but I dont find solution!! 
>
> P.S. I use the SherlockActionBar, I don't know id it is relevant. 
> Sorry for my bad English 
>
> Thank you 
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to