Issue
Im having troubles adding Actionbar Items to control my Viewpager screens, I want to be able to control the viewpager depending on which page is pulled up. I really just have 3 webviews i want to add actionbar items to control the webview via the Actionbar (Refresh, Back, Forward). Thank you in advance for any help you provide. Thanks
Code
public class AwesomePagerActivity extends SherlockActivity {
private ViewPager awesomePager;
private static int NUM_AWESOME_VIEWS = 3;
private Context context;
private AwesomePagerAdapter awesomeAdapter;
LayoutInflater inflater ;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up the action bar.
getSupportActionBar().show();
context = this;
inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
awesomeAdapter = new AwesomePagerAdapter();
awesomePager = (ViewPager) findViewById(R.id.pager);
awesomePager.setOffscreenPageLimit(3);
awesomePager.setAdapter(awesomeAdapter);
//Title Page
//Get a reference to the TitlePageIndicator
TitlePageIndicator awesomeIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
// Attach adapter to ViewPager
awesomePager.setAdapter(awesomeAdapter);
// Attach ViewPager to the indicator
awesomeIndicator.setViewPager(awesomePager);
}
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//super.onCreateOptionsMenu(menu);
//getMenuInflater().inflate(R.menu.activity_enlighten_webview_secondary, menu);
MenuInflater Inflater=getSupportMenuInflater();
Inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
WebView webView = (WebView) awesomePager.getChildAt(awesomePager.getCurrentItem());
switch (item.getItemId())
{
case R.id.menu_back:
webView.goBack();
Toast.makeText(AwesomePagerActivity.this, "Now ", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
private class AwesomePagerAdapter extends PagerAdapter{
private String[] titles = new String[]{"Dashboard", "Community", "Support"};
public int getCount() {
return NUM_AWESOME_VIEWS;
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position % titles.length];
}
public Object instantiateItem(View collection, int position) {
WebView web = new WebView(AwesomePagerActivity.this);
View layout = null;
switch (position) {
case 0: {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = mInflater.inflate(R.layout.activity_main, null);
// changed from WebView web = (WebView)layout.findViewById(R.id.webView1);
layout.findViewById(R.id.webView);
WebSettings websettings = web.getSettings();
websettings.setBuiltInZoomControls(false);
websettings.setUseWideViewPort(true);
websettings.setJavaScriptEnabled(true);
websettings.setAllowFileAccess(true);
websettings.setDomStorageEnabled(true);
websettings.setLoadWithOverviewMode(true);
websettings.setSavePassword(true);
web.requestFocusFromTouch();
web.setWebViewClient(new WebViewClient());
web.setWebChromeClient(new WebChromeClient());
web.loadUrl("https://google.com");
}
break;
case 1: { LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = mInflater.inflate(R.layout.activity_community, null);
// WebView web = (WebView)layout.findViewById(R.id.webView2);
WebSettings websettings = web.getSettings();
websettings.setBuiltInZoomControls(true);
websettings.setUseWideViewPort(true);
websettings.setJavaScriptEnabled(true);
websettings.setAllowFileAccess(true);
websettings.setDomStorageEnabled(true);
websettings.setLoadWithOverviewMode(true);
websettings.setSavePassword(true);
web.requestFocusFromTouch();
web.setWebViewClient(new WebViewClient());
web.setWebChromeClient(new WebChromeClient());
web.loadUrl("http://developer.android.com");
}
break;
case 2: { LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = mInflater.inflate(R.layout.activity_support, null);
// WebView web = (WebView)layout.findViewById(R.id.webView3);
WebSettings websettings = web.getSettings();
websettings.setBuiltInZoomControls(true);
websettings.setUseWideViewPort(false);
websettings.setJavaScriptEnabled(true);
websettings.setAllowFileAccess(true);
websettings.setDomStorageEnabled(true);
websettings.setLoadWithOverviewMode(true);
websettings.setSavePassword(true);
web.requestFocusFromTouch();
web.setWebViewClient(new WebViewClient());
web.setWebChromeClient(new WebChromeClient());
web.loadUrl("http://www.xda-developers.com");
}
break;
}
((ViewPager) collection).addView((View)layout,0);
return layout;
}
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
public boolean isViewFromObject(View view, Object object) {
return view==((View)object);
}
public void finishUpdate(View arg0) {}
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
public Parcelable saveState() {
return null;
}
public void startUpdate(View arg0) {}
}
}
Updated code per recommendations
Solution
I've rewritten your initial code into something that should do what you want:
public class AwesomePagerActivity extends Activity {
private ViewPager awesomePager;
private static int NUM_AWESOME_VIEWS = 3;
private Context context;
private AwesomePagerAdapter awesomeAdapter;
LayoutInflater inflater;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
awesomeAdapter = new AwesomePagerAdapter();
awesomePager = (ViewPager) findViewById(R.id.pager);
awesomePager.setOffscreenPageLimit(3);
awesomePager.setAdapter(awesomeAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater Inflater = getSupportMenuInflater();
Inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem Item) {
final int current = awesomePager.getCurrentItem();
int currentId = current == 0 ? R.id.webView1
: (current == 1 ? R.id.webView2 : R.id.webView3);
WebView web = (WebView) findViewById(currentId);
switch (Item.getItemId()) {
case R.id.menu_back:
web.goBack();
return true;
case R.id.menu_forward:
web.goForward();
return true;
case R.id.menu_refresh:
web.reload();
return true;
}
return super.onOptionsItemSelected(Item);
}
private class AwesomePagerAdapter extends PagerAdapter {
public int getCount() {
return NUM_AWESOME_VIEWS;
}
public Object instantiateItem(View collection, int position) {
View layout = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
WebView web = null;
switch (position) {
case 0:
layout = mInflater.inflate(R.layout.activity_main, null);
web = (WebView) layout.findViewById(R.id.webView1);
web.loadUrl("http://developer.android.com");
break;
case 1:
layout = mInflater.inflate(R.layout.activity_community, null);
web = (WebView) layout.findViewById(R.id.webView2);
web.loadUrl("http://www.xda-developers.com");
break;
case 2:
layout = mInflater.inflate(R.layout.activity_support, null);
web = (WebView) layout.findViewById(R.id.webView3);
web.loadUrl("http://www.samsung.com");
break;
}
WebSettings websettings = web.getSettings();
websettings.setBuiltInZoomControls(position == 0 ? false : true);
websettings.setUseWideViewPort(position == 0 ? true : false);
websettings.setJavaScriptEnabled(true);
websettings.setAllowFileAccess(true);
websettings.setDomStorageEnabled(true);
websettings.setLoadWithOverviewMode(true);
web.requestFocusFromTouch();
web.setWebViewClient(new WebViewClient());
web.setWebChromeClient(new WebChromeClient());
websettings.setSavePassword(true);
((ViewPager) collection).addView((View) layout);
return layout;
}
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}
}
}
Answered By - user
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.