public class PullToRefreshViewPagerActivity extends Activity implements OnRefreshListener<ViewPager> {
public static ArrayList<Integer> al = new ArrayList<Integer>();
private PullToRefreshViewPager mPullToRefreshViewPager;
private static int[] sDrawables = { R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper,
R.drawable.wallpaper, R.drawable.wallpaper};
SamplePagerAdapter sm ;
ViewPager vp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ptr_viewpager);
mPullToRefreshViewPager = (PullToRefreshViewPager) findViewById(R.id.pull_refresh_viewpager);
mPullToRefreshViewPager.setOnRefreshListener(this);
for(int i =0;i<sDrawables.length;i++){
al.add(sDrawables[i]);
}
sm = new SamplePagerAdapter();
vp = mPullToRefreshViewPager.getRefreshableView();
vp.setAdapter(sm);
vp.setCurrentItem(5);
}
@Override
public void onRefresh(PullToRefreshBase<ViewPager> refreshView) {
new GetDataTask().execute();
}
static class SamplePagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return al.size();
}
@Override
public View instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(container.getContext());
imageView.setImageResource(al.get(position));
// Now just add ImageView to ViewPager and return it
container.addView(imageView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("clicked","clikced");
}
});
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
private class GetDataTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
return "FFF";
}
@Override
protected void onPostExecute(String result) {
vp.setCurrentItem(5,false);
ArrayList<Integer> al2 = new ArrayList<Integer>();
al2.add(R.drawable.a);
al2.add(R.drawable.sharath);
al2.add(R.drawable.car);
al2.add(R.drawable.sa);
al2.add(R.drawable.ba);
al.addAll(0,al2);
sm.notifyDataSetChanged();
Log.e("size",""+al.size());
mPullToRefreshViewPager.onRefreshComplete();
super.onPostExecute(result);
}
}
}
thanks
sarath
No comments:
Post a Comment