0

我试图实现一个 Viewpager 指示器,以便我的用户知道他们在哪个页面上,这个活动有 3 个 webviews,我需要显示一个横幅,而不是通过 3 个单独的布局文件,但实际上是在代码中,就像普通的 viewPager 指示器一样。

代码

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

private class AwesomePagerAdapter extends PagerAdapter{


    public int getCount() {
        return NUM_AWESOME_VIEWS;
    }



    public Object instantiateItem(View collection, int position) {

        View layout = null; 
        switch (position) {
        case 0: {
            LayoutInflater mInflater =  (LayoutInflater)
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout  = mInflater.inflate(R.layout.activity_main, null);

        WebView web = (WebView)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("http://developer.android.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(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;

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

}
}

主要的.xml

<?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:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app1="http://schemas.android.com/apk/res/com.example.test">

<com.viewpagerindicator.TitlePageIndicator
    android:id="@+id/indicator"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#18000000"
    android:padding="10dip"
    android:textColor="#FF5E6973"
    app:footerColor="#FFAA2222"
    app:footerIndicatorHeight="3dp"
    app:footerIndicatorStyle="underline"
    app:footerLineHeight="1dp"
    app:selectedBold="true"
    app:selectedColor="#FF000000"
    app1:textColor="#000000" />

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

</LinearLayout>
4

1 回答 1

1

您需要将您的附加ViewPagerTitlePageIndicator

// Get a reference to the TitlePageIndicator
awesomeIndicator = (TitlePageIndicator) findViewById(R.id.indicator);

// Attach adapter to ViewPager
awesomePager.setAdapter(awesomeAdapter;

// Attach ViewPager to the indicator
awesomeIndicator.setViewPager(awesomePager);

您还需要getPageTitle()PagerAdapter.

private class AwesomePagerAdapter extends PagerAdapter {
    private String[] titles = new String[]{"Page 1 title", "Page 2 title"};

    .
    .

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position % titles.length];
    }
}
于 2013-03-11T17:34:47.847 回答