0

Viewpager Indicator を実装して、ユーザーが現在のページを認識できるようにしようとしています。このアクティビティには 3 つの Web ビューがあり、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) {}

}
}

main.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

にアタッチする必要がありViewPagerますTitlePageIndicator

// 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 に答える