First of all, I apologize for my English, as this is not very good. Thank you There are several ListWiews with adapters in my application. The data for ListViews is taken from a parsed XML file located on the Internet. Now I have one TabHost with three tabs and different ListViews on each tab. I want to change TabHost to a new and minor method - the ViewPager indicator by Jake Wharton (https://github.com/JakeWharton/Android-ViewPagerIndicator), because there is a slippery effect.
I tried to include this code in a sample project and it works well. At the moment, I have a ListView in one ViewPager fragment and a simple TextView in two other fragments. He works.
ViewPagerAdapter code is shown below:
package com.formatbce.pager; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.content.Context; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import com.viewpagerindicator.TitleProvider; public class ViewPagerAdapter extends PagerAdapter implements TitleProvider { private int[] scrollPosition = new int[titles.length]; private static String[] titles = new String[] { "News", "Bio", "Music" }; private final Context context; public ViewPagerAdapter( Context context ) { this.context = context; for ( int i = 0; i < titles.length; i++ ) { scrollPosition[i] = 0; } } @Override public String getTitle( int position ) { return titles[ position ]; } @Override public int getCount() { return titles.length; } @Override public Object instantiateItem( View pager, final int position ) { if (position == 0) { ListView v = new ListView( context ); String[] from = new String[] { "str" }; int[] to = new int[] { android.R.id.text1 }; List<Map<String, String>> items = new ArrayList<Map<String, String>>(); for ( int i = 0; i < 20; i++ ) { Map<String, String> map = new HashMap<String, String>(); map.put( "str", String.format( "Item %d", i + 1 ) ); items.add( map ); } SimpleAdapter adapter = new SimpleAdapter( context, items, android.R.layout.simple_list_item_1, from, to ); v.setAdapter( adapter ); ( (ViewPager) pager ).addView( v, 0 ); v.setOnScrollListener( new OnScrollListener() { @Override public void onScroll( AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount ) { scrollPosition[ position ] = firstVisibleItem; } @Override public void onScrollStateChanged(AbsListView view, int scrollState) {
So, with the ListView sample, everything is fine.
But my problems begin when I try to add my custom ListView there. In my old project, this ListView has the code:
package com.formatbce.mdrive; import java.util.ArrayList; import android.app.AlertDialog; import android.app.ListActivity; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.ListView; public class Concerts extends ListActivity { private ArrayList<Rss_PostItem> messages; private Rss_PostAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState != null) { messages = savedInstanceState.getParcelableArrayList("messages"); adapter = new Rss_PostAdapter(Concerts.this, R.layout.post_entry, messages); setListAdapter(adapter); } else new GetParserResult().execute(); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Intent postActivity = new Intent(this, Rss_PostActivity.class); Bundle store = new Bundle(); store.putString("title", messages.get(position).title); store.putString("description", messages.get(position).description); store.putString("link", messages.get(position).link); postActivity.putExtras(store); startActivity(postActivity); } protected void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); savedInstanceState.putParcelableArrayList("messages", messages); } private class GetParserResult extends AsyncTask<Context, Integer, ArrayList<Rss_PostItem>> { private ProgressDialog loadingDialog; private Concerts_Parser parser; protected void onPreExecute() { parser = new Concerts_Parser(); loadingDialog = ProgressDialog.show(Concerts.this, "", "Loading concerts list...", true); } @Override protected ArrayList<Rss_PostItem> doInBackground(Context... arg0) { return parser.parse(); } protected void onPostExecute(ArrayList<Rss_PostItem> result) { if (result == null) { AlertDialog.Builder builder = new AlertDialog.Builder( Concerts.this); builder.setMessage( "Cannot load list... \nIs Internet ON?") .setCancelable(false) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); loadingDialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); } else { messages = result; loadingDialog.dismiss(); adapter = new Rss_PostAdapter(Concerts.this, R.layout.post_entry, messages); setListAdapter(adapter); } } } }
Therefore, I do not understand how to correctly place the ViewPager code. I tried setting ListFragment, but for me it is quite new, so I can not get it to work.
Google cannot help me, just like in various developer forums.
Truly, the question is how to get a custom ListView to be displayed in the ViewPager?
Thank you, I hope you help.
UPD:
Thanks to the first answer, I tried to convert one of my actions into a fragment using this lesson: http://www.e-nature.ch/tech/?p=55 .
Firstly, it was one mistake that I could not pass: this variable is not converted, and I do not know how to make it work.
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Now one more thing: when I tried to put the Fragment result in the FragmentPagerAdapter - I found the correct code here:
@Override public Fragment getItem(int arg0) {
Please tell me what should this method return? Thanks!