我有一个ViewPager在Activity中使用FragmentStatePagerAdapter,它必须显示3个片段,现在只实现了第一个片段。 第一个片段使用asyncTask获取类别列表并在ListView中显示它们。
ViewPager使用FragmentPagerAdapter来显示项目。
问题出现在用户点击标签栏上的按钮以返回第一个活动时。我们第二次使用片段进行活动时,我无法更新视图,有一个我更新的ListView和ProgressBar,我调用setVisibility但没有任何反应。
为什么视图只是第一次更新?
包含ViewPager的活动:
public class BoutiqueActivity extends AppCompatActivity {
protected ViewPager viewPager;
protected TabLayout tabLayout;
protected Bundle savedInstanceState;
protected ViewPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_boutique);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
this.viewPager = this.findViewById(R.id.viewPager);
this.viewPager.setSaveFromParentEnabled(false);
this.savedInstanceState = savedInstanceState;
this.adapter = new ViewPagerAdapter(getSupportFragmentManager());
if (savedInstanceState == null){
this.setupViewPager();
}else {
Integer count = savedInstanceState.getInt("tabsCount");
String[] titles = savedInstanceState.getStringArray("titles");
for (int i = 0; i < count; i++) {
this.adapter.addFragment(getFragment(i), titles[i]);
}
}
}
@Override
public void onStart(){
super.onStart();
viewPager.setAdapter(adapter);
this.tabLayout = this.findViewById(R.id.tabLayout);
this.tabLayout.setupWithViewPager(viewPager);
}
private Fragment getFragment(int position){
return savedInstanceState == null ? adapter.getItem(position) : getSupportFragmentManager().findFragmentByTag(getFragmentTag(position));
}
private String getFragmentTag(int position) {
return "android:switcher:" + R.id.viewPager + ":" + position;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tabsCount", adapter.getCount());
outState.putStringArray("titles", adapter.getFragmentTitles().toArray(new String[0]));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == android.R.id.home) {
this.finish();
}
return super.onOptionsItemSelected(item);
}
private void setupViewPager() {
this.adapter.addFragment(new CategorieFragment(), "Catégories");
this.adapter.addFragment(new ArticleFragment(), "Articles");
this.adapter.addFragment(new PromotionFragment(), "Promotions");
}}
片段:
public class CategorieFragment extends Fragment implements ActiviteEnAttenteAvecResultat<Categorie> {
protected ArrayList<Categorie> categories;
protected ListView listView;
protected Drawable substitut;
protected ProgressBar loader;
public CategorieFragment(){};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
View view = inflater.inflate(R.layout.fragment_categorie, null);
try {
this.substitut = Drawable.createFromStream(getActivity().getAssets().open("cintre.png"), null);
} catch (IOException e){}
return view;
}
@Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
this.categories = new ArrayList<>();
}
@Override
public void onStart(){
super.onStart();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.listView = view.findViewById(R.id.liste);
this.loader = view.findViewById(R.id.loader);
CategorieAdapter categorieAdapter = new CategorieAdapter(getActivity(), categories, substitut);
this.listView.setAdapter(categorieAdapter);
CategorieDAO.getInstance(this, this.loader, this.listView).findAll();
}
@Override
public void notifyRetourRequete(Categorie result){
}
@Override
public void notifyRetourRequeteFindAll(ArrayList<Categorie> liste){
this.categories.clear();
this.categories.addAll(liste);
((BaseAdapter) this.listView.getAdapter()).notifyDataSetChanged();
}
@Override
public void afficheLoader(){
this.loader.setVisibility(View.VISIBLE);
this.listView.setVisibility(View.INVISIBLE);
}
@Override
public void cacheLoaderAfficheContenu() {
this.loader.setVisibility(View.INVISIBLE);
this.listView.setVisibility(View.VISIBLE);
}}
适配器:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
protected final List<Fragment> fragmentList = new ArrayList<>();
protected final List<String> fragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager){
super(manager);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position){
return fragmentTitleList.get(position);
}
public void addFragment(Fragment fragment, String title){
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
public List<String> getFragmentTitles(){
return this.fragmentTitleList;
}}
的AsyncTask
public class HTTPRequest<T extends Object> extends AsyncTask<String, Void, String> {
protected ProgressBar loader;
protected ListView listView;
protected ActiviteEnAttenteAvecResultat activite;
protected DAO dao;
protected String method;
protected String error;
protected Object data;
protected Class deserializationClass;
public HTTPRequest(ActiviteEnAttenteAvecResultat activite, DAO dao, String method, Object data, Class deserializationClass, ProgressBar loader, ListView listView){
this.activite = activite;
this.dao = dao;
this.method = method;
this.data = data;
this.deserializationClass = deserializationClass;
this.loader = loader;
this.listView = listView;
}
public HTTPRequest(ActiviteEnAttenteAvecResultat activite, DAO dao, String method, Object data, Class deserializationClass){
this.activite = activite;
this.dao = dao;
this.method = method;
this.data = data;
this.deserializationClass = deserializationClass;
}
@Override
protected void onPreExecute(){
this.afficheLoader();
}
@Override
protected String doInBackground(String... urls) {
String urlRequete = urls[0];
StringBuffer resultat = new StringBuffer(1024);
try{
final HttpURLConnection connection = (HttpURLConnection) new URL(urlRequete).openConnection();
connection.setReadTimeout(1000);
connection.setConnectTimeout(1500);
connection.setRequestMethod(this.method);
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
String line = "";
while((line = in.readLine()) != null){
resultat.append(line);
}
in.close();
} catch (Exception e){
this.error = "Error : " + e.getMessage();
return null;
}
return resultat.toString();
}
@Override
protected void onPostExecute(String result){
Gson gson = new Gson();
ArrayList<T> liste = new ArrayList<>();
if(result != null){
switch (this.method){
case (HTTPRequestMethod.GET):
JsonParser parser = new JsonParser();
JsonArray array = parser.parse(result).getAsJsonArray();
for (int i = 0; i < array.size(); i++) {
Object object = gson.fromJson(array.get(i), this.deserializationClass);
liste.add((T) object);
}
activite.notifyRetourRequeteFindAll(liste);
break;
}
} else {
}
this.cacheLoaderAfficheContenu();
}
public void afficheLoader(){
this.loader.setVisibility(View.VISIBLE);
this.listView.setVisibility(View.INVISIBLE);
}
public void cacheLoaderAfficheContenu() {
this.loader.setVisibility(View.INVISIBLE);
this.listView.setVisibility(View.VISIBLE);
}}