ViewPager花时间加载包含ListViews的片段

时间:2014-08-01 06:54:37

标签: android android-fragments android-listview android-viewpager

范围:

我在片段(Schedule Fragment)中使用ViewPager,后者又显示了包含相应ListView中不同数据的同一片段(ScheduleListSubFragment)的5个实例。我正在使用FragmentPagerAdaper(ScheduleFragmentAdapter)。

代码:

代码如下:

计划片段:

    public class ScheduleFragment extends Fragment{
ScheduleFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
TitlePageIndicator indicator;

@Override
public void onResume() {
    Log.d(" onResume()------------------ScheduleFrament","oijlkjlijijoij");
    super.onResume();
}

public void setNstartAnimation(final View v, int id, int duration)
{
    Animation anim = AnimationUtils.loadAnimation(getActivity(), id);
    anim.setAnimationListener(new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            v.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // TODO Auto-generated method stub

        }
    });

    anim.setDuration(duration);
    v.startAnimation(anim);
}


@Override
public void onViewStateRestored(Bundle savedInstanceState) {
    Log.d(" onViewState Restored()------------------ScheduleFrament","oijlkjlijijoij");

    super.onViewStateRestored(savedInstanceState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.d(" onCreate()------------------ScheduleFrament","oijlkjlijijoij");

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.scheduled_list, null);

    MainActivity.setTouchModewithCurrentPage();

    Log.d(" onCreateView()------------------ScheduleFrament","oijlkjlijijoij");


    mAdapter = new ScheduleFragmentAdapter(getChildFragmentManager());
    prepareSlidingPager(v);

    final TextView tv = (TextView) v.findViewById(R.id.category_header);
    tv.setTypeface(Utils.getHeroFace(getActivity().getAssets()));
    setNstartAnimation(tv, R.anim.slide_in_top, 1000);

    return v;
}

private void prepareSlidingPager(View v)
{

        mPager = (ViewPager)v.findViewById(R.id.pager);

        mPager.setOffscreenPageLimit(4);

        mPager.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();


        mIndicator = (TitlePageIndicator)v.findViewById(R.id.indicator);
        indicator = (TitlePageIndicator) mIndicator;
        mIndicator.setViewPager(mPager);
        mIndicator.setCurrentItem(getArguments().getInt("page"));

        mIndicator.setOnPageChangeListener(new pageListener());


        final float density = getResources().getDisplayMetrics().density;
        indicator.setBackgroundColor(0x18AF0000);
        indicator.setFooterColor(0xFFAA2222);

        //indicator.setFooterColor(0xFFAA33);

        indicator.setFooterLineHeight(2 * density); //1dp
        indicator.setFooterIndicatorHeight(3 * density); //3dp
        indicator.setFooterIndicatorStyle(IndicatorStyle.Underline);

        indicator.setTextColor(0xAA000000);
        indicator.setSelectedColor(0xFF000000);
        indicator.setSelectedBold(true);
}


public static ScheduleFragment newInstance(int currentPage)
{
    ScheduleFragment frag = new ScheduleFragment();
    Bundle bundle = new Bundle();
    bundle.putInt("page", currentPage);
    frag.setArguments(bundle);
    return frag;
}


@Override
public void onDetach() {
    super.onDetach();

    try {
        Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this, null);

    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

}

ScheduleListSubFragment

    public class ScheduleListSubFragment extends Fragment implements OnItemClickListener, UpdateableFragment{

long startDate;
long endDate;


private ListView eventlist;
private listadapter adapter;
private List events;
private MyDB db;



@Override
public void onResume() {

    Utils.log("on resume......subfragment");



    super.onResume();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    Log.v(" OnsaveInstanceState()------------------","oijlkjlijijoij");
    super.onSaveInstanceState(outState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    db=new MyDB(this.getActivity());

    db.open();
    events=db.getScheduleEntries_bytime(getArguments().getLong("start_time"), getArguments().getLong("end_time"));
    db.close();
    if(events!=null)
    {
    adapter=new listadapter(getActivity(), android.R.layout.simple_expandable_list_item_1, events);

    }
    Log.d(" onCreate()------------------sublist schedule","oijlkjlijijoij");

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.eventslist_subfragment_layout, null);

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);

    Log.d(" onViewCreated()------------------sublist schedule","asdfasdf");// Toast.LENGTH_SHORT).show();

    eventlist=(ListView)view.findViewById(R.id.Event_list);
    eventlist.setOnItemClickListener(this);
    //new updateViewTask().execute(new Object());
    fetcheventdata();

}

private class updateViewTask extends AsyncTask<Object, Object, Object>
{

    @Override
    protected Object doInBackground(Object... params) {
        //try{Thread.sleep(2000);}catch(Exception e){e.printStackTrace();}
        publishProgress(new Object());
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void onProgressUpdate(Object... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);

    }


}

public static ScheduleListSubFragment newInstance(long starttime, long endtime)
{
    ScheduleListSubFragment frag = new ScheduleListSubFragment();
    Bundle bundle = new Bundle();

    bundle.putLong("start_time", starttime);
    bundle.putLong("end_time", endtime);
    frag.setArguments(bundle);

    return frag;
}



 private void fetcheventdata()
    {


        if(events!=null)
        {
        eventlist.setAdapter(adapter);

        adapter.notifyDataSetChanged();
        Utils.log(".list size: subframgment:"+events.size()+"");//, duration)
        }
        else
            Toast.makeText(getActivity(), "No Events Yet", Toast.LENGTH_LONG);

        //Toast.makeText(getActivity(), events.size(), Toast.LENGTH_LONG).show();



    }


 private class listadapter extends ArrayAdapter<Event>
    {
        private List items;

        public listadapter(Context context, int textViewResourceId,
                List objects){
            super(context, textViewResourceId, objects);
            items=new ArrayList();
            items=objects;

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View v=convertView;

            ScheduleEntryViewHolder viewHolder=null;
            if(v==null)
            {
                LayoutInflater vi=(LayoutInflater)getActivity().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                v=vi.inflate(R.layout.schedule_entry, null);

                viewHolder = new ScheduleEntryViewHolder();

                viewHolder.nameView=(TextView)v.findViewById(R.id.Name);
                viewHolder.nameView.setTypeface(Utils.getHeroFace(getActivity().getAssets()));
                viewHolder.nameView.setTextColor(getResources().getColor(R.color.black));


                viewHolder.roundView=(TextView)v.findViewById(R.id.Round);
                viewHolder.roundView.setTypeface(Utils.getHeroFace(getActivity().getAssets()));

                Utils.log("entry "+position+"th in listview");

                viewHolder.venueView=(TextView)v.findViewById(R.id.Venue);
                viewHolder.venueView.setTypeface(Utils.getHeroFace(getActivity().getAssets()));
                viewHolder.venueView.setTextColor(getResources().getColor(R.color.darkgrey));

                viewHolder.timeView=(TextView)v.findViewById(R.id.time);
                viewHolder.timeView.setTextColor(getResources().getColor(R.color.black));


                viewHolder.categoryView=(TextView)v.findViewById(R.id.cat_name_view);
                viewHolder.favView = (ImageView) v.findViewById(R.id.favourite_view);



                v.setTag(viewHolder);
            }
            else
                viewHolder = (ScheduleEntryViewHolder) v.getTag();

            ScheduleEntry scheduleentry=(ScheduleEntry)items.get(position);
            if(scheduleentry!=null)
            {

                viewHolder.nameView.setText(scheduleentry.getEventName());
                viewHolder.venueView.setText(scheduleentry.getVenue());
                viewHolder.roundView.setText("("+scheduleentry.getRoundName()+")");
                viewHolder.timeView.setText(Html.fromHtml("<b>"+scheduleentry.getStartTime12()+"-"+scheduleentry.getEndTime12()+"</b><br></br>"+scheduleentry.getStartDate()));

                if(scheduleentry.getFav_status().equals("1"))
                    viewHolder.favView.setVisibility(View.VISIBLE);
                else
                    viewHolder.favView.setVisibility(View.INVISIBLE);

                viewHolder.categoryView.setText(scheduleentry.getCategory());
                viewHolder.categoryView.setBackgroundColor(Utils.getColorforCategory(scheduleentry.getCategory().toUpperCase(), getActivity()));
            }
            return v;
        }

    }


 @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
        eventnotifierapp app=(eventnotifierapp)getActivity().getApplication();

        db.open();
        app.crntEvent=db.getEvent( ((ScheduleEntry)events.get(pos)).getEventName()  );

        db.close();
        //startActivity(new Intent(this.getActivity(), EventDescription.class));

        EventDescriptionFragment frag = EventDescriptionFragment.NewInstance();
        ((MainActivity)getActivity()).OpenNextFragment(frag);

    }



@Override
public void update(long start, long end) {
    Toast.makeText(getActivity(), "finally.... ------------------------------called", Toast.LENGTH_SHORT).show();
    fetcheventdata();

}

}

ScheduleFragmentAdapter

  public class ScheduleFragmentAdapter extends FragmentPagerAdapter{
    String cat_name;
    protected static final String[] CONTENT = new String[] { "All", "Thursday, Feb 27", 
        "Friday, Feb 28", "Saturday, Mar 1", "Sunday, Mar 2", };

    private List<Fragment> fragments;
    private static long startdates[]={day1start, day2start, day3start, day4start};
    private static long enddates[]={day1end, day2end, day3end, day4end};

    private int mCount = CONTENT.length;
    private int mindex=0;


    public void setmIndex(int i)
    {
        mindex=i;
    }

    public ScheduleFragmentAdapter(FragmentManager fm) {
        super(fm);

        fragments = new ArrayList<Fragment>();
        fragments.add(ScheduleListSubFragment.newInstance(day1start, day4end));
        fragments.add(ScheduleListSubFragment.newInstance(day1start, day1end));
        fragments.add(ScheduleListSubFragment.newInstance(day2start, day2end));
        fragments.add(ScheduleListSubFragment.newInstance(day3start, day3end));
        fragments.add(ScheduleListSubFragment.newInstance(day4start, day4end));


    }





    @Override
    public int getItemPosition(Object item) {
        Log.v("getItemPosition() callled......", "yeah-----------------");

        return super.getItemPosition(item);
    }





    @Override
    public Fragment getItem(int position) {

        Log.v("getItem() callled......", "getItem() ...........");
        return fragments.get(position);


    }

    @Override
    public int getCount() {
        return mCount;
    }

    @Override
    public CharSequence getPageTitle(int position) {
      return CONTENT[position % CONTENT.length];
    }
}

问题

所以,出现问题。每当我在我的Activity中替换我的Frame上的ScheduleFragment时,这个片段需要花费很多时间才能出现。增加你的知识;它几乎挂起了主线程。

为了防止这会让用户烦恼,我做了一个asynctask,显示进度条3秒钟。但是由于主线程在这3分钟内也会卡住,进度条会出现并且卡住(意味着圆圈没有旋转)3分钟然后消失。

所以大家请帮助我如何使这个ViewPager加载效率高。我的意思是,我如何有效地异步加载'ScheduleListSubFragment实例'中的数据,以便主线程不会卡住。

补充知识

  1. 在我的ListView项目中,我有6个textViews和1个ImageView,因此,视图生成可能需要时间。

2 个答案:

答案 0 :(得分:3)

mPager.setOffscreenPageLimit(4);替换mPager.setOffscreenPageLimit(1);会使第一次加载更快,因为您不需要一次加载5个片段,它们将在您滑动时加载。

答案 1 :(得分:1)

扩展FragmentStatePagerAdapter而不是FragmentPagerAdapter ...... https://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html

提取物:

  

当存在大量页面时,此版本的寻呼机更有用,更像列表视图。当页面对用户不可见时,它们的整个片段可能会被破坏,只保留该片段的保存状态。

这有助于更快地加载viewPager片段,因为只有在需要时才会创建/销毁片段。

您可能还想使用RecyclerView而不是ListView来实现仅加载可见单元格并回收隐藏单元格。