Android中带有ActionBar.TabListener的片段的ViewPager

Android中带有ActionBar.TabListener的片段的ViewPager,android,performance,android-fragments,android-viewpager,android-gridview,Android,Performance,Android Fragments,Android Viewpager,Android Gridview,我想在ViewPager内显示的片段中填充listview和grid view。我实现了相同的功能,但在选项卡之间切换时会出现延迟。 我正在从在线api xml填充listview。Gridview当前已填充本地文件,可能会将其更改为从联机api填充。 当我从“最近阅读”移动到“库”选项卡时,滑动不顺畅。 问题:从第一个选项卡滑动到第二个选项卡时,很容易感觉到滑动时的平滑度。如何删除它。请考虑Aldio Reader的UI样本。相似的 代码如下: //MainActivity Code , I

我想在ViewPager内显示的片段中填充listview和grid view。我实现了相同的功能,但在选项卡之间切换时会出现延迟。 我正在从在线api xml填充listview。Gridview当前已填充本地文件,可能会将其更改为从联机api填充。 当我从“最近阅读”移动到“库”选项卡时,滑动不顺畅。 问题:从第一个选项卡滑动到第二个选项卡时,很容易感觉到滑动时的平滑度。如何删除它。请考虑Aldio Reader的UI样本。相似的

代码如下:

//MainActivity Code , I am writing the selected sections which I feel will make
 you able to understand the View part.

AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
setContentView(R.layout.activity_main);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Set up the ViewPager, attaching the adapter and setting up a listener for when the
    // user swipes between sections.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAppSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            // When swiping between different app sections, select the corresponding tab.
            // We can also use ActionBar.Tab#select() to do this if we have a reference to the
            // Tab.
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by the adapter.
        // Also specify this Activity object, which implements the TabListener interface, as the
        // listener for when this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction)
    {
    }
/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
 * sections of the app.
 */
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

    public AppSectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Log.d("Tag","i = "+ i);
        switch (i) {
        case 0:

            return new LaunchpadSectionFragment();

        case 1: 

            return new TestSectionFragment();

        default:
            // The other sections of the app are dummy placeholders.
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
            fragment.setArguments(args);
            return fragment;
        }
    }
} 
getItem方法中使用的所有三个都是静态类,我将复制前2个,因为我在刷这2个方面面临滞后

LaunchpadSectionFragment类:

// All static variables
static final String URL = "http://api.androidhive.info/music/music.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";

static ListView list;
static LazyAdapter adapter;
static ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();


public static class LaunchpadSectionFragment extends Fragment {


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

        Thread th = new Thread(){
            public void run(){

                XMLParser parser = new XMLParser();
                String xml = parser.getXmlFromUrl(URL); // getting XML from URL
                Document doc = parser.getDomElement(xml); // getting DOM element
                NodeList nl = doc.getElementsByTagName(KEY_SONG);


                // looping through all song nodes <song>
                for (int i = 0; i < nl.getLength(); i++) {
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();
                    Element e = (Element) nl.item(i);
                    // adding each child node to HashMap key => value
                    map.put(KEY_ID, parser.getValue(e, KEY_ID));
                    map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                    map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
                    map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                    map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

                    // adding HashList to ArrayList
                    songsList.add(map);
                }

            }
        };
        th.start();
        list = ((ListView) rootView.findViewById(R.id.list));//.setImageResource(imageId);
        // Getting adapter by passing xml data ArrayList
        adapter = new LazyAdapter(getActivity(), songsList);        
        list.setAdapter(adapter);

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Log.d("TAG","Test");

            }
        }); 
        // getActivity().setTitle(planet);
        return rootView;
    }
}
TestSectionFragment类:

public static class TestSectionFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.grid_layout, container, false);
        GridView gridView = (GridView) rootView.findViewById(R.id.grid_view);

        gridView.setAdapter(new in.cdac.ebasta.gridview.ImageAdapter(getActivity()));
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

                // Sending image id to FullScreenActivity
                Intent i = new Intent(mContext, FullImageActivity.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
            }
        });
        return rootView;
    }
}
activity_main.xml

 <android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

   <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    </android.support.v4.view.ViewPager>


 </android.support.v4.widget.DrawerLayout>
当我在选项卡之间滑动时,特别是当所有数据都必须从internet获取时,我应该做些什么来提高性能。
请让我知道是否需要任何其他部分来更好地理解。

我真正想要的只是给出一个评论,但我的声誉太低了,不能这样做。很抱歉
我自己也在使用与你相同的解决方案,在从一个片段切换到其他片段时,它也一直处于滞后状态。下面的链接可能有助于

使用Tab listener而不是FragmentPagerAdapter实现片段之间切换的教程,我仍然没有找到真正的原因。你有解决方案吗?