I need a little help to understand how can I change my current implementation of ViewFlow to android v4.ViewPager. Till now I was populating my views from ArayLists<?> with data. Now I need to use the same way I was doing it (if it’s possible) and use ViewPager from Android SDK. Any suggestions would be great to understand how it’s working, because the examples that I found over internet are using tabs and etc. but I don’t need tabs.
Here is how I was using my adapter before :
public class WebViewAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<String> name;
private ArrayList<String> date;
private ArrayList<String> bodies;
private static LayoutInflater inflater = null;
public WebViewAdapter(Activity a, ArrayList<String> names, ArrayList<String> dates, ArrayList<String> body) {
this.activity = a;
this.name = names;
this.date = dates;
this.bodies = body;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return name.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView title, date;
public WebView body;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.article_layout, null);
holder = new ViewHolder();
holder.title = (TextView) vi.findViewById(R.id.article_title);
holder.date = (TextView) vi.findViewById(R.id.date);
holder.body = (WebView) vi.findViewById(R.id.body);
vi.setTag(holder);
} else
holder = (ViewHolder) vi.getTag();
holder.title.setText(name.get(position).toString());
holder.date.setText(date.get(position).toString());
holder.body.loadDataWithBaseURL(null, bodies.get(position).toString(), "text/html", "utf-8", null);
return vi;
}
}
And I was using it like this :
WebViewAdapter adapter = new WebViewAdapter(this, titles, dates, bodies);
viewFlow.setAdapter(adapter, pos);
Now I need the same result with the ViewPager implementation, because I am moving all my project to use Fragments and stuff from the new SDK API.
Any advices / suggestions / help how can I achieve this?
Instead of your adapter implement FragmentPagerAdapter. Invflate ViewPager as you did with ViewFlow and set the created adapter on it.
Everything is pretty the same. The only difference – An adapter instatiates fragements and the fragments inflates views.