喜 这是我在照片中的项目。我的项目在每个页面都有自定义listView,listViews用json数据填充。
我写了每个标签的onCreateView
事件Toast消息,以澄清问题,看看发生了什么。
例如,当我点击第2个标签时,我得到“2个工作”,然后“3个工作”作为吐司消息。也一遍又一遍地向listview添加相同的数据btw。
抱歉,如果我无法解释我的问题。
这是我的代码。
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new VasitaTurFragment();
.
.
.
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 6;
}
MainActivity:
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "1", "2", "3","4","5","6" };
ModelFragment mF= new ModelFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
片段:
public class MenseiFragment extends Fragment {
private static final String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private List<Mensei> menseiList = new ArrayList<Mensei>();
private ListView listView;
private menseiCLA adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.mensei, container, false);
listView = (ListView) rootView.findViewById(R.id.listView1);
adapter = new menseiCLA(getActivity(), menseiList);
listView.setAdapter(adapter);
Toast.makeText(getActivity(),"2 worked",
Toast.LENGTH_LONG).show();
JsonArrayRequest commonReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
for (int i = 0; i < response.length(); i++) {
try {
JSONObject objAna = response.getJSONObject(i);
Mensei mensei = new Mensei();
.
.
menseiList.add(mensei);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(commonReq);
return rootView;
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}}
答案 0 :(得分:1)
这是viewpager
的默认行为,因此如果您单击每个选项卡,则会自动创建它的两个邻居,并且都在onResume
中运行。如果你真的希望他们只在可见时才做某事,请在每个标签片段的函数下面使用:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisible()){
if(isVisibleToUser){
Log.d("MyTag","My Fragment is visible");
}else{
Log.d("MyTag","My Fragment is not visible");
}
}
}