我有一个带有ViewPager的片段活动,这个片段活动应该将数据发送到寻呼机内部的片段(因此片段可以显示搜索结果)。 所以我的问题是,当我滑动到片段时它不会刷新 - 我需要在片段之间再滑动几次以便显示新数据。有人可以告诉我这样做的方法是什么?
这是我的代码:
//first i init the widgets
private void initWidgets() {
bundle = new Bundle();
progressBar = (ProgressBar) findViewById(R.id.search_progressBar);
viewPager = (ViewPager) findViewById(R.id.search_viewpager);
titleStrip = (PagerTabStrip) findViewById(R.id.search_titlestrip);
myFragmentPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(myFragmentPagerAdapter);
viewPager.setCurrentItem(1001);
titleStrip.setDrawFullUnderline(true);
}
//then after writing some text in the search i call this function//
private void search(String keyWord) {
try {
bundle.putString(IntentConstants.KEYWORD, searchEt.getText().toString());
myFragmentPagerAdapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
/*and then the bundle is passed to the fragments - this is in a subclass of the fragmentActivity*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position % PAGE_TITLES.length) {
// community
case 0:
CommunitySearchFragment f = new CommunitySearchFragment();
f.setArguments(bundle);
return f;
//My Clipbords Clipboardadater FragmentSearchMyClipboards
case 1:
FragmentSearchMyClipboards fragment = new FragmentSearchMyClipboards();
fragment.setArguments(bundle);
return fragment;
// My clips
case 2:
MyClipsFragment f2 = new MyClipsFragment();
f2.setArguments(bundle);
return f2;
// members
case 3:
MembersFragment f3 = new MembersFragment();
f3.setArguments(bundle);
return f3;
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
return PAGE_TITLES[position % PAGE_TITLES.length];
}
@Override
public int getCount() {
return 5000;
}
}
这是我的一个片段,作为我所做的参考:
public class MyClipsFragment extends Fragment {
ArrayList<ClipsVO> clipsArray;
private View mView;
private ListView list;
private MyClipboardSearchAdapter adapter;
private ArrayList<ClipsVO> rClipsArray;
private ProgressBar progress;
@Override
public void onAPIResponse(ResultVO result) {
super.onAPIResponse(result);
if (result.getApiMethod().equals(ClipixServer.APIMETHOD_SEARCHCLIP)) {
clipsArray = (ArrayList<ClipsVO>) result.getData();
int userId = Integer.parseInt(UserProfile.getinstance().getUserData().getUserId());
if (clipsArray.size() > 0) {
ArrayList<ClipsVO> rClipsArray = new ArrayList<ClipsVO>();
for (int i = 0; i < clipsArray.size(); i++) {
ClipsVO clip = clipsArray.get(i);
if (clip.getOwnerUserId().equals(userId + "")) {
if (rClipsArray.size() == 0) {
//Add header for the Current User's clips in the result
ClipsVO headerClip = new ClipsVO();
headerClip.setHeader(true);
headerClip.setDescription(getLocalizationString("lblCommunityMyClips"));
rClipsArray.add(headerClip);
}
rClipsArray.add(clipsArray.remove(i));
i--;
}
}
if (clipsArray.size() > 0) {
//Add header for the other Clipboards in the result
ClipsVO headerClip = new ClipsVO();
headerClip.setHeader(true);
headerClip.setDescription(getLocalizationString("lblClips"));
rClipsArray.add(headerClip);
rClipsArray.addAll(clipsArray);
}
}
this.rClipsArray = clipsArray;
if (getActivity() != null)
setupData();
}
}
private void setupData() {
adapter = new MyClipboardSearchAdapter(getActivity(), rClipsArray);
list.setAdapter(adapter);
TextView emptyTxt = (TextView) mView.findViewById(R.id.empty);
emptyTxt.setText(getLocalizationString("txtPeopleSearchNoResult"));
progress.setVisibility(View.GONE);
emptyTxt.setTextSize(14);
list.setEmptyView(emptyTxt);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Object itemObject = rClipsArray.get(position);
ArrayList<ClipsVO> clipsList = new ArrayList<ClipsVO>();
ClipsVO clipVO = (ClipsVO) itemObject;
clipsList.add(clipVO);
Intent intent = new Intent(getActivity(), ClipsListDetailActivity.class);
intent.putExtra(IntentConstants.CLIP_DATA, clipVO);
intent.putExtra(IntentConstants.CLIP_ID, clipVO.getClipId());
intent.putExtra(IntentConstants.IS_USERCLIPS, false);
UserProfile.getinstance().setClipsList(clipsList);
intent.putExtra(IntentConstants.CLIP_INDEX_POSITION, 0);
intent.putExtra(IntentConstants.CLIPBOARD_INDEX_POSITION, 0);
startActivity(intent);
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_list_layout, container, false);
list = (ListView) mView.findViewById(R.id.fragment_list_layout_list);
progress = (ProgressBar) mView.findViewById(R.id.frag_progress);
progress.setVisibility(View.VISIBLE);
rClipsArray = new ArrayList<ClipsVO>();
if (getArguments().getString(IntentConstants.KEYWORD) != null) {
//my clips
HashMap<String, String> params = new HashMap<String, String>();
setUserData(params);
params.put(ClipixServer.FEEDPARAMS_KEYWORD, getArguments().getString(IntentConstants.KEYWORD));
params.put(ClipixServer.FEEDPARAMS_RESULTCOUNT, "100");
placeRequest(ClipixServer.APIMETHOD_SEARCHCLIP, Json.METHODNAME_PARSECLIPS, params);
} else {
progress.setVisibility(View.GONE);
}
return mView;
}
}
答案 0 :(得分:0)
方法1:
ViewPager默认在两侧加载片段1。所以,它可以在适配器中加载3个片段。
尝试使用以下方法
public void setOffscreenPageLimit(int limit)
作为
<强> viewPager.setOffscreenPageLimit(0); 强>
这将允许始终动态加载片段。
方法2:
在加载片段[onAttachActivity]时,您可以刷新视图[设置视图内容]