我为Android 4.0创建了一个带有“Swipe View + Title Strip”导航类型的项目。我想要的是当我点击标题或在页面之间滑动时显示不同的页面。例如,我有三个选项卡,每个选项卡都显示一个不同的网页。我现在如何创建一个简单的webview应用程序,它使用javascript等加载网页。请允许有人帮助我修改这个代码,因此我每次都会从这些标题中点击标签,以显示不同的标题。 我看到一些int位置然后切换(位置){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;
}
}
}
答案 0 :(得分: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在刷卡时重新加载,但我不知道如何完成此操作。
我希望看过这篇文章的人会在每次重点关注网页视图时发表评论。