0

我在添加操作栏项来控制我的 Viewpager 屏幕时遇到了麻烦,我希望能够根据拉起的页面来控制 viewpager。我真的只有 3 个 web 视图,我想添加操作栏项以通过操作栏(刷新、后退、前进)控制 web 视图。提前感谢您提供的任何帮助。谢谢

代码

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) {}

}
}

根据建议更新代码

4

2 回答 2

3

我已将您的初始代码重写为应该执行您想要的操作:

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);
    }

}
}
于 2013-03-16T06:44:17.573 回答
2

将您WebViews的内容塞入每个ViewPager插槽可能会更简单。

@Override
public Object instantiateItem(ViewGroup viewPager, int position)
{
    WebView webView = new WebView(AwesomePagerActivity.this);

    switch (postion)
    {
        case 0:
            //All other necessary setup
            webView.loadUrl("http://developer.android.com");
        break;
        case 0:
            //All other necessary setup
            webView.loadUrl("http://xda-developers.com");
        break;
        case 0:
            //All other necessary setup
            webView.loadUrl("http://www.samsung.com");
        break;
    }
}

这样,您的 Activity 可以简单地...

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    WebView webView = (WebView) awesomePager.getChildAt(awesomePager.getCurrentItem());

    switch (item.getItemId())
    {
        case R.id.menu_back:
            webView.goBack;
            return true;
    }
}

当然,您仍然可以扩展您的WebViewsfrom XML,只需将相关findViewById()调用插入onOptionsItemSelected.

于 2013-03-14T18:28:48.823 回答