Back to project page kluster-android.
The source code is released under:
Apache License
If you think the Android project kluster-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2010 The Android Open Source Project */*www. ja v a2s . co m*/ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.app; import android.os.Bundle; import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.GridView; import android.widget.ListAdapter; import android.widget.TextView; import com.cs446.kluster.R; public class GridFragment extends Fragment { private ListAdapter mAdapter; private CharSequence mEmptyText; private View mEmptyView; private GridView mGrid; private View mGridContainer; private boolean mGridShown; final private Handler mHandler = new Handler(); final private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { onGridItemClick((GridView) parent, v, position, id); } }; private View mProgressContainer; final private Runnable mRequestFocus = new Runnable() { @Override public void run() { mGrid.focusableViewAvailable(mGrid); } }; private TextView mStandardEmptyView; private void ensureGrid() { if (mGrid != null) { return; } View root = getView(); if (root == null) { throw new IllegalStateException("Content view not yet created"); } if (root instanceof GridView) { mGrid = (GridView) root; } else { mStandardEmptyView = (TextView) root .findViewById(R.id.internalEmpty); if (mStandardEmptyView == null) { mEmptyView = root.findViewById(android.R.id.empty); } else { mStandardEmptyView.setVisibility(View.GONE); } mProgressContainer = root.findViewById(R.id.progressContainer); mGridContainer = root.findViewById(R.id.listContainer); View rawGridVIew = root.findViewById(android.R.id.list); if (!(rawGridVIew instanceof GridView)) { if (rawGridVIew == null) { throw new RuntimeException( "Your content must have a GridVIew whose id attribute is " + "'android.R.id.list'"); } throw new RuntimeException( "Content has view with id attribute 'android.R.id.list' " + "that is not a GridVIew class"); } mGrid = (GridView) rawGridVIew; if (mEmptyView != null) { mGrid.setEmptyView(mEmptyView); } else if (mEmptyText != null) { mStandardEmptyView.setText(mEmptyText); mGrid.setEmptyView(mStandardEmptyView); } } mGridShown = true; mGrid.setOnItemClickListener(mOnClickListener); if (mAdapter != null) { ListAdapter adapter = mAdapter; mAdapter = null; setGridAdapter(adapter); } else { if (mProgressContainer != null) { setGridShown(false, false); } } mHandler.post(mRequestFocus); } protected View getEmptyView() { return mEmptyView; } public GridView getGridView() { ensureGrid(); return mGrid; } public ListAdapter getListAdapter() { return mAdapter; } public long getSelectedItemId() { ensureGrid(); return mGrid.getSelectedItemId(); } public int getSelectedItemPosition() { ensureGrid(); return mGrid.getSelectedItemPosition(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.grid_content, container, false); } @Override public void onDestroyView() { mHandler.removeCallbacks(mRequestFocus); mGrid = null; mGridShown = false; mEmptyView = mProgressContainer = mGridContainer = null; mStandardEmptyView = null; super.onDestroyView(); } public void onGridItemClick(GridView l, View v, int position, long id) { } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ensureGrid(); } public void setEmptyText(CharSequence text) { ensureGrid(); if (mStandardEmptyView == null) { throw new IllegalStateException( "Can't be used with a custom content view"); } mStandardEmptyView.setText(text); if (mEmptyText == null) { mGrid.setEmptyView(mStandardEmptyView); } mEmptyText = text; } public void setGridAdapter(ListAdapter adapter) { boolean hadAdapter = mAdapter != null; mAdapter = adapter; if (mGrid != null) { mGrid.setAdapter(adapter); if (!mGridShown && !hadAdapter) { // The list was hidden, and previously didn't have an // adapter. It is now time to show it. setGridShown(true, getView().getWindowToken() != null); } } } public void setGridShown(boolean shown) { setGridShown(shown, true); } private void setGridShown(boolean shown, boolean animate) { ensureGrid(); if (mProgressContainer == null) { throw new IllegalStateException( "Can't be used with a custom content view"); } if (mGridShown == shown) { return; } mGridShown = shown; if (shown) { if (animate) { mProgressContainer.startAnimation(AnimationUtils.loadAnimation( getActivity(), android.R.anim.fade_out)); mGridContainer.startAnimation(AnimationUtils.loadAnimation( getActivity(), android.R.anim.fade_in)); } else { mProgressContainer.clearAnimation(); mGridContainer.clearAnimation(); } mProgressContainer.setVisibility(View.GONE); mGridContainer.setVisibility(View.VISIBLE); } else { if (animate) { mProgressContainer.startAnimation(AnimationUtils.loadAnimation( getActivity(), android.R.anim.fade_in)); mGridContainer.startAnimation(AnimationUtils.loadAnimation( getActivity(), android.R.anim.fade_out)); } else { mProgressContainer.clearAnimation(); mGridContainer.clearAnimation(); } mProgressContainer.setVisibility(View.VISIBLE); mGridContainer.setVisibility(View.GONE); } } public void setGridShownNoAnimation(boolean shown) { setGridShown(shown, false); } public void setSelection(int position) { ensureGrid(); mGrid.setSelection(position); } }