i am implementing an app in which i firstly load images into viewpager. I need to replace the images and get webview after it loads completely. how can i do it.
My viewpager code is as follows:
public class HtmlPagerAdapter extends PagerAdapter {
private ArrayList<LinearLayout> views;
Context con;
public HtmlPagerAdapter(Context context, ArrayList<LinearLayout> filelist) {
this.con = context;
views = new ArrayList<LinearLayout>();
this.views = filelist;
}
#Override
public void destroyItem(View view, int arg1, Object object) {
((ViewPager) view).removeView((LinearLayout) object);
}
#Override
public void finishUpdate(View arg0) {
}
#Override
public int getCount() {
return views.size();
}
#Override
public Object instantiateItem(View view, int position) {
View myView = views.get(position);
((ViewPager) view).addView(myView);
return myView;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void startUpdate(View arg0) {
}
}
my imageview creation code is as like as follows:
for(int t=0;t<list.length;t++){
ImageView img=new ImageView(con); // con is context
img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
line = new LinearLayout(con);
img.setBackgroundResource(drawable.bg_downissue);
line.addView(img);
filelist.add(line); //filelist is arraylist<linearlayout>
}
adapter = new HtmlPagerAdapter(con, filelist);
viewPager.setAdapter(adapter);
My webview creation is also a dynamic webview like
for(int t=0;t<list.length;t++){
WebView web=new WebView(this);
web.loadDataWithBaseURL(myUrl);
}
i want to place this webview into the imageview in viewpager. Please help me. I am really stuck on this part.
I would suggest you to change quite a bit of those code:
change the ImageView and WebView creation to a XML layout, put both inside a FrameLayout with match_parent.
On the WebView you'll attach a WebViewClient and override the onPageFinished to know when the page finished loading.
Then it's just a matter of inflating the XML and make the WebView invisible; then whenever the WebViewClient notifies that it finished loading you set the WebView visible and the ImageView invisible.
Related
i implemented a viewpager in my activity that on scroll, loads a set of data to an adapter and display them in a list view. everything is working fine, but i cant seem to find out how to make it more efficient because when scrolling, the view freezes but for example in the GMAIL app when scrolling between Emails the scrolling is so smooth.
Below is the code i am using:
Activity:
in oncreate
pageradapter adapter2 = new pageradapter(this, ArrayOfItems); //ArrayOfItems is an Array containing the data that will be displayed
ViewPager myPager = (ViewPager) findViewById(R.id.mypanelpager);
myPager.setAdapter(adapter2);
pagechangelistener pageListener = new pagechangelistener();
myPager.setOnPageChangeListener(pageListener);
pageradapter:
public pageradapter(Activity act, ArrayList <List<Item>>Arrayitems) {
this.Arrayitems = Arrayitems;
activity = act;
}
public int getCount() {
return Arrayitems.size();
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
List<Item> item = Arrayitems.get(position);
ListView view = new ListView(activity);
taskdetailsarrayadaptertest adapter = new taskdetailsarrayadaptertest(activity, item);
view.setAdapter(adapter);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
((ViewPager) container).addView(view, 0);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
pagechangelistener:
#Override
public void onPageSelected(int position)
{
currentPage = position;
int positionTemp = position + 1;
}
public final int getCurrentPage() {
return currentPage;
}
First you need to use holders like this.
in your ViewPager:
class Holder{
ListView listView;
Adapter adapter;
}
private ArrayList<Holder> mHolders;
//init holders
#Override
public Object instantiateItem(ViewGroup collection, int position) {
Holder holder = mHolders.get(position);
holder.listView.setAdapter(holder.adapter);
collection.addView(holder.listView);
return holder.listView;
}
Second try async loading data in listview
set myPager.setOffscreenPageLimit(1);
I have a ViewPager in my activity. The ViewPager shows different values on swiping the pager. But I get an IllegalStateException:
PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged!
Expected adapter item count: 0, found: 20
Pager id: com.exalt.vts:id/summarypager Pager class: class android.support.v4.view.ViewPager
Problematic adapter: class com.exalt.vts.DashBoard$MyPagerAdapter
This is my source code for PagerAdapter:
public class SamplePagerAdapter extends PagerAdapter{
List<View> pages = null;
public SamplePagerAdapter(List<View> pages){
this.pages = pages;
}
#Override
public Object instantiateItem(View collection, int position){
View v = pages.get(position);
((ViewPager) collection).addView(v, 0);
return v;
}
#Override
public void destroyItem(View collection, int position, Object view){
((ViewPager) collection).removeView((View) view);
}
#Override
public int getCount(){
return pages.size();
}
#Override
public boolean isViewFromObject(View view, Object object){
return view.equals(object);
}
#Override
public void finishUpdate(View arg0){
}
#Override
public void restoreState(Parcelable arg0, ClassLoader arg1){
}
#Override
public Parcelable saveState(){
return null;
}
#Override
public void startUpdate(View arg0){
}
}
This is the MainActivity:
public class MainActivity extends Activity {
ViewPager viewPager;
SamplePagerAdapter myPagerAdapter;
private TextView textView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
List<View> pages = new ArrayList<View>();
viewPager = (ViewPager)findViewById(R.id.myviewpager);
myPagerAdapter = new SamplePagerAdapter(pages);
viewPager.setAdapter(myPagerAdapter);
viewPager.setCurrentItem(1);
LayoutInflater inflater = LayoutInflater.from(this);
View page = inflater.inflate(R.layout.mylayout, null);
for (int i = 0; i < 20; i++) {
page = inflater.inflate(R.layout.mylayout, null);
textView = (TextView) page.findViewById(R.id.text_view);
textView.setText("Страница "+i);
pages.add(page);
}
}
I try to add 20 views in my ViewPager but I get an exception.
You either need to move the following code from where it is currently to after your for loop, so that the adapter is set after your data set is setup
myPagerAdapter = new SamplePagerAdapter(pages);
viewPager.setAdapter(myPagerAdapter);
viewPager.setCurrentItem(1);
or alternatively, you can add the following after your for loop to notify the adapter that the data set has changed since you set it up
myPagerAdapter.notifyDataSetChanged();
I build a project. but I'm having problems.
My project has page A , Page B
from Picture http://s20.postimg.org/5pupjbuyl/untitled.png
Page A is FrameLayout (id=layout) , in FrameLayout has LinearLayout and FrameLayout (id=content)
Page B is FrameLayout (id=pager) , in FrameLayout has Viewpager
I would take Framelayout (id=content) in page A containing FrameLayout(id = pager) in page B
(Notice : I try to minimize my code because It many line)
PageIndicatorActivity .java
public class PageIndicatorActivity extends Activity {
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
..
..
ViewGroup Content = (ViewGroup)findViewById(R.id.layout);
ViewBookAdapter pager = new ViewBookAdapter(this);
Content.addView(pager.getView());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main,menu);
return true;
}}
pager.java
public class Pager extends PagerAdapter {
Activity activity;
private static String content[] = {"file:///android_asset/html/page1.html","file:///android_asset/html/page2.html"};
public Pager(Activity act) {
activity = act;
}
public int getCount() {
return content.length;
}
public Object instantiateItem(View collection, int position) {
WebView view = new WebView(activity);
view.loadUrl(content[position]);
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
((ViewPager) collection).addView(view, 0);
return view;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}}
and ViewBookAdapter.java
class ViewBookAdapter {
Activity activity;
private LayoutInflater minflate;
private ViewPager myPager;
public ViewBookAdapter(Activity act){
activity = act;
myPager = (ViewPager)act.findViewById(R.id.viewPager);
Pager p = new Pager(act);
myPager.setAdapter(p); // <--- THIS PROBLEM ---
}
public View getView(){
LayoutInflater minflate = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = minflate.inflate(R.layout.pager, null);
return v;
}}
I have a probleam at myPager.setAdapter(p);
Android Report Bug is NullPointerException
How to solve?
THANK YOU FOR ANSWER ^^
PS. sorry english .
This is happening because your Activity does not have the ViewPager in its layout. Its in the other layout. You will need to do setAdapter in getView()
Your current PageIndicatorActivity layout does not have a ViewPager. If you need to continue this way you have to move to Activity(Page B) and follow your implementation.
I had do the following code, but the views that offscreen seems does not been recycle appropriately.
assume I have all 60 views to let ViewPager to show, it's display correctly, but it doesn't reuse the views, while I swipe to the tail of ArrayList, the views in preceding still not recycle (I guess it because of I swipe to the rightmost, then swipe to the left again, no sign of reloading)
I've search a lot articles, but all alike what I did below, not sure where's the point.
(here's what I actually did)
public class MyPagerAdapter extends PagerAdapter{
ArrayList<View> viewList = new ArrayList<View>();
public MyPagerAdapter(ArrayList<View> list) {
this.viewList = list;
}
#Override
public int getCount() {
return viewList.size();
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View view = viewList.get(position);
container.addView(view);
return view;
}
public void destroyItem(ViewGroup container, int position, Object view) {
container.removeView((View)view);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == (View)object;
}
#Override
public void finishUpdate(View arg0) {
}
#Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void startUpdate(View arg0) {
}
}
There are two adapters i.e. PagerAdapter & FragmentStatePagerAdapter.
PagerAdapter saves pages/views in stack and is used where total number of pages used are lesser in number.
On the other hand if the number of pages/views are more then it is recommended to use FragmentStatePagerAdapter because it does not save save views/pages in stack but creates them during runtime. FragmentStatePagerAdapter Recycles your views/pages.
I recommend to use FragmentStatePagerAdapter.
For example: I have 2 pages in PageAdapter. When I go to second page, I click on button "Lock" and horizontal scrolling is off. I click "Unlock" and again can scrolling. How block scrolling ?
Code:
public class slide extends PagerAdapter
{
List<View> pages = null; // empty list views
public slide (List<View> page)
{
this.pages = page;
}
#Override
public int getCount()
{
return pages.size();
}
#Override
public boolean isViewFromObject(View view, Object obj)
{
return view.equals(obj);
}
#Override
public Object instantiateItem (View views, int position)
{
View v = pages.get(position);
((ViewPager) views).addView(v, 0);
return v;
}
#Override
public void destroyItem(View views, int position, Object view)
{
((ViewPager) views).removeView((View) view);
}
#Override
public void finishUpdate(View view)
{
}
#Override
public void restoreState(Parcelable arg0, ClassLoader arg1)
{
}
#Override
public Parcelable saveState()
{
return null;
}
#Override
public void startUpdate(View view)
{
}
In Activity:
List<View> pages = new ArrayList<View>();
View page = inflater.inflate(R.layout.page, null);
TextView textView = (TextView) page.findViewById(R.id.text_view);
textView.setText("Page 1"); // first page
pages.add(page);
page = new surfaceControl(this); // second page with some modul
pages.add(page);
slide pageAdapter = new slide(pages);
ViewPager viewPager = new ViewPager(this);
viewPager.setAdapter(pageAdapter);
viewPager.setCurrentItem(0);