|
| 1 | +/* |
| 2 | + * Copyright (C) 2013 Evgeny Shishkin |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.devspark.progressfragment.sample; |
| 18 | + |
| 19 | +import android.os.Bundle; |
| 20 | +import android.os.Handler; |
| 21 | +import android.view.Menu; |
| 22 | +import android.view.MenuInflater; |
| 23 | +import android.view.MenuItem; |
| 24 | +import android.view.View; |
| 25 | +import android.widget.ArrayAdapter; |
| 26 | + |
| 27 | +import com.devspark.progressfragment.ProgressGridFragment; |
| 28 | + |
| 29 | +/** |
| 30 | + * Sample implementation of {@link com.devspark.progressfragment.ProgressGridFragment}. |
| 31 | + * |
| 32 | + * @author Evgeny Shishkin |
| 33 | + */ |
| 34 | +public class DefaultProgressGridFragment extends ProgressGridFragment { |
| 35 | + private Handler mHandler; |
| 36 | + private Runnable mShowContentRunnable = new Runnable() { |
| 37 | + |
| 38 | + @Override |
| 39 | + public void run() { |
| 40 | + setGridAdapter(new ArrayAdapter<String>(getActivity(), |
| 41 | + android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.sweets))); |
| 42 | + setGridShown(true); |
| 43 | + } |
| 44 | + |
| 45 | + }; |
| 46 | + |
| 47 | + public static DefaultProgressGridFragment newInstance() { |
| 48 | + DefaultProgressGridFragment fragment = new DefaultProgressGridFragment(); |
| 49 | + return fragment; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void onCreate(Bundle savedInstanceState) { |
| 54 | + super.onCreate(savedInstanceState); |
| 55 | + setHasOptionsMenu(true); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void onViewCreated(View view, Bundle savedInstanceState) { |
| 60 | + super.onViewCreated(view, savedInstanceState); |
| 61 | + getGridView().setNumColumns(2); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void onActivityCreated(Bundle savedInstanceState) { |
| 66 | + super.onActivityCreated(savedInstanceState); |
| 67 | + // Setup text for empty content |
| 68 | + setEmptyText(R.string.empty); |
| 69 | + obtainData(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void onDestroyView() { |
| 74 | + super.onDestroyView(); |
| 75 | + mHandler.removeCallbacks(mShowContentRunnable); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 80 | + inflater.inflate(R.menu.refresh, menu); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 85 | + switch (item.getItemId()) { |
| 86 | + case R.id.menu_refresh: |
| 87 | + obtainData(); |
| 88 | + return true; |
| 89 | + default: |
| 90 | + return super.onOptionsItemSelected(item); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private void obtainData() { |
| 95 | + // Show indeterminate progress |
| 96 | + setGridShown(false); |
| 97 | + |
| 98 | + mHandler = new Handler(); |
| 99 | + mHandler.postDelayed(mShowContentRunnable, 3000); |
| 100 | + } |
| 101 | +} |
0 commit comments