내 MainActivity
을 사용하고 viewPager
을 사용하고 사용자가 스 와이프 제스처에서 두 번의 fragment
보기를 인스턴스화합니다. 조각 중 하나가 서버에서 일부 데이터를 가져 와서보기를 업데이트합니다.ViewPager : 사용자 스 와이프에서 조각보기 새로 고침
문제 :
인터넷에 연결을 통해 응용 프로그램을 시작,하지만 응용 프로그램을 실행하고 조각이 업데이트되지 않습니다에 보내 주시면 동안 와이파이 전환에 따라 일부 더미 데이터와 fragment
업데이트 수신 된 데이터로 조각화합니다.
fragment
보기를 인스턴스화 한 후 업데이트 할 수있는 방법을 제안하십시오. 다음
MainActivity.java
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return fragment1.newInstance();
case 1:
return fragment3.newInstance();
default:
return fragment1.newInstance();
}
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
}
FragmentA 클래스 (코드의 Fragment3)이 주어진다
가public class fragment3 extends Fragment implements YourFragmentInterface{
public static ListView mylistView;
public static ConfessionsAdapter adapter;
public static ArrayList<Confession> confessionList;
public static Context context;
public static fragment3 newInstance() {
fragment3 fragment = new fragment3();
new HttpGetReq().execute("http://example.com");
return fragment;
}
public fragment3() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getActivity(); // Getting context to tell async task about it, so it can SET stuff here.
if (container == null) {
return null;
}
View view = inflater.inflate(R.layout.list_fragment, container, false);
mylistView = (ListView) view.findViewById(android.R.id.list);
new HttpGetReq().execute("http://example.com");
return view;
}
}
HttpGetReq
클래스가 서버로부터 데이터를 취득하는 AsyncTask
이며, JSON을 파싱 fragmentA의 list
을 doInBackground()
방법으로 업데이트 한 다음, 어댑터를 fragmentA의 목록 View
의 onPostExecute()
방법으로 설정합니다.