我使用标签创建了一个滑动视图。
问题是,当创建活动时,三个片段同时加载。我将默认片段设置为在活动打开时启动,因此,其他片段无法从服务器下载数据,但是有人会刷到此片段。
这是我的活动,我不会从三个片段中提取代码,因为我认为与解决方案无关,但如果有必要请告诉我。
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
public class Information extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
String id;
String idUser;
Complejo complejo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_icon_text_tabs);
recogerParametro();
setTitle(complejo.getName());
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(2);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_informacion_complejo, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.btn_llamarBarra:
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + complejo.getTelefono().toString()));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("Llamando..", "Falló la llamada", activityException);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new GaleriaComplejoFragment(), "GALERÍA");
adapter.addFrag(new InformacionComplejoFragment(), "INFORMACIÓN");
adapter.addFrag(new ComentariosComplejoFragment(), "COMENTARIOS");
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(1, false);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
private void recogerParametro() {
id= getIntent().getStringExtra("id");
idUser = getIntent().getStringExtra("idUser");
complejo = (Complejo) getIntent().getSerializableExtra("complejo");
}
}
感谢您的帮助!
答案 0 :(得分:0)
如前所述,您无法随时在ViewPager中加载单个视图/片段,因为最小offScreenPageLimit为1,即一次最少加载三个片段。
如果要加载数据结构并仅在片段变为可见时填充视图,即在滑动时,请使用片段的setUserVisibleHint()
方法 -
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
loadMyStuff();
}
}
您也可以在从服务器调用收到数据后对其进行缓存,并仅在setUserVisibleHint()
请注意,每次刷片段时都会调用此方法。
答案 1 :(得分:0)
private boolean isLoaded =false,isVisibleToUser;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
this.isVisibleToUser=isVisibleToUser;
if(isVisibleToUser && isAdded() ){
loadData();
isLoaded =true;
}
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
if(isVisibleToUser && (!isLoaded)){
loadData();
isLoaded=true;
}
}