我希望在创建(或加载页面)时在 viewPager 中的 FragmentStateAdapter 片段中设置我的 TextView 的 setText。我用 setOnPageChangeListener 进行测试,但它不起作用,所以你知道如何做到这一点。贝娄我的适配器以及我如何尝试使用 setOnPageChangeListener,(如果我可以在创建片段时初始化我的字段,那将非常好)。
测试:
.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}
@Override
public void onPageSelected(int arg0) {
pdate=(TextView)myPager.getChildAt(arg0).findViewById(R.id.txtPagerDate);
pdate.setText(nextDay.get(arg0).get(KEY_DATE).toString());
pmaxmin=(TextView)myPager.getChildAt(arg0).findViewById(R.id.txtPagerMaxMin);
pmaxmin.setText("MAX: "+nextDay.get(arg0).get(KEY_HIGH).toString()+"° | Min: "+nextDay.get(arg0).get(KEY_LOW).toString()+"°");
pprecip=(TextView)myPager.getChildAt(arg0).findViewById(R.id.txtPagerPrecipitations);
pprecip.setText("Précipitation: "+nextDay.get(arg0).get(KEY_PRECIPAMOUNT).toString()+" mm");
pvent=(TextView)myPager.getChildAt(arg0).findViewById(R.id.txtPagerVent);
pvent.setText("Vent: "+nextDay.get(arg0).get(KEY_WINDSPEED).toString()+" Km/h - "+nextDay.get(arg0).get(KEY_WINDDIRECTION).toString());
pneige=(TextView)myPager.getChildAt(arg0).findViewById(R.id.txtPagerNeige);
pneige.setText("Neige: "+nextDay.get(arg0).get(KEY_SNOWAMOUNT).toString()+" mm");
pweatherIcn=(ImageView)myPager.getChildAt(arg0).findViewById(R.id.imgPagerTemps);
pweatherIcn.setImageDrawable(drawable2);
}
});
ViewPagerAdapterBis.java
package base.appliMeteo.basemeteo;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class ViewPagerAdapterBis extends FragmentStatePagerAdapter {
static ArrayList<HashMap<String, String>> nextDay=MainMeteo.nextDay;
public ViewPagerAdapterBis(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int i) {
Fragment fragment = new ViewPagerAdapterObject();
Bundle args = new Bundle();
// Our object is just an integer :-P
args.putInt(ViewPagerAdapterObject.ARG_OBJECT, i + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return 7;
}
@Override
public CharSequence getPageTitle(int position) {
return "OBJECT " + (position + 1);
}
}
ViewPageAdapterObject.java
package base.appliMeteo.basemeteo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ViewPagerAdapterObject extends Fragment {
public static final String ARG_OBJECT = "object";
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.meteo_pager, container, false);
return rootView;
}
}