2016-06-06 2 views
-1

viewPager에서 시작하여 참조 용 snapchat과 비슷한 애플리케이션이 있습니다. 현재 패턴을 기준으로하면 다음과 같습니다. 조각에있는 모든 내용을 초기화합니다. UI 스레드가 막히고 응용 프로그램이 시작하는 데 상당한 시간이 걸립니다.viewpager의 일부분에있는 객체 초기화하기

어떻게해야합니까? 이후 메인 스레드 자체에서 이러한 개체를 작업해야합니다. 당신의 조각에서

답변

1

public static FragmentSample newInstance(Datamodel Model) { 
    FragmentBatch fragment = new FragmentBatch(); 
    Bundle args = new Bundle(); 
    args.putParcelable("extra",Model); 
    fragment.setArguments(args); 
    return fragment; 
} 

같은 정적 인스턴스를 생성하고 활동의 섹션 어댑터를 초기화

public class SampleActivity extends AppCompatActivity { 

/** 
* The {@link android.support.v4.view.PagerAdapter} that will provide 
* fragments for each of the sections. We use a 
* {@link FragmentPagerAdapter} derivative, which will keep every 
* loaded fragment in memory. If this becomes too memory intensive, it 
* may be best to switch to a 
* {@link android.support.v4.app.FragmentStatePagerAdapter}. 
*/ 
private SectionsPagerAdapter mSectionsPagerAdapter; 

/** 
* The {@link ViewPager} that will host the section contents. 
*/ 
private ViewPager mViewPager; 
Datamodel datamodel; 
int batchCount; 
List<Datamodel > datamodels; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_weight_record); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    // Create the adapter that will return a fragment for each of the three 
    // primary sections of the activity. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 
    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 
    datamodels= new ArrayList<>(); 
    datamodels.add(new Datamodel ("Neoperene", "skyperene", "", "22w441",1,"+/-1%","")); 
    datamodels.add(new Datamodel ("Neoperene", "skyperene", "", "22w442",2,"+/-1%","")); 
    datamodels.add(new Datamodel ("Neoperene", "skyperene", "", "22w443",3,"+/-1%","")); 

} 



public class SectionsPagerAdapter extends FragmentPagerAdapter { 

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

    @Override 
    public Fragment getItem(int position) { 
     // getItem is called to instantiate the fragment for the given page. 
     // Return a PlaceholderFragment (defined as a static inner class below). 

     switch(position){ 
      case 0:      
       return FragmentSample.newInstance(Datamodel.get(0)); 
      case 1: 
       return FragmentBatch.newInstance(Datamodel.get(1)); 
      default: 
       return FragmentBatch.newInstance(Datamodel.get(0)); 
    } 

    @Override 
    public int getCount() { 
     // Show 3 total pages. 
     return 3; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
      case 0: 
       return "SECTION 1"; 
      case 1: 
       return "SECTION 2"; 
      case 2: 
       return "SECTION 3"; 
     } 
     return null; 
    } 
} 
}