|
| 1 | +package com.etsy.android.sample; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.support.v4.app.Fragment; |
| 5 | +import android.support.v4.app.FragmentActivity; |
| 6 | +import android.support.v4.app.FragmentManager; |
| 7 | +import android.util.Log; |
| 8 | +import android.view.*; |
| 9 | +import android.widget.AbsListView; |
| 10 | +import android.widget.AdapterView; |
| 11 | +import android.widget.TextView; |
| 12 | +import android.widget.Toast; |
| 13 | +import com.etsy.android.grid.StaggeredGridView; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | + |
| 17 | +public class StaggeredGridActivityFragment extends FragmentActivity { |
| 18 | + |
| 19 | + private static final String TAG = "StaggeredGridActivityFragment"; |
| 20 | + |
| 21 | + @Override |
| 22 | + protected void onCreate(Bundle savedInstanceState) { |
| 23 | + super.onCreate(savedInstanceState); |
| 24 | + |
| 25 | + setTitle("SGV"); |
| 26 | + |
| 27 | + final FragmentManager fm = getSupportFragmentManager(); |
| 28 | + |
| 29 | + // Create the list fragment and add it as our sole content. |
| 30 | + if (fm.findFragmentById(android.R.id.content) == null) { |
| 31 | + final StaggeredGridFragment fragment = new StaggeredGridFragment(); |
| 32 | + fm.beginTransaction().add(android.R.id.content, fragment).commit(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + private class StaggeredGridFragment extends Fragment implements |
| 38 | + AbsListView.OnScrollListener, AbsListView.OnItemClickListener { |
| 39 | + |
| 40 | + |
| 41 | + private StaggeredGridView mGridView; |
| 42 | + private boolean mHasRequestedMore; |
| 43 | + private SampleAdapter mAdapter; |
| 44 | + |
| 45 | + private ArrayList<String> mData; |
| 46 | + |
| 47 | + @Override |
| 48 | + public void onCreate(final Bundle savedInstanceState) { |
| 49 | + super.onCreate(savedInstanceState); |
| 50 | + setRetainInstance(true); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { |
| 55 | + return inflater.inflate(R.layout.activity_sgv, container, false); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void onActivityCreated(final Bundle savedInstanceState) { |
| 60 | + super.onActivityCreated(savedInstanceState); |
| 61 | + |
| 62 | + mGridView = (StaggeredGridView) getView().findViewById(R.id.grid_view); |
| 63 | + |
| 64 | + if (savedInstanceState == null) { |
| 65 | + final LayoutInflater layoutInflater = getActivity().getLayoutInflater(); |
| 66 | + |
| 67 | + View header = layoutInflater.inflate(R.layout.list_item_header_footer, null); |
| 68 | + View footer = layoutInflater.inflate(R.layout.list_item_header_footer, null); |
| 69 | + TextView txtHeaderTitle = (TextView) header.findViewById(R.id.txt_title); |
| 70 | + TextView txtFooterTitle = (TextView) footer.findViewById(R.id.txt_title); |
| 71 | + txtHeaderTitle.setText("THE HEADER!"); |
| 72 | + txtFooterTitle.setText("THE FOOTER!"); |
| 73 | + |
| 74 | + mGridView.addHeaderView(header); |
| 75 | + mGridView.addFooterView(footer); |
| 76 | + } |
| 77 | + |
| 78 | + if (mAdapter == null) { |
| 79 | + mAdapter = new SampleAdapter(getActivity(), R.id.txt_line1); |
| 80 | + } |
| 81 | + |
| 82 | + if (mData == null) { |
| 83 | + mData = SampleData.generateSampleData(); |
| 84 | + } |
| 85 | + |
| 86 | + for (String data : mData) { |
| 87 | + mAdapter.add(data); |
| 88 | + } |
| 89 | + |
| 90 | + mGridView.setAdapter(mAdapter); |
| 91 | + mGridView.setOnScrollListener(this); |
| 92 | + mGridView.setOnItemClickListener(this); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void onScrollStateChanged(final AbsListView view, final int scrollState) { |
| 97 | + Log.d(TAG, "onScrollStateChanged:" + scrollState); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount, final int totalItemCount) { |
| 102 | + Log.d(TAG, "onScroll firstVisibleItem:" + firstVisibleItem + |
| 103 | + " visibleItemCount:" + visibleItemCount + |
| 104 | + " totalItemCount:" + totalItemCount); |
| 105 | + // our handling |
| 106 | + if (!mHasRequestedMore) { |
| 107 | + int lastInScreen = firstVisibleItem + visibleItemCount; |
| 108 | + if (lastInScreen >= totalItemCount) { |
| 109 | + Log.d(TAG, "onScroll lastInScreen - so load more"); |
| 110 | + mHasRequestedMore = true; |
| 111 | + onLoadMoreItems(); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private void onLoadMoreItems() { |
| 117 | + final ArrayList<String> sampleData = SampleData.generateSampleData(); |
| 118 | + for (String data : sampleData) { |
| 119 | + mAdapter.add(data); |
| 120 | + } |
| 121 | + // stash all the data in our backing store |
| 122 | + mData.addAll(sampleData); |
| 123 | + // notify the adapter that we can update now |
| 124 | + mAdapter.notifyDataSetChanged(); |
| 125 | + mHasRequestedMore = false; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { |
| 130 | + Toast.makeText(getActivity(), "Item Clicked: " + position, Toast.LENGTH_SHORT).show(); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments