每个页面都有列表视图的ViewPager是滞后的

时间:2013-05-24 21:04:00

标签: android android-listview android-viewpager

我正在尝试创建一个ViewPager,其中每个页面都有一个带有几个项目的ListView。虽然应用程序编译并运行良好,但有些问题有时需要大约1到2分钟才能使ViewPager在刷卡时转到下一页或上一页。我使用的代码如下:

主要启动屏幕活动如下:

public class LaunchScreen extends FragmentActivity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitylaunchscreen);

        int[] size = {2,3,1,4,1,3,3,3,3,1};

        ArrayList<Parent> parents = new ArrayList<Parent>();

        for(int i=0; i<size.length; i++)
        {
            ArrayList<Child> childs = new ArrayList<Child>();

            for(int j=0; j<size[i]; j++)
            {
                childs.add(new Child("" + j));
            }

            Parent parent = new Parent(childs);
            parents.add(parent);
        }

        MyPagerAdapter myPageAdapter = new MyPagerAdapter(getSupportFragmentManager(), this, parents);
        ViewPager viewPager = (ViewPager) findViewById(R.id.activitylaunchscreen_viewpager);
        viewPager.setAdapter(myPageAdapter);
    }
}

ViewPager的适配器如下:

public class MyPagerAdapter extends FragmentPagerAdapter
{
    final String parentFragmentTag = "visit";

    Context context;

    ArrayList<Parent> parents;

    public MyPagerAdapter(FragmentManager fragmentManager, Context context, ArrayList<Parent> parents)
    {
        super(fragmentManager);

        this.context = context;

        this.parents = parents;
    }


    @Override
    public Fragment getItem(int position) 
    {
        MyFragment myFragment = new MyFragment();

        myFragment.setParent(parents.get(position));

        return myFragment;

    }



    @Override
    public int getCount() 
    {
        return parents.size();
    }
}

每页的片段如下:

public class MyFragment extends ListFragment
{
    Parent parent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle)
    {       
        View view = inflater.inflate(R.layout.fragmentparentpage, null);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
    {
        super.onActivityCreated(savedInstanceState);
        ChildBaseAdapter childBaseAdapter = new ChildBaseAdapter(getActivity(), parent.getChilds());
        setListAdapter(childBaseAdapter);
    }

    public Parent getParent()
    {
        return parent;
    }

    public void setParent(Parent parent) 
    {
        this.parent = parent;
    }
}

最后,每个页面中ListView的适配器如下:

public class ChildBaseAdapter extends BaseAdapter
{
    private String childViewTag = "child";

    Context context;

    ArrayList<Child> childs;

    public ChildBaseAdapter(Context context, ArrayList<Child> childs)
    {
        this.context = context;

        this.childs = childs;
    }

    @Override
    public int getCount() 
    {
        return childs.size();
    }

    @Override
    public Object getItem(int position) 
    {
        return childs.get(position);
    }

    @Override
    public long getItemId(int position) 
    {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) 
    {

        if(view == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.childlayout, null);
        }
        else
        {
            if(((String) view.getTag()).compareTo(childViewTag) != 0)
            {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.childlayout, null);
            }
        }

        TextView childName = (TextView) view.findViewById(R.id.childlayout_textviewChildName);

        childName.setText(childs.get(position).getNumber());
        view.setTag(childViewTag);

        return view;
    }
}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

nvm是android放的填充。默认情况下,当你创建一个新的android项目时,android会使主活动的xml文件的父布局有填充,这似乎搞乱了viewpager监听器的滚动。