public class MainApplicationFragment extends Fragment {
private static final float APP_PAGE_SIZE = 16.0f;
private MyViewPagerAdapter adapter;
private ArrayList<GridView> array;
private ViewPager viewPager;
private LinearLayout ll_dot;
private ImageView[] dots;
/** ViewPager当前页 */
private int currentIndex;
private int pageCount;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_main_applications, container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewPager = (ViewPager) getActivity().findViewById(R.id.main_applications_viewpager);
ll_dot = (LinearLayout) view.findViewById(R.id.main_application_points);
initViews();
initDots();
adapter = new MyViewPagerAdapter(getActivity(), array);
viewPager.setAdapter(adapter);
}
/**
* 获取系统所有的应用程序,并根据APP_PAGE_SIZE生成相应的GridView页面
*/
public void initViews() {
final PackageManager packageManager = getActivity().getPackageManager();
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// get all apps
final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
// the total pages
pageCount = (int) Math.ceil(apps.size() / APP_PAGE_SIZE);
array = new ArrayList<GridView>();
for (int i = 0; i < pageCount; i++) {
GridView appPage = new GridView(getActivity());
appPage.setVerticalSpacing(25);
appPage.setHorizontalSpacing(10);
appPage.setAdapter(new AppAdapter(getActivity(), apps, i));
appPage.setNumColumns(4);
array.add(appPage);
}
}
// 初始化底部小圆点
private void initDots() {
// 根据屏幕宽度高度计算pageItemCount
WindowManager mWindowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
@SuppressWarnings("deprecation")
int width = mWindowManager.getDefaultDisplay().getWidth();
int high = mWindowManager.getDefaultDisplay().getHeight();
if (0 < pageCount) {
/* if (ll_dot.getChildCount() != 0) {
ll_dot.removeAllViews();
}*/
ll_dot.removeAllViews();
if (1 == pageCount) {
ll_dot.setVisibility(View.GONE);
} else if (1 < pageCount) {
ll_dot.setVisibility(View.VISIBLE);
for (int j = 0; j < pageCount; j++) {
ImageView image = new ImageView(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(15, 15);
params.setMargins(8, 0, 8, 0);
image.setBackgroundResource(R.drawable.guide_dot_selector);
ll_dot.addView(image, params);
}
}
}
if (pageCount != 1) {
dots = new ImageView[pageCount];
for (int i = 0; i < pageCount; i++) {
dots[i] = (ImageView) ll_dot.getChildAt(i);
dots[i].setEnabled(true);
dots[i].setTag(i);
}
currentIndex = 0;
dots[currentIndex].setEnabled(false);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
setCurDot(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
}
/** 当前底部小圆点 */
private void setCurDot(int positon) {
if (positon < 0 || positon > pageCount - 1 || currentIndex == positon) {
return;
}
dots[positon].setEnabled(false);
dots[currentIndex].setEnabled(true);
currentIndex = positon;
}
}
AppAdapter
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = null;
View view = null;
if (convertView == null || convertView.getTag() == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.app_item, parent, false);
holder = new Holder(view);
view.setTag(holder);
} else {
view = convertView;
holder = (Holder) convertView.getTag();
}
ResolveInfo appInfo = mList.get(position);
holder.appicon.setImageDrawable(appInfo.loadIcon(pm));
holder.appname.setText(appInfo.loadLabel(pm));
return view;
}
public class Holder {
public ImageView appicon;
public TextView appname;
public Holder(View view) {
appicon = (ImageView) view.findViewById(R.id.ivAppIcon);
appname = (TextView) view.findViewById(R.id.tvAppName);
}
}