[英]Android : Pager Title Strip and WebView
我為Android 4.0創建了一個具有“ Swipe View + Title Strip”導航類型的項目。 我想要的是在單擊標題或在頁面之間滑動時顯示不同的頁面。 例如,我有三個標簽,每個標簽顯示一個不同的網頁。 現在,我將如何創建一個簡單的Webview應用程序,該應用程序將使用javascript等加載網頁。 請有人可以幫助我修改此代碼,以便每次我單擊這些標題中的標簽以顯示不同的內容時。 我看到了一些具有int位置的東西,然后切換了{position 0 {case 0,case 1,case 2}}等,但是我不知道將這段代碼放在哪里。
這是股票代碼,有人可以為我修改嗎? 我正在搜索有關此問題的數周:((。
package com.actionbartest;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
etc.
public class MainActivity extends FragmentActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
* sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will
* keep every loaded fragment in memory. If this becomes too memory intensive, it may be best
* to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three primary sections
// of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
* sections of the app.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return getString(R.string.title_section1).toUpperCase();
case 1: return getString(R.string.title_section2).toUpperCase();
case 2: return getString(R.string.title_section3).toUpperCase();
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
public DummySectionFragment() {
}
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
Bundle args = getArguments();
textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
return textView;
}
}
}
你可以在你的資產從生成文本視圖生成web視圖,像年底創建例如3個HTML文件/ WWW文件夾(index1.html,index2.html,index3.html)比變化onCreateView():
WebView mWeb = new WebView(getActivity());
WebSettings webSettings = mWeb.getSettings();
webSettings.setJavaScriptEnabled(true);
mWeb.setVerticalScrollBarEnabled(true);
Bundle args = getArguments();
mWeb.loadUrl("file:///android_asset/www/index"+args.getInt(ARG_SECTION_NUMBER)+".html");
return mWeb;
這種方法的壞處是,當您滑動Webview時會重新加載。 您應該將onCreateView設置為僅填充一次視圖,並防止在滑動時重新加載Webview,但是我不知道該如何完成。
我希望閱讀此書的人會在每次重點關注如何阻止webview重新加載時發表評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.