Fragment
java
package help.huhu.hhyy.tool.fetalmove; import help.huhu.hhyy.R; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnGroupExpandListener; import android.widget.ImageView; public class FetalMoveSumFragment extends Fragment implements OnClickListener{ ImageView detail_loading; public final static int SET_NEWSLIST = 0; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub initData(); super.onCreate(savedInstanceState); } @Override public void setUserVisibleHint(boolean isVisibleToUser) { if (isVisibleToUser) { //fragment可見 listView.setAdapter(adapter); handler.obtainMessage(SET_NEWSLIST).sendToTarget(); }else{ //fragment不可見 } super.setUserVisibleHint(isVisibleToUser); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub FetalExpandInfoAdapter adapter = new FetalExpandInfoAdapter(getActivity()); listView.setOnGroupExpandListener(new OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { if (lastPoint != groupPosition) { listView.collapseGroup(lastPoint); lastPoint = groupPosition; } } }); return view; } private void initData() { } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) { case SET_NEWSLIST: mBarCharts.showBarChart(mBarChart, mBarData); break; default: break; } super.handleMessage(msg); } }; @Override public void onDestroyView() { // TODO Auto-generated method stub super.onDestroyView(); } @Override public void onClick(View v) { } }
經過setUserVisibleHint 來實現延時加載,用戶可見界面時在顯示出來android
ViewPagerAdapter
app
package help.huhu.hhyy.tool.fetalmove; import java.util.ArrayList; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentTransaction; import android.view.ViewGroup; public class NewsFragmentPagerAdapter extends FragmentPagerAdapter { private ArrayList<Fragment> fragments; private FragmentManager fm; public NewsFragmentPagerAdapter(FragmentManager fm) { super(fm); this.fm = fm; } public NewsFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments) { super(fm); this.fm = fm; this.fragments = fragments; } @Override public int getCount() { return fragments.size(); } @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getItemPosition(Object object) { return POSITION_NONE; } public void setFragments(ArrayList<Fragment> fragments) { if (this.fragments != null) { FragmentTransaction ft = fm.beginTransaction(); for (Fragment f : this.fragments) { ft.remove(f); } ft.commit(); ft = null; fm.executePendingTransactions(); } this.fragments = fragments; notifyDataSetChanged(); } @Override public Object instantiateItem(ViewGroup container, final int position) { Object obj = super.instantiateItem(container, position); return obj; } }
應用例子
ide
package help.huhu.hhyy.tool.fetalmove; import java.util.ArrayList; import help.huhu.androidframe.base.activity.BaseActivity; import help.huhu.androidframe.base.activity.navigation.OnNavigationListener; import help.huhu.androidframe.util.unit.UnitUtil; import help.huhu.hhyy.R; import help.huhu.hhyy.app.APPApplication; import com.cicue.tools.FindView; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class FetalMoveActivity extends BaseActivity implements OnNavigationListener,OnClickListener{ private ViewPager viewPager; private TextView tabLine; // tab下方滑動線滑動步長 private int offset; private int tabLineWidth; //計數 private Button countBtn; //統計 private Button sumBtn; private ArrayList<Fragment> fragments = new ArrayList<Fragment>(); @Override public void drawView(int drawCode, Object data) { } @Override protected void create(Bundle savedInstanceState) { setContentView(R.layout.activity_countmove); initView(); } private void initView() { countBtn = (Button) findViewById(R.id.fetalmove_count_btn); countBtn.setOnClickListener(this); sumBtn = FindView.byId(getWindow(), R.id.fetalmove_sum_btn); sumBtn.setOnClickListener(this); viewPager = (ViewPager) findViewById(R.id.fetalmove_viewpager); FetalMoveCountFragment countFragment = new FetalMoveCountFragment(); FetalMoveSumFragment sumFragment = new FetalMoveSumFragment(); fragments.add(countFragment); fragments.add(sumFragment); NewsFragmentPagerAdapter pagerAdapter = new NewsFragmentPagerAdapter(getSupportFragmentManager(),fragments); viewPager.setAdapter(pagerAdapter); viewPager.setOnPageChangeListener(pageListener); // line tabLine = (TextView) findViewById(R.id.fetalmove_scroll_line); tabLineWidth = tabLine.getLayoutParams().width; offset = (UnitUtil.windowPixels(this)[0]/2 - tabLineWidth)/2; // 設置動畫初始位置 moveTabLine(offset); } public OnPageChangeListener pageListener= new OnPageChangeListener(){ @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int index) { // TODO Auto-generated method stub moveTabLine((2 * offset + tabLineWidth) * index + offset); } }; /** * 移動tab下劃線 * * @param width */ private void moveTabLine(int width) { LinearLayout.LayoutParams tabLineLayoutParams = new LinearLayout.LayoutParams(tabLine.getLayoutParams()); tabLineLayoutParams.leftMargin = width; tabLine.setLayoutParams(tabLineLayoutParams); } @Override public void onNavigationClick(View view, int index) { switch (index) { case 0: finish(); break; } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.fetalmove_count_btn: viewPager.setCurrentItem(0); break; case R.id.fetalmove_sum_btn: viewPager.setCurrentItem(1); break; default: break; } } }