gpt4 book ai didi

android - 在 TabLayout 中使用多个 webview 来处理内存问题的逻辑

转载 作者:可可西里 更新时间:2023-11-01 19:01:27 25 4
gpt4 key购买 nike

我在 Tablayout 中有将近 30 个 webview。一切正常,但我的应用程序消耗了大量内存,并且会因为内存问题而关闭。这是我在日志中得到的

04-05 21:00:09.458 19720-19801/com.example.choman.webview A/chromium: [FATAL:memory.cc(19)] Out of memory. size=16777216

这是我的 java 文件。基本上所有剩余的 29 个 fragment 都包含相同的代码,只是 url 发生了变化。我不确定如何处理。

public class stackOverflow extends Fragment {


private WebView webView;
private ProgressBar progressBar1;
private SwipeRefreshLayout mSwipeRefreshLayout1;




public stackOverflow() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment


return inflater.inflate(R.layout.fragment_tab1, container, false);


}



@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {


progressBar1 = (ProgressBar) view.findViewById(R.id.progressBar1);

webView = (WebView) view.findViewById(R.id.website_detail_1);

webView.setWebViewClient(new MyAppWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);

webView.loadUrl("http://www.stackoverflow.com");


WebSettings webSettings = webView.getSettings();

webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.getSettings().setAppCacheEnabled(true);
webSettings.setDomStorageEnabled(true);

webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);


mSwipeRefreshLayout1 = (SwipeRefreshLayout) view.findViewById(R.id.swipe1);
mSwipeRefreshLayout1.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.loadUrl("http://www.stackoverflow.com");
}
});

if (mSwipeRefreshLayout1.isRefreshing()) {
mSwipeRefreshLayout1.setRefreshing(false);
}


webView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}

return false;
}
});

}

public class MyAppWebViewClient extends WebViewClient {

@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//view.findViewById(R.id.progressBar1).setVisibility(View.GONE);
Log.i("pageFinished", "yesss");
//progressBar.setVisibility(View.INVISIBLE);
progressBar1.setVisibility(View.GONE);


if (mSwipeRefreshLayout1.isRefreshing()) {
mSwipeRefreshLayout1.setRefreshing(false);
}


}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}


@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}


}
}

主要 Activity

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public WebView view;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);


TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);


tabLayout.addTab(tabLayout.newTab().setText("WebView1").setTag("WebView1"));
//all the way down to .....
tabLayout.addTab(tabLayout.newTab().setText("WebView30").setTag("WebView30"));

final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setOffscreenPageLimit(6);


final PagerAdapter adapter = new TabPagerAdapter(getSupportFragmentManager(), tabLayout
.getTabCount());
viewPager.setAdapter(adapter);

viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(
tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});


DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();

// //noinspection SimplifiableIfStatement
// if (id == R.id.action_settings) {
// return true;
// }

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.WebView1) {

viewPager.setCurrentItem(0);

//all the way down to..30

if (id == R.id.WebView30) {

viewPager.setCurrentItem(30);






}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}

这是 TabPagerAdapter 的代码

public class TabPagerAdapter extends FragmentStatePagerAdapter
{

int tabCount;


public TabPagerAdapter(FragmentManager fm, int numberOfTabs)
{
super(fm);
this.tabCount = numberOfTabs;

}


@Override
public Fragment getItem(int position)
{
switch (position)
{
case 0:
//
WebViewFragment1 tab1 = new WebViewFragment1();
return tab1;

//all the way to tab 29

webViewFragment30 tab30 = new WebViewFragment30();
return tab30



default:
return null;

}

}

@Override
public int getCount()
{
return tabCount;
}
}

最佳答案

使用ViewPagerViewPager 将随时在内存中保存最多 3 个 fragment (不更改默认的离屏页面限制)。 3个 fragment 是当前可见 fragment ,当前 fragment 的左侧和右侧 fragment 。

如果你想要标签,你可以创建一个看起来像系统标签的自定义 View ,并将它添加到 ViewPager 之上。将 PageTransformerViewPager 结合使用以跟踪 ViewPager 滑动并相应地滚动自定义选项卡。然后听 Tabs 的点击,并根据按下的 Tab 滚动 ViewPager

更新:

我完全忘记了我们可以将 TabLayout 附加到 ViewPager 而不是创建自定义选项卡。

tabLayout.setUpWithViewPager(viewpager) 将起作用。

感谢@choman

关于android - 在 TabLayout 中使用多个 webview 来处理内存问题的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36430739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com