抱歉,我知道有很多关于这个问题的问题,但我尝试了我找到的所有解决方案但没有成功。
我正在尝试在ViewPager中更改TextView的文本。我将屏幕分为两部分:一部分是ViewPager,另一部分是普通布局。如果我更改了正常布局中的文本,它会毫无问题地更改,但如果我更改ViewPager中的文本,它就不会更改。
我有两个xml布局:自动从Android Studio创建的activity_main和fragment main,在MainActivity的OnCreate中我有
setContentView(R.layout.activity_main)
my Fragment
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ImageView imageView = (ImageView) rootView.findViewById(R.id.weathericon);
if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
rootView = inflater.inflate(R.layout.fragment_main, container, false);
}
if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
rootView = inflater.inflate(R.layout.temperatura_main, container, false);
}
if (getArguments().getInt(ARG_SECTION_NUMBER) == 3) {
rootView = inflater.inflate(R.layout.precipitazioni_main, container, false);
}
if (getArguments().getInt(ARG_SECTION_NUMBER) == 4) {
rootView = inflater.inflate(R.layout.vento_main, container, false);
}
if (getArguments().getInt(ARG_SECTION_NUMBER) == 5) {
rootView = inflater.inflate(R.layout.alba_main, container, false);
}
if (getArguments().getInt(ARG_SECTION_NUMBER) == 6) {
rootView = inflater.inflate(R.layout.umidita_main, container, false);
}
if (getArguments().getInt(ARG_SECTION_NUMBER) == 7) {
rootView = inflater.inflate(R.layout.pressione_main, container, false);
}
return rootView;
}
}
我的PageAdapter
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 7;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
case 3:
return "SECTION 4";
case 4:
return "SECTION 5";
case 5:
return "SECTION 6";
case 6:
return "SECTION 7";
}
return null;
}
}
我尝试以这种方式更改文本,当我有新数据时:
@Override
public void success(WeatherResponse weatherResponse, Response response) {
String forecast = weatherResponse.getCurrently().getSummary();
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout1 = inflater.inflate(R.layout.fragment_main,null);
forecast1 = (TextView) findViewById(R.id.foredesc1);
wedesc = (TextView) layout1.findViewById(R.id.weatherdescription); //this is in fragment_main
city = (TextView) layout1.findViewById(R.id.city);
wedesc.setText("test"); //the problem is here
forecast1.setText(forecast);
/* Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("container");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
*/
mViewPager.getAdapter().notifyDataSetChanged();
我发现如果添加更改文本的唯一方法
setContentView(layout1)
但问题是,通过这种方式,我不再拥有ViewPager和其他布局,只有fragment_main xml中的内容。
我不知道我是否解释得很好但问题出在哪里?