2012-08-05 14 views
1

我爲Android 4.0創建了一個帶有「滑動視圖+標題條」導航類型的項目。我想要的是當我點擊標題或在頁面之間滑動時顯示不同的頁面。例如,我有三個選項卡,每個選項卡顯示不同的網頁。我現在如何創建一個簡單的webview應用程序,該應用程序使用JavaScript等來加載網頁。請有人幫助我修改此代碼,因此每次我點擊這些標題中的一張以顯示不同的內容。 我看到了一些int位置,然後切換(位置){情況0,情況1,情況2}等,但我不知道在哪裏把這個代碼。Android:Pager標題條和WebView

這是股票代碼,有人可以幫我修改嗎?我在尋找這個數週:((

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

回答

1

你可以在你的資產/ WWW文件夾(index1.html,index2.html,index3.html)比變化,例如創建3個HTML文件onCreateView() 從生成文本視圖生成web視圖,像年底

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從重裝上。刷卡,但我不知道如何做到這一點。

我希望讀過這篇文章的人會留言評論如何防止每次重點關注網頁時重新加載網頁視圖。