我有一个带有4个片段的SlidingPagerAdapter。每个片段都需要使用来自服务器的数据进行更新。
我为此目的使用了asynctask。
问题:
我们都知道ViewPager的工作原理。有没有我们可以从服务器为每个片段下载一次数据的方式。
FragmentPagerAdapter
public class FragmentAdapter extends FragmentStatePagerAdapter {
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
private String[] tabs = { "Strikers", "MidFielders", "Defenders", "GoalKeepers"};
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Strikers();
case 1:
return new MidFielders();
case 2:
return new Defenders();
case 3:
// return new FragmentPage4();
return new GoalKeepers();
}
return null;
// return MyFragment.newInstance(...);
}
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 4;
}
}
ViewPager
public class FirstTeamPlayers extends Activity {
private ViewPager mViewPager;
private SlidingTabLayout mSlidingTabLayout;
private FragmentAdapter mAdapter;
private ArrayList<Forwards> forwards;
private ArrayList<KMidFielders> midFielders;
private ArrayList<KDefenders> defenders;
private ArrayList<Keepers> goalkeepers;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_team_players);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mAdapter = new FragmentAdapter(getFragmentManager());
mViewPager.setAdapter(new FragmentAdapter(getFragmentManager()));
mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(mViewPager);
mViewPager.setOffscreenPageLimit(4);
}
@Override
protected void onPause() {
super.onPause();
//igetActionBar().getSelectedNavigationIndex());
}
public Fragment getActiveFragment(ViewPager container, int position) {
String name = makeFragmentName(container.getId(), position);
return getFragmentManager().findFragmentByTag(name);
}
private static String makeFragmentName(int viewId, int index) {
return "android:switcher:" + viewId + ":" + index;
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
}
片段
public class Strikers extends Fragment {
private TextView tv;
private ArrayList<Forwards> forwards;
private ProgressDialog pd;
private ListView lv;
private StrikersAdapter mAdapter;
private Context mContext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.strikers_fragment,container,false);
setHasOptionsMenu(true);
pd = new ProgressDialog(getActivity());
pd.setMessage("Loading...");
pd.setCancelable(false);
mContext =getActivity();
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
lv = (ListView) getView().findViewById(R.id.listView1);
new GetTeamPlayers().execute();
}
public void onEventMainThread(ArrayList<Forwards> result)
{
Toast.makeText(getActivity(),""+result.get(0).getPlayer_name(), Toast.LENGTH_SHORT).show();
tv.setText(""+result.get(0).getPlayer_name());
}
private class GetTeamPlayers extends AsyncTask<Void,Void,ArrayList<Forwards>>//ArrayList<NewsRes>>
{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd.show();
}
@Override
//ArrayList<NewsRes>
protected ArrayList<Forwards> doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
try {
HttpGet httpGet = new HttpGet("http://eastbengalfootballclub.com/ebfcapp/profile");
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
String response = EntityUtils.toString(httpEntity);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if(statusCode == 200) {
response = response.replace("(", "");
response = response.replace(")", "");
Log.i(".................", "" + response);
Gson gson = new Gson();
TeamModel responseModel = gson.fromJson(response, TeamModel.class);
ResponseResults results = responseModel.getResults();
forwards = results.getStrikers();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpClient.getConnectionManager().shutdown();
return forwards;
}
@Override
protected void onPostExecute(final ArrayList<Forwards> result) {//final ArrayList<NewsRes> result
// TODO Auto-generated method stub
super.onPostExecute(result);
if(result!=null) {
pd.dismiss();
mAdapter = new StrikersAdapter(mContext, result);
lv.setAdapter(mAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent(mContext, StrikersDetails.class);
intent.putExtra("details", result.get(position).getDescription());
intent.putExtra("preferrredpostion", result.get(position).getPosition_name());
intent.putExtra("nationality", result.get(position).getNationality());
intent.putExtra("name", result.get(position).getPlayer_name());
intent.putExtra("pastclubs", result.get(position).getPast_clubs());
intent.putExtra("bigimage", result.get(position).getBig_image());
String url = "http://eastbengalfootballclub.com/ebfcapp/profile/permalink/"+result.get(position).getPermalink();
Log.i("url is","......................."+url);
intent.putExtra("url",url);
startActivity(intent);
}
});
}
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.result_menu, menu);
// get my MenuItem with placeholder submenu
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.search:
MenuItem searchMenuItem = item;
searchMenuItem.expandActionView(); // Expand the search menu item in order to show by default the query
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.onActionViewExpanded();
searchView.setQueryHint("Search Results");
searchMenuItem.setOnActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// TODO Auto-generated method stub
mAdapter.setData();
mAdapter.notifyDataSetChanged();
return true;
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// TODO Auto-generated method stub
return true;
}
});
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
// this is your adapter that will be filtered
mAdapter.getFilter().filter(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
// this is your adapter that will be filtered
mAdapter.getFilter().filter(query);
mAdapter.notifyDataSetChanged();
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
break;
}
return super.onOptionsItemSelected(item);
}
}
其他片段类似。我想从服务器加载一次数据,并在用户滑动并在那里更新视图时将其传递给片段。从服务器加载数据必须是一次。
最初我想过在包含视图寻呼机的活动中从服务器加载所有数据。
使用
的想法 mViewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i2) {
}
@Override
public void onPageSelected(int i) {
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
我还在stackoverflow上找到了一些链接,建议在onPageSelected中使用接口。每个片段都实现了接口。
答案 0 :(得分:1)
有很多方法可以获得您想要的结果......其中一种方法是让主要活动负责数据,另一种方法是将数据存储到单例类中。
我进一步阐述了让主要活动为片段提供数据的例子:
例如,在主要活动中,我们有一个名称和姓氏的类:
public static class MyData{
String mName;
String mSurname;
public MyData(String mName, String mSurname) {
this.mName = mName;
this.mSurname = mSurname;
}
public String getmName() {
return mName;
}
public String getmSurname() {
return mSurname;
}
}
同样在主要活动中,我们有一个包含数据项的公共静态数组:
public static ArrayList<MyData>mData = new ArrayList<>();
在OnCreate主要活动中,我们填充数据:
mData.add(new MyData("John","Doe"));
mData.add(new MyData("Peter","Pan"));
我们可以从片段中访问数据:
ArrayList<MainActivity.MyData>list = MainActivity.mData;
for(MainActivity.MyData obj : list){
Log.d("DATA", obj.getmName() + " " + obj.getmSurname());
}