试试这个
主要片段
public class Fragment_Main_Dashboard extends Fragment {
GridView dashbord_icon;
List<ItemObj_for_Dashbord_list> dashbord_list1 = getAllItemObject();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dashbord_alert_list, container, false);
setHasOptionsMenu(true);
getActivity().setTitle("Dashboard");
Intent intent = new Intent(getActivity(), Service_for_login_check.class);
getActivity().startService(intent);
dashbord_icon = (GridView) view.findViewById(R.id.dashboard_icon);
Custom_Adapter_Dashboard_grid adapter1 = new Custom_Adapter_Dashboard_grid(getActivity(), dashbord_list1);
dashbord_icon.setAdapter(adapter1);
dashbord_icon.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object o = dashbord_icon.getItemAtPosition(position);
if (position == 0) {
Fragment fragment = new Fragment_Main_Advance_tracking();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.texeting_fram, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else if (position == 1) {
Fragment fragment = new Fragment_Main_Vehicle_Status();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.texeting_fram, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else if (position == 2) {
Intent All_veickle = new Intent(getActivity(), Activity_Drawer_Dashboard.class);
Boolean key = true;
All_veickle.putExtra("total_veickle", key);
startActivity(All_veickle);
getActivity().finish();
} else if (position == 3) {
Fragment fragment = new Fragment_Main_Alerts();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.texeting_fram, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else if (position == 4) {
Fragment fragment = new Fragment_Main_VEHICLE_Setting();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.texeting_fram, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else {
Fragment fragment = new Fragment_Main_Support();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.texeting_fram, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
});
return view;
}
private List<ItemObj_for_Dashbord_list> getAllItemObject() {
List<ItemObj_for_Dashbord_list> items = new ArrayList<>();
items.add(new ItemObj_for_Dashbord_list(R.drawable.dashbord_icon1, "ADVANCE TRACKING", 0));
items.add(new ItemObj_for_Dashbord_list(R.drawable.dashbord_icon2, "VEHICLE STATUS", 0));
items.add(new ItemObj_for_Dashbord_list(R.drawable.dashbord_icon4, "TOTAL VEHICLES", 0));
items.add(new ItemObj_for_Dashbord_list(R.drawable.dashbord_icon3, "ALERTS", 0));
items.add(new ItemObj_for_Dashbord_list(R.drawable.dashbord_icon5, "VEHICLE SETTINGS", 0));
items.add(new ItemObj_for_Dashbord_list(R.drawable.dashbord_icon6, "SUPPORT", 0));
return items;
}
//****************this is for inflatting nodification count funtionality
public Drawable buildCounterDrawable(int count, int backgroundImageId, Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.menu_counter_menuitem_layout, null);
view.setBackgroundResource(backgroundImageId);
if (count == 0 || count >= 100) {
View counterTextPanel = view.findViewById(R.id.counterValuePanel);
counterTextPanel.setVisibility(View.GONE);
} else {
TextView textView = (TextView) view.findViewById(R.id.count);
textView.setText("" + count);
}
view.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return new BitmapDrawable(getResources(), bitmap);
}
@Override
public void onCreateOptionsMenu(
Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
menu.clear();
MenuItem item =
menu.add(Menu.FIRST, R.id.dashboard_nodifycation, 3, "NODIFICATION");
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.FIRST, R.id.System_log, 4, "LOGOUT");
Database_for_GCM_data dbHelper = null;
dbHelper = new Database_for_GCM_data(getActivity());
int unread_count = Integer.parseInt(dbHelper.getUnreadCount());
// calling function for nodification inflater
item.setIcon(buildCounterDrawable(unread_count, R.drawable.white_nodification, getActivity()));
}
}
片段 xml 布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<GridView
android:id="@+id/dashboard_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffffff"
android:columnWidth="90dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:horizontalSpacing="1dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp"></GridView>
</LinearLayout>
自定义适配器
public class Custom_Adapter_Dashboard_grid extends BaseAdapter {
private LayoutInflater layoutinflater;
private List<ItemObj_for_Dashbord_list> listStorage;
private Context context;
public Custom_Adapter_Dashboard_grid(Context context, List<ItemObj_for_Dashbord_list> customizedListView) {
this.context = context;
try {
layoutinflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listStorage = customizedListView;
} catch (Exception e) {
}
}
@Override
public int getCount() {
return listStorage.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder listViewHolder;
if (convertView == null) {
listViewHolder = new ViewHolder();
convertView = layoutinflater.inflate(R.layout.custom_adapter_dashbord_grid_list, parent, false);
listViewHolder.screenShot = (ImageView) convertView.findViewById(R.id.icon_image);
listViewHolder.musicName = (TextView) convertView.findViewById(R.id.icon_name);
convertView.setTag(listViewHolder);
} else {
listViewHolder = (ViewHolder) convertView.getTag();
}
listViewHolder.screenShot.setImageResource(listStorage.get(position).getimage());
listViewHolder.musicName.setText(listStorage.get(position).geticonName());
return convertView;
}
static class ViewHolder {
ImageView screenShot;
TextView musicName;
}
}
自定义适配器布局
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="8dp">
<ImageView
android:id="@+id/icon_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:contentDescription="@string/hello_world"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/icon_name"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_below="@+id/icon_image"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:text="sample"
android:textSize="12sp" />
</LinearLayout>
项目对象
public class ItemObj_for_Dashbord_list {
private int dashbord;
private String iconname1;
String unread;
public ItemObj_for_Dashbord_list(int screenShot, String iconname, int unreadcound) {
dashbord = screenShot;
iconname1 = iconname;
unread = String.valueOf(unreadcound);
}
public int getimage() {
return dashbord;
}
public String geticonName() {
return iconname1;
}
public String getunreadcount() {
return unread;
}
}