处理碎片内的线程

时间:2014-02-15 17:04:58

标签: android tabs android-asynctask android-actionbar adapter

目前我已经制作了3个标签。在每个选项卡(片段)上有一个线程来获取内容并通过ArrayAdapter使用listview显示它。 现在,当您滚动选项卡时,线程会一次又一次地执行(不是必需的)。 有办法阻止这个吗?或者我是否需要将内容写入外部存储以便更快地访问? 有时我在不同的标签上看到相同的数据,如何避免这种情况?

提前致谢

这是我在片段中使用的一些代码,在滑动时多次调用:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView;
    rootView = inflater.inflate(R.layout.leadlist, container, false);



    profielen = new ArrayList<Profile>();
    new getLeaderboards().execute(url);

    lv = (ListView) rootView.findViewById(android.R.id.list);
    lbadapter = new LeaderBoardAdapter(this.getActivity(), R.layout.item_layout, profielen);


    return rootView;
}

public static class getLeaderboards extends AsyncTask<String, Void, JsonObject> {


    @Override
    protected JsonObject doInBackground(String... urls) {

        JsonObject x = parseURL.GetJSON(urls[0]);

        return x;
    }


    @Override
    protected void onPostExecute(JsonObject obj) {
        Gson gson = new Gson();
        System.out.println(obj.toString());
        JsonArray arr = obj.get("rows").getAsJsonArray();

        for (int i=0;i<10;i++) {
            Profile pat = gson.fromJson( arr.get(i) , Profile.class);
            profielen.add(pat);
        }

        lv.setAdapter(lbadapter);


    }

}

My FragmentActivity:

    public class LeaderboardsActivity extends FragmentActivity implements
    ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;

private String[] tabs = { "Tab1", "Tab2", "Tab3" };

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    setContentView(R.layout.leaderboard);

    Intent i = getIntent();
    String region = i.getStringExtra("region");

    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    getActionBar().setDisplayShowTitleEnabled(false);
    getActionBar().setDisplayShowHomeEnabled(false);
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager(),region);

    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) {
}

}

0 个答案:

没有答案