I'm trying to create a calendar similar to Google Calendar on Day mode, where you swipe through the days and see the hours
I'm stuck on how to update the date as the user swipes
here's what I have:
ListView Adapter
public class ListViewItems extends ArrayAdapter{
private Activity context;
private ArrayList<String> horarios;
public ListViewItems(Activity c, ArrayList<String> h){
super(c, R.layout.listview_item, h);
this.context = c;
this.horarios = h;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup){
LayoutInflater inflater = context.getLayoutInflater();
View v = inflater.inflate(R.layout.listview_item, null, true);
TextView hor = (TextView)v.findViewById(R.id.horario);
hor.setText(horarios.get(position));
return v;
}
}
EventsFragment where I set the adapter for the Viewpage
public class EventsFragment extends Fragment {
private SharedPreferences.Editor editor;
private SharedPreferences sharedPref;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_events, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
ViewPager vp = (ViewPager)view.findViewById(R.id.viewpager);
SwipeAdapter sp = new SwipeAdapter(getActivity().getSupportFragmentManager());
vp.setAdapter(sp);
vp.setCurrentItem(249);
}
The Swipe Adapter
public class SwipeAdapter extends FragmentStatePagerAdapter {
public SwipeAdapter(FragmentManager fm){
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new CalendarFragment();
return fragment;
}
#Override
public int getCount() {
return 500;
}
}
And my CalendarFragment where I populate the listView with the hours of the day
public class CalendarFragment extends Fragment {
private Integer currentPosition;
public CalendarFragment(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_calendar, container, false);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
TextView text = (TextView)view.findViewById(R.id.day);
Calendar currentDate = Calendar.getInstance();
String day = Integer.toString(currentDate.get(Calendar.DAY_OF_MONTH));
text.setText(day);
ArrayList<String> horariosFixos = new ArrayList<String>();
horariosFixos.add("01:00");
horariosFixos.add("02:00");
horariosFixos.add("03:00");
horariosFixos.add("04:00");
horariosFixos.add("05:00");
horariosFixos.add("06:00");
horariosFixos.add("07:00");
horariosFixos.add("08:00");
horariosFixos.add("09:00");
horariosFixos.add("10:00");
horariosFixos.add("11:00");
horariosFixos.add("12:00");
horariosFixos.add("13:00");
horariosFixos.add("14:00");
horariosFixos.add("15:00");
horariosFixos.add("16:00");
horariosFixos.add("17:00");
horariosFixos.add("18:00");
horariosFixos.add("19:00");
horariosFixos.add("20:00");
horariosFixos.add("21:00");
horariosFixos.add("22:00");
horariosFixos.add("23:00");
horariosFixos.add("00:00");
ListViewItems events = new ListViewItems(getActivity(),horariosFixos);
ListView events_list = (ListView)view.findViewById(R.id.events_list);
events_list.setAdapter(events);
}
It's working, but I just can't figure out a way to update the date.
I tried a few things like storing the position given on SwipeAdapter and add or subtract a day from getInstance() but it didn't seem to work
Any help would be appreciated.
Ok well I don't know if I can answer your question definitively, but I may be able to help shed some light or give you food for thought.
In my code, I set a View Pager Listener. If you can do that in your code I think it would give you the functionality you are looking for.
Below is an example from one of my apps:
public void setViewPagerListener()
{
view_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
#Override
public void onPageSelected(int position)
{
//Log.i(TAG, "position: " + position);
// on changing the page
// make respected tab selected
//actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
//Log.i(TAG, "positionOffset: " + positionOffset);
if (positionOffset >= .7)
{
handler_scrim_alpha.post(setScrimAlphaRunnable(.1f));
}
else if (positionOffset < .7 && positionOffset >= .4)
{
handler_scrim_alpha.post(setScrimAlphaRunnable(.3f));
}
else if (positionOffset < .5 & positionOffset >= .2)
{
handler_scrim_alpha.post(setScrimAlphaRunnable(.5f));
}
else if (positionOffset < .2 && !(positionOffset == 0))
{
handler_scrim_alpha.post(setScrimAlphaRunnable(.7f));
}
}
#Override
public void onPageScrollStateChanged(int scrollState)
{
/*scrollState == 0 when the pager is idle and has finished it's scroll.*/
scroll_state_global = scrollState;
if (scrollState == 0 && view_pager.getCurrentItem() == 1)
{
scroll_state_global = -1;
frame_input_scrim.setVisibility(View.INVISIBLE);
view_pager.setVisibility(View.INVISIBLE);
}
}
});
}
Related
The problem is this:
When you first choose a picture - everything is fine. If you swipe, and press the "back" button, after you open another image then will open last image which will opened.
The correct position is passed through the Bundle. I don’t understand where to look for the error.
public class ViewPagerAdapter extends PagerAdapter {
private LayoutInflater layoutInflater;
public ViewPagerAdapter() {
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.image_fullscreen, container, false);
ImageView imageView = view.findViewById(R.id.image_fScreen);
Hit hit = hits.get(position);
Picasso.get().load(hit.getLargeImageURL())
.into(imageView);
container.addView(view);
return view;
}
#Override
public int getCount() {
return hits.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View) object);
}
}
DialogFragment:
public class FullScreenDialogFragment extends DialogFragment {
private ArrayList<Hit> hits;
private ViewPager viewPager;
private ViewPagerAdapter viewPagerAdapter;
private TextView author;
private TextView count;
private int selectedPosition;
private Button buttonShare;
private static FullScreenDialogFragment fullScreenDialogFragment;
private int rlyPosition;
public static FullScreenDialogFragment getInstance() {
if (fullScreenDialogFragment == null) {
fullScreenDialogFragment = new FullScreenDialogFragment();
}
return fullScreenDialogFragment;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_FRAME, R.style.DialogStyle);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.image_viewpager, container, false);
viewPager = view.findViewById(R.id.viewpager);
author = view.findViewById(R.id.author);
buttonShare = view.findViewById(R.id.btnShare);
count = view.findViewById(R.id.count);
hits = (ArrayList<Hit>) getArguments().getSerializable("hits");
selectedPosition = getArguments().getInt("position");
viewPagerAdapter = new ViewPagerAdapter();
viewPager.setAdapter(viewPagerAdapter);
viewPager.addOnPageChangeListener(onViewPageChangeListener);
setItem(selectedPosition);
return view;
}
private void setItem(int pos) {
viewPager.setCurrentItem(pos, true);
setItemInfo(selectedPosition);
}
private void setItemInfo(int pos) {
count.setText((pos + 1) + " of " + hits.size());
Hit hit = hits.get(pos);
author.setText(hit.getUser());
}
ViewPager.OnPageChangeListener onViewPageChangeListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
setItemInfo(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
};
I Guess the Problem is here
private void setItem(int pos) {
viewPager.setCurrentItem(pos, true);
setItemInfo(selectedPosition);
}
You might be passing the selectedPosition instead of currentPosition
Please check.
My problem was in creating DialogFramgent object.
I did it him in Singlton pattern and called it with old data.
Now in OnClick i'm created new object DialogFramget and him get new values.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fullScreenDialog = new FullScreenDialogFragment();
fullScreenDialog.setArguments(bundle);
fullScreenDialog.show(fragmentTransaction, "");
I am writing a simple view pager test app that loads a bunch of data from a website and creates pages to display them.
For the most part the displaying is pretty much all works. However, it falls apart when I load a new set of data to display. When I get a new set, it loads all the data perfectly well however, instead of starting from view 0 it starts at a random location. It could start at the end or somewhere in the middle.
I want all the data to load from page 1, like a book. If you load a new book the book should start from page 1 not a random page.
Reloading calls
I've tried using GotCampDetails() which sets the local variable in the class.
viewPager.setCurrentItem(0);
in my fragment but it doesn't work. Also called notifydatasetchanged() on my adapter, still didn't work.
What am I missing?
public class ViewPagerFragment extends Fragment implements WebReadResultsListener {
private ArrayList<HashMap<String, String>> campDetails;
CustomPagerAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.camp_viewpager, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
if (campDetails != null && ! campDetails.isEmpty()) {
ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
adapter = new CustomPagerAdapter(getActivity(), campDetails, viewPager);
viewPager.setAdapter(adapter);
viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
}
}
#Override
public boolean GotCampDetails(ArrayList<HashMap<String, String>> campDetails) {
this.campDetails = campDetails;
return true;
}
#Override
public boolean GotCampList(ArrayList<HashMap<String, String>> campDetails) {
return false;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getFragmentManager().putFragment(outState,"fragmentInstanceSaved",getFragmentManager().findFragmentById(R.id.content));
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Handle orientation changes here
}
}
Adapter:
public class CustomPagerAdapter extends PagerAdapter implements ColorChangeListener {
private ArrayList<HashMap<String, String>> campDetails;
private ViewGroup collection;
private Context mContext;
private boolean isRunning = false;
private ColorChangeListener colorChangeListener;
private int color= pink;
private int currentposition;
private ViewPager viewPager;
public CustomPagerAdapter(Context context, final ArrayList<HashMap<String, String>> campDetails, ViewPager viewPager) {
this.campDetails=campDetails;
this.mContext = context;
this.viewPager = viewPager;
colorChangeListener = (ColorChangeListener) mContext;
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int pos) {
currentposition = pos;
if (campDetails.get(currentposition).get("color").equals("green")) {
color = green;
} else {
color = pink;
}
ColorAppBar(color);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public Object instantiateItem(ViewGroup collection, final int position) {
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.child_item, collection, false);
if (campDetails.size() <= 0) {
Toast.makeText(mContext, "Error in getting camp details", Toast.LENGTH_SHORT).show();
return null;
}
if (campDetails.get(position).get("description").equals("RD") ) {
layout.setBackgroundColor(mContext.getResources().getColor(green));
} else {
layout.setBackgroundColor(mContext.getResources().getColor(pink));
}
collection.addView(layout);
this.collection = collection;
TextView eName = (TextView) layout.findViewById(R.id.en);
TextView nEName= (TextView) layout.findViewById(R.id.nen);
TextView textView6 = (TextView) layout.findViewById(R.id.textView6);
// ImageView nextImg = (ImageView) groupLayout.get(index).findViewById(R.id.nextImg);
eName.setText(campDetails.get(position).get("name"));
if (position+1 < campDetails.size()) {
nEName.setText(campDetails.get(position+1).get("name"));
textView6.setVisibility(View.VISIBLE);
} else {
nEName.setText("You are DONE!!");
textView6.setVisibility(View.INVISIBLE);
}
return layout;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
#Override
public int getCount() {
return campDetails.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public CharSequence getPageTitle(int position) {
return campDetails.get(position).get("name");
}
#Override
public void ColorAppBar(int color) {
colorChangeListener.ColorAppBar(color);
}
}
I finally managed to get it done.
I had to delay the function call setCurrentItem by a few milliseconds. Race condition? I don't know why this is the case but now it works perfectly.
Here is what I added in case someone else may find it useful:
pager.postDelayed(new Runnable() {
#Override
public void run() {
pager.setCurrentItem(pos);
}
}, 100);
I'm having trouble following the details well enough to pinpoint the problem, but this guy seems to have accomplished what you're trying to do: dynamically add and remove view to viewpager
hope it helps
I've got a problem with gridview. I'm using custom adapter and custom view for every item (months) in the adapter.
http://cs622428.vk.me/v622428086/3f452/K30qwCnc-iQ.jpg
When I go to another fragment and then press back, the gridview disappears.
http://cs622428.vk.me/v622428086/3f45c/2sbbchFTYgM.jpg
=================================================================
Sorry for tangled code. There was only 2 fragments when I faced the problem but then I decided to try to replace a whole fragment every time I need to update something. But still it doesn't work even when I call onResume in the first activity and replace everything...
So how can I refresh it ? It does not work:
#Override
public void onResume() {
super.onResume();
gridViewAdapter = new GridViewAdapter(getActivity(), currentYear.getMonths());
gridView.setAdapter(gridViewAdapter);
gridViewAdapter.notifyDataSetChanged();
gridView.setOnItemClickListener(this);
}
Adapter:
public class GridViewAdapter extends ArrayAdapter<Month> {
private Context context;
private ArrayList<Month> months;
public GridViewAdapter(Context context, ArrayList<Month> months) {
super(context, 0, months);
this.context = context;
this.months = months;
}
public ArrayList<Month> getData() {
return months;
}
#Override
public int getCount() {
return months.size();
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = new DrawSingleMonthView(context, months.get(i));
return view;
//return new DrawSingleMonthView(context, months.get(i));
}
Flagment - controller. There I call single month (double click) or all months.
public class CalendarFragment extends Fragment implements OnClickCalendarListener {
private FragmentTransaction fTrans;
private Year year;
private Year secondYear;
private Year thirdYear;
private Year currentYear;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Calendar calendar = GregorianCalendar.getInstance();
year = new Year(calendar.get(Calendar.YEAR));
secondYear = new Year(calendar.get(Calendar.YEAR) + 1);
thirdYear = new Year(calendar.get(Calendar.YEAR) + 2);
currentYear = year;
View v = inflater.inflate(R.layout.fragment_calendar_container, container, false);
return v;
}
#Override
public void onResume() {
super.onResume();
fTrans = getFragmentManager().beginTransaction();
AllMonthsFragment monthsFragment = new AllMonthsFragment(year, this);
fTrans.replace(R.id.fragment_calendar_container, monthsFragment);
fTrans.commit();
MonthFragment.addListener(this);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void onDoubleCalendarClick(int position) {
fTrans = getFragmentManager().beginTransaction();
SingleMonthFragment calendar = new SingleMonthFragment(currentYear.getMonths().get(position));
fTrans.replace(R.id.fragment_calendar_container, calendar);
fTrans.addToBackStack(null);
fTrans.commit();
}
This is the fragment where i replace all months on FrameLayout.
public class AllMonthsFragment extends Fragment implements View.OnClickListener{
private TextView yearTW;
private View v;
private FragmentTransaction fTrans;
private FrameLayout frameLayout;
private Year year;
private boolean flag;
CalendarFragment calendarFragment;
public AllMonthsFragment(Year year) {
this.year = year;
flag = false;
}
public AllMonthsFragment(Year year, CalendarFragment calendarFragment) {
this.year = year;
this.calendarFragment = calendarFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.calendar_all_month, container, false);
return v;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
yearTW = (TextView) v.findViewById(R.id.yearTW);
yearTW.setText("" + year.getNumberOfYear());
yearTW.setOnClickListener(this);
frameLayout = (FrameLayout) v.findViewById(R.id.container_months);
}
private void openCalendarMonthFragment () {
fTrans = getFragmentManager().beginTransaction();
MonthFragment monthFragment = new MonthFragment(year.getMonths());
fTrans.replace(R.id.container_months, monthFragment);
fTrans.commit();
}
#Override
public void onPause() {
super.onPause();
flag = true;
}
#Override
public void onResume() {
super.onResume();
if(flag) {
calendarFragment.onResume();
}
openCalendarMonthFragment();
}
#Override
public void onClick(View view) {
}
A fragment with gridlayout.
public class MonthFragment extends Fragment implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
private FragmentTransaction fTrans;
private View v;
private GridView gridView;
private GridViewAdapter gridViewAdapter;
private static final long DOUBLE_CLICK_TIME_DELTA = 300;//milliseconds
private long lastClickTime = 0;
private ArrayList<Month> months;
private static List<OnClickCalendarListener> listeners;
MonthFragment (ArrayList<Month> months) {
this.months = months;
}
static {
listeners = new ArrayList<>();
}
#Override
public void onPause() {
super.onPause();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_calendar_all_month, container, false);
gridView = (GridView) v.findViewById(R.id.gridView);
adjustGridView();
gridViewAdapter = new GridViewAdapter(getActivity(), months);
gridView.setAdapter(gridViewAdapter);
gridView.setOnItemClickListener(this);
gridView.setOnItemLongClickListener(this);
return v;
}
public static void addListener(OnClickCalendarListener onClickCalendarListener) {
listeners.add(onClickCalendarListener);
}
private static void notifyDoubleClick(int i) {
for (OnClickCalendarListener onClickCalendarListener : listeners)
onClickCalendarListener.onDoubleCalendarClick(i);
}
private void adjustGridView() {
gridView.setVerticalScrollBarEnabled(false);
}
private void onSingleClick(View v, int i) {
if (months.get(i).isSelected())
months.get(i).setSelected(false);
else months.get(i).setSelected(true);
v.invalidate();
}
private void onDoubleClick(int i) {
notifyDoubleClick(i);
}
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
onDoubleClick(i);
return false;
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
long clickTime = System.currentTimeMillis();
if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA) {
onDoubleClick(i);
} else {
onSingleClick(v, i);
}
lastClickTime = clickTime;
view.invalidate();
User runOnUi Thread..!!! in setOnItemClickListner..!!
runOnUiThread(new Runnable() {
#Override
public void run() {
gridViewAdapter.notifyDataSetChanged();
}
});
Hope this help.!
Hi i have a view pager that loads fragments and i'm wanting to add a bit of animation to an ImageView make it move up as the ViewPager scrolls. problem i'm having is that the viewPager creates the views for the first two pages so the animation doesnt happen on those pages. Is there is a way to detect if viewpager is scrolling or if the page has changed?
heres what i have tried so far - heres my viewPagerAdapter
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(page == 0){
view = inflater.inflate(R.layout.layout_tutorial, container, false);
}else if(page == 1){
view = inflater.inflate(R.layout.layout_tutorial, container, false);
tutImage = (ImageView)view.findViewById(R.id.tutImage);
int imageResource = getResources().getIdentifier("tut_view_2", "drawable", context.getPackageName());
Drawable tutImageDrawable = getResources().getDrawable(imageResource);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
tutImage.setBackground(tutImageDrawable);
}else{
tutImage.setBackgroundDrawable(tutImageDrawable);
}
}else if(page == 2){
view = inflater.inflate(R.layout.layout_tutorial, container, false);
tutImage = (ImageView)view.findViewById(R.id.tutImage);
int imageResource = getResources().getIdentifier("tut_view_3", "drawable", context.getPackageName());
Drawable tutImageDrawable = getResources().getDrawable(imageResource);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
tutImage.setBackground(tutImageDrawable);
}else{
tutImage.setBackgroundDrawable(tutImageDrawable);
}
heres my viewpager
public static class MyPagerAdapter extends FragmentPagerAdapter {
private Context context;
public MyPagerAdapter(android.support.v4.app.FragmentManager fragmentManager,Context c) {
super(fragmentManager);
context = c;
}
#Override
public int getCount() {
int levelCount = 4;
return levelCount;
}
// Returns the fragment to display for that page
#Override
public TutorialAdapter getItem(int position) {
String data = null;
return TutorialAdapter.newInstance(position, data, context);
}
#Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}
}
You need to use a OnPageChangeListener, set it to your viewpager:
viewPager.setOnPageChangeListener(myOnPageChangeListener);
The OnPageChangeListener implements some methods like:
#Override
public void onPageScrollStateChanged(int state) {}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
#Override
public void onPageSelected(int position) {}
They can help you. Check this for more information: ViewPager.OnPageChangeListener!
I have a CarsFragment which is inculde cars. One page is one trade (Ferrari, Mercedes) and on every page has a list with models of actual trade.
The main view:
public class CarsFragment extends Fragment {
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.cars_fragment, container, false);
pager = (ViewPager) view.findViewById(R.id.carsPager);
adapter = new CarsPagerAdapter(getChildFragmentManager());
pager.setAdapter(adapter);
return view;
}
}
Adapter:
public class CarsPagerAdapter extends FragmentStatePagerAdapter {
private final FragmentManager fm;
private String[] cars = new String[]{"Ferrari","Mercedes"};
public MatchesPageAdapter(final FragmentManager fm) {
super(fm);
this.fm = fm;
}
#Override
public int getCount() {
return cars.length;
}
#Override
public Fragment getItem(final int position) {
return FragmentModelList.newInstance(cars[position]);
}
#Override
public void destroyItem(final ViewGroup container, final int position, final Object object) {
super.destroyItem(container, position, object);
}
}
List:
public class FragmentModelList extends Fragment {
public static FragmentModelList newInstance(String trade) {
FragmentModelList fragmentModelList = new FragmentModelList();
Bundle args = new Bundle();
args.putString("trade", trade);
fragmentModelList.setArguments(args);
return fragmentModelList;
}
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_matches_list_view, container, false);
list = (ListView) view.findViewById(R.id.modelList);
return view;
}
#Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
modelListAdapter = new ModelListAdapter(getActivity());
list.setAdapter(modelListAdapter);
loadModels();
}
private void loadModels() {
new AsyncTask<Void, Void, Boolean>() {
#Override
protected Boolean doInBackground(final Void... params) {
try {
List<Model> result = Downloader.getModelsByName(getArguments().getString("trade"));
modelListAdapter.setData(result);
return true;
} catch (Exception ex) {
return false;
}
}
#Override
protected void onPostExecute(final Boolean success) {
if (!success) {
// Show error
} else {
// Update list
modelListAdapter.notifyDataSetChanged();
}
}
}.execute();
}
}
and the modelListAdapter set row where one row inculde 2 image and some textview.
And the problem:
when i swiping on the viewpager it isn't smooth :( a loged it and when created the next or previous page (depends on swipe direction) the animation will be bad, is lagging.
What is the solution?
(SGS4 android 4.2.2 - where is the project butter :(( )