Currently I am fetching result in JSON format in ListActivity and adding them in 4 ArrayList. I have set ViewPagers for four different pages. My layout is as:
<?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:background="@drawable/bg_gradient"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_gradient" />
</LinearLayout>
I have array lists as
ArrayList<HashMap<String, String>> itemKB,itemMU,itemMI,itemRS;
onCreate method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_items);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new CustomPagerAdapter(this));
itemKB = new ArrayList<HashMap<String, String>>();
itemMU = new ArrayList<HashMap<String, String>>();
itemMI = new ArrayList<HashMap<String, String>>();
itemRS = new ArrayList<HashMap<String, String>>();
new LoadItems().execute();
}
PostExecute of the Class that fetches the result is as:
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter1 = new SimpleAdapter(
FetchItems.this, itemMI, R.layout.menu_kacha_bazaar, new String[] {TAG_ITEM_NAME,TAG_CATEGORY},new int[]
{ R.id.itemName,
R.id.category
});
ListAdapter adapter2 = new SimpleAdapter(
FetchItems.this, itemMU, R.layout.menu_mudi, new String[] {TAG_ITEM_NAME,TAG_CATEGORY},new int[]
{ R.id.itemName,
R.id.category
});
ListAdapter adapter3 = new SimpleAdapter(
FetchItems.this, itemKB, R.layout.menu_mishti, new String[] {TAG_ITEM_NAME,TAG_CATEGORY},new int[]
{ R.id.itemName,
R.id.category
});
ListAdapter adapter4 = new SimpleAdapter(
FetchItems.this, itemRS, R.layout.menu_rannar_sarjam, new String[] {TAG_ITEM_NAME,TAG_CATEGORY},new int[]
{ R.id.itemName,
R.id.category
});
setListAdapter(adapter1);
//setListAdapter(adapter2);
//setListAdapter(adapter3);
//setListAdapter(adapter4);
}
});
}
Now the problem is if I don't call the class that fetches the result, the view pager works fine, I can swipe across the pages.
But when I call the class that fetches the result,the view pager doesn't work.
I mean if I call all 4 setListAdapter,
setListAdapter(adapter1);
setListAdapter(adapter2);
setListAdapter(adapter3);
setListAdapter(adapter4);
It displays the fourth page only and I am unable to swipe to any pages.
If I call just one setListAdapter,
setListAdapter(adapter1);
//setListAdapter(adapter2);
//setListAdapter(adapter3);
//setListAdapter(adapter4);
I can see only the first page and again I can't swipe across pages.
Googled for this and found it can be done using Fragment, but I don't want more code changes.Can anyone help me out fixing this.
My CustomPagerAdapter is as:
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class CustomPagerAdapter extends PagerAdapter {
private Context mContext;
public CustomPagerAdapter(Context context) {
mContext = context;
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(customPagerEnum.getLayoutResId(), collection, false);
collection.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
@Override
public int getCount() {
return CustomPagerEnum.values().length;
}
@Override
public CharSequence getPageTitle(int position) {
CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position];
return mContext.getString(customPagerEnum.getTitleResId());
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
Edit 1: Adding Layout code: Those layouts are as:
`<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5fb0c9"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="40dp"
android:background="#fff"
android:orientation="vertical"
android:padding="20dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="30dp" >
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="50dp"
android:textSize="17dip"
android:text="Kacha Bazaar"
android:textStyle="bold" />
<TextView
android:id="@+id/itemName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="50dp"
android:textSize="17dip"
android:textStyle="bold" />
<TextView
android:id="@+id/category"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="50dp"
android:paddingTop="30dp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>`