我希望在创建(或加载页面)时,在viewPager中的FragmentStateAdapter的片段中使用TextView的setText。我用setOnPageChangeListener测试但是它不起作用,所以你有没有想过如何做到这一点。 Bellow my adaptater以及我如何尝试两个使用setOnPageChangeListener,(如果我可以在创建片段时初始化我的字段,那将非常好。)
mytest:
.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;
}
}
答案 0 :(得分:2)
这里是我所做的解决方案,效果很好
ViewPagerAdapterBis.java
public class ViewPagerAdapterBis extends FragmentStatePagerAdapter {
static int x;
public ViewPagerAdapterBis(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int i) {
x=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 boolean isViewFromObject(View view, Object object) {
if(object != null){
return ((Fragment)object).getView() == view;
}else{
return false;
}
}
}
ViewPagerAdapterObject.java
public class ViewPagerAdapterObject extends Fragment {
public static final String ARG_OBJECT = "object";
TextView pdate;
TextView pvent;
TextView phumidite;
TextView pmaxmin;
TextView pprecip;
TextView pneige;
ImageView pweatherIcn;
int x=ViewPagerAdapterBis.x;
static final String KEY_DAY="day";
static final String KEY_DAYTIME="daytime";
static final String KEY_DATE="obsdate";
static final String KEY_HIGH="hightemperature";
static final String KEY_LOW="lowtemperature";
static final String KEY_PRECIPAMOUNT="precipamount";
static final String KEY_SNOWAMOUNT="snowamount";
static final String KEY_WINDSPEED="windspeed";
static final String KEY_WINDDIRECTION="winddirection";
static ArrayList<HashMap<String, String>> nextDay=MainMeteo.nextDay;
static ArrayList<HashMap<String, String>> nextDayJourney=MainMeteo.nextDayJourney;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.meteo_pager, container, false);
pdate=(TextView)rootView.findViewById(R.id.textPagerDateBis);
pdate.setText(nextDay.get(x).get(KEY_DATE).toString());
pmaxmin=(TextView)rootView.findViewById(R.id.txtPagerMaxMin);
pmaxmin.setText("MAX: "+nextDayJourney.get(x).get(KEY_HIGH).toString()+"° | Min: "+nextDayJourney.get(x).get(KEY_LOW).toString()+"°");
pprecip=(TextView)rootView.findViewById(R.id.txtPagerPrecipitations);
pprecip.setText("Précipitation: "+nextDay.get(x).get(KEY_PRECIPAMOUNT).toString()+" mm");
pvent=(TextView)rootView.findViewById(R.id.txtPagerVent);
pvent.setText("Vent: "+nextDay.get(x).get(KEY_WINDSPEED).toString()+" Km/h - "+nextDay.get(x).get(KEY_WINDDIRECTION).toString());
pneige=(TextView)rootView.findViewById(R.id.txtPagerNeige);
pneige.setText("Neige: "+nextDay.get(x).get(KEY_SNOWAMOUNT).toString()+" mm");
return rootView;
}
}
我只需在我的自适应器中取项目编号并将其提供给我的片段以调用匹配值。