ViewPager with Fragment with Android中的ActionBar.TabListener

时间:2014-12-19 11:38:17

标签: android performance android-fragments android-viewpager android-gridview

我想在ViewPager中显示的片段中填充listview和网格视图。我实现了相同的但是当我在Tabs之间切换时有滞后。 我正在从在线api(xml)填充listview。 Gridview目前填充了本地文件(可以将其更改为从Online api填充)。 当我从最近的读取移动到库选项卡时,然后滑动不顺畅。 问题:从第一个标签向第二个标签滑动时,可以轻松感受到延迟(滑动时的平滑度)。如何删除它。请考虑使用Aldico Reader获取UI示例。类似的

enter image description here enter image description here

以下是代码:

//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的滑动中面临滞后。

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>

如果我必须从互联网上获取所有数据,我应该在标签之间刷卡时如何改善性能。 如果需要任何其他部分以便更好地理解,请告诉我。

1 个答案:

答案 0 :(得分:0)

(我真正想要的只是发表一条评论,但我的名声太低,不能这样做。抱歉) 我自己使用与您相同的解决方案,并且在从一个片段切换到其他片段时也保持滞后。以下链接可能有助于https://github.com/codepath/android_guides/wiki/ActionBar-Tabs-with-Fragments