fragment:ide
/** * author: Created by zzl on 15/11/19. */ @SuppressLint("validFragment") public class pushMealHistoryFragment extends Fragment { Context ctxt; View gridALl; public pushMealHistoryFragment(Context ctxt) { this.ctxt = ctxt; } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { gridALl = inflater.inflate(R.layout.fragment_push_meal_history, null); return gridALl; } public void updateGridview() { final GridView gridviewHistory = (GridView)gridALl.findViewById(R.id.gridview_pushing_history); final Button btnBackground = (Button)gridALl.findViewById(R.id.empty_meal_done_history_gridview); ApisManager.getMealPushedHistory(CommonUtils.getFormatDate(0), new ApiCallback() { @Override public void success(final Object object) { getActivity().runOnUiThread(new Runnable() { @Override public void run() { HandlerUtils.showToast(ctxt, "hello,i'm refreshing..."); List<PushHistoryFood> list = (List<PushHistoryFood>) object; PushHistoryAdapterr adapter = new PushHistoryAdapterr(ctxt, list); gridviewHistory.setAdapter(adapter); if (list.size() == 0) { btnBackground.setVisibility(View.VISIBLE); } else { btnBackground.setVisibility(View.INVISIBLE); } } }); } @Override public void error(BaseApi.ApiResponse response) { } }); } }
若是要在activity中調用上面的fragment的updateGridview方法,demo:this
/** * author: Created by zzl on 15/11/19. */ public class PushMealActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mealdone); init(); } public void init() { Fragment fragmentPushMealHistory = new pushMealHistoryFragment(ctxt); //在這裏調用fragmentPushMealHistory的updateGridview方法 ((pushMealHistoryFragment)fragmentPushMealHistory).updateGridview(); } }
2015.11.27更新:spa
根本緣由是fragmentPushMealHistory定義的類型爲Fragment,若是爲pushMealHistoryFragment,則不須要作強制轉換了code