1

Android 4.0 用の「スワイプ ビュー + タイトル ストリップ」ナビゲーション タイプのプロジェクトを作成しました。タイトルをクリックするか、ページ間をスワイプすると、別のページが表示されます。たとえば、3 つのタブがあり、それぞれが異なる Web ページを表示します。ここでは、JavaScript などを使用して Web ページをロードする単純な WebView アプリを作成する方法について説明します。タイトルのタブをクリックするたびに別のものが表示されるように、誰かがこのコードを修正するのを手伝ってくれませんか。int position と switch(position){ 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;
    }
}
}
4

1 に答える 1

1

たとえば、assets/www フォルダー (index1.html、index2.html、index3.html) に 3 つの html ファイルを作成し、最後に onCreateView() をテキスト ビューの生成から webview の生成に変更することができます。

    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 がリロードされないようにする必要がありますが、これを達成する方法がわかりません。

これを読んだ誰かが、セクションがフォーカスされるたびに Web ビューがリロードされないようにする方法についてコメントを残してくれることを願っています。

于 2012-10-02T03:54:06.597 に答える