addOnLoadMoreRequestListener method. When
24 | * the onLoadMoreRequest event occurs, that object's appropriate
25 | * method is invoked.
26 | *
27 | * @see OnLoadMoreRequestEvent
28 | */
29 | public interface OnLoadMoreRequestListener {
30 |
31 | /**
32 | * On load more requested.
33 | */
34 | public abstract void onLoadMoreRequested();
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/gridlistviewadaptersdemo/ParentDemoMenuList.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadaptersdemo;
17 |
18 | public class ParentDemoMenuList extends BaseDemoMenuList {
19 |
20 | private final Demo>[] AVAILABLE_DEMOS = {
21 | new Demo(
22 | "ListGridAdapter Demo\nUsed for java.util.List data\n("
23 | + JavaUtilListDataDemos.AVAILABLE_DEMOS.length
24 | + " Demos)", JavaUtilListDataDemos.class),
25 | new Demo(
26 | "CursorGridAdapter Demo\nUsed for android.database.Cursor data\n("
27 | + CursorDataDemos.AVAILABLE_DEMOS.length
28 | + " Demos)", CursorDataDemos.class),
29 |
30 | };
31 |
32 | @Override
33 | protected Demo>[] getDemos() {
34 | return AVAILABLE_DEMOS;
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdapters/src/com/birin/gridlistviewadapters/RowViewHolder.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadapters;
17 |
18 | import java.util.ArrayList;
19 |
20 | /**
21 | * The Class RowViewHolder which holds together all CardViewHolders present in a
22 | * single row.
23 | *
24 | * @param
25 | * the CardViewHolder type which is going to be held in this class.
26 | */
27 | class RowViewHolder {
28 |
29 | /**
30 | * The card view holders list, this list holds all the CardViewHolders
31 | * present in a single row.
32 | */
33 | private ArrayList cardViewHolders = new ArrayList();
34 |
35 | /**
36 | * Gets the card view holders list
37 | *
38 | * @return the card view holders
39 | */
40 | public ArrayList getCardViewHolders() {
41 | return cardViewHolders;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Studio/GridListViewAdapters/gridlistviewadapters/src/main/java/com/birin/gridlistviewadapters/dataholders/RowDataHolder.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadapters.dataholders;
17 |
18 | import java.util.ArrayList;
19 |
20 | /**
21 | * This class holds list of card's positional properties. This class bundles
22 | * {@link CardDataHolder}. This RowDataHolder class corresponds to positional
23 | * properties of a single row, This List of RowDataHolder will be directly fed to
24 | * GridAdapters to render UI.
25 | *
26 | * @see CardPositionInfo
27 | */
28 | public final class RowDataHolder {
29 |
30 | private ArrayList cardPositionInfos = new ArrayList();
31 |
32 | public ArrayList getCardPositionInfos() {
33 | return cardPositionInfos;
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/res/layout/load_more_button_footer.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
22 |
23 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/listgridadapter/demo3/CardClickHandlingEmployeeListGridAdapter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.listgridadapter.demo3;
17 |
18 | import android.content.Context;
19 | import android.widget.Toast;
20 |
21 | import com.birin.listgridadapter.base.BaseEmployeeListGridAdapter;
22 | import com.birin.listgridadapter.datasetup.Employee;
23 |
24 | public class CardClickHandlingEmployeeListGridAdapter extends
25 | BaseEmployeeListGridAdapter {
26 |
27 | public CardClickHandlingEmployeeListGridAdapter(Context context,
28 | int totalItemsInRow) {
29 | super(context, totalItemsInRow);
30 | }
31 |
32 | @Override
33 | public void onCardClicked(Employee cardData) {
34 | Toast.makeText(getContext(),
35 | "Employee card clicked " + cardData.getEmployeeName(),
36 | Toast.LENGTH_LONG).show();
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/res/layout/cursor_demo_menu_screen_layout.xml:
--------------------------------------------------------------------------------
1 |
16 |
20 |
21 |
26 |
27 |
34 |
35 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/gridlistviewadaptersdemo/common/Constants.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadaptersdemo.common;
17 |
18 | import com.birin.gridlistviewadapters.utils.MaxCardsInfo;
19 |
20 | public class Constants {
21 |
22 | public static final int MAX_ITEM_IN_ROW_POTRAIT = 3;
23 | public static final int MAX_ITEM_IN_ROW_LANDSCAPE = 4;
24 |
25 | public static final int TEXT_VIEW_CLICK_EVENT_ID = 0;
26 | public static final int DUMMY_DELAY_IN_MILLIS = 1500;
27 |
28 | public static final MaxCardsInfo MAX_CARDS_INFO = new MaxCardsInfo(
29 | MAX_ITEM_IN_ROW_POTRAIT, MAX_ITEM_IN_ROW_LANDSCAPE);
30 |
31 | public static final String TAG_RETAINED_FRAGMENT = "retained_fragment";
32 |
33 | public static final String KEY_OLD_POSITION = "oldPosition";
34 | public static final String KEY_OLD_ORIENTATION = "oldOrientation";
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/Studio/GridListViewAdapters/gridlistviewadapters/src/main/java/com/birin/gridlistviewadapters/RowViewHolder.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadapters;
17 |
18 | import java.util.ArrayList;
19 |
20 | /**
21 | * The Class RowViewHolder which holds together all CardViewHolders present in a
22 | * single row.
23 | *
24 | * @param
25 | * the CardViewHolder type which is going to be held in this class.
26 | */
27 | class RowViewHolder {
28 |
29 | /**
30 | * The card view holders list, this list holds all the CardViewHolders
31 | * present in a single row.
32 | */
33 | private ArrayList cardViewHolders = new ArrayList();
34 |
35 | /**
36 | * Gets the card view holders list
37 | *
38 | * @return the card view holders
39 | */
40 | public ArrayList getCardViewHolders() {
41 | return cardViewHolders;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/listgridadapter/datasetup/Employee.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.listgridadapter.datasetup;
17 |
18 | import com.birin.gridlistviewadaptersdemo.common.RandomInfoGenerator;
19 |
20 | public class Employee {
21 |
22 | private String name;
23 | private final int iconChar;
24 | private final int iconCharColor;
25 |
26 | public Employee(int id) {
27 | name = "id:" + id;
28 | iconChar = RandomInfoGenerator.getRandomChar();
29 | iconCharColor = RandomInfoGenerator.getRandomColor();
30 | }
31 |
32 | public String getEmployeeName() {
33 | return name;
34 | }
35 |
36 | public void setEmployeeName(String employeeName) {
37 | this.name = employeeName;
38 | }
39 |
40 | public char getIconChar() {
41 | return (char) iconChar;
42 | }
43 |
44 | public int getIconCharColor() {
45 | return iconCharColor;
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/res/layout/simplest_card_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
25 |
26 |
35 |
36 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
16 |
21 |
22 |
28 |
29 |
36 |
37 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdapters/src/com/birin/gridlistviewadapters/dataholders/CardDataHolder.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadapters.dataholders;
17 |
18 | /**
19 | * This class holds single card's data & its positional properties (which is
20 | * held by {@link CardPositionInfo}), This class is passed to user to setup
21 | * views present in card.
22 | *
23 | *
24 | * @param
25 | * T signifies data type which this DataHolder class is going to
26 | * hold.
27 | *
28 | * @see CardPositionInfo
29 | */
30 | public class CardDataHolder {
31 |
32 | private T data;
33 | private CardPositionInfo cardPositionInfo;
34 |
35 | public CardDataHolder(T data, CardPositionInfo cardPositionInfo) {
36 | this.data = data;
37 | this.cardPositionInfo = cardPositionInfo;
38 | }
39 |
40 | public T getData() {
41 | return data;
42 | }
43 |
44 | public CardPositionInfo getCardInfo() {
45 | return cardPositionInfo;
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/Studio/GridListViewAdapters/gridlistviewadapters/src/main/java/com/birin/gridlistviewadapters/dataholders/CardDataHolder.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadapters.dataholders;
17 |
18 | /**
19 | * This class holds single card's data and its positional properties (which is
20 | * held by {@link CardPositionInfo}), This class is passed to user to setup
21 | * views present in card.
22 | *
23 | *
24 | * @param
25 | * T signifies data type which this DataHolder class is going to
26 | * hold.
27 | *
28 | * @see CardPositionInfo
29 | */
30 | public class CardDataHolder {
31 |
32 | private T data;
33 | private CardPositionInfo cardPositionInfo;
34 |
35 | public CardDataHolder(T data, CardPositionInfo cardPositionInfo) {
36 | this.data = data;
37 | this.cardPositionInfo = cardPositionInfo;
38 | }
39 |
40 | public T getData() {
41 | return data;
42 | }
43 |
44 | public CardPositionInfo getCardInfo() {
45 | return cardPositionInfo;
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/cursorgridadapter/demo2/CardClickHandlingEmployeeCursorGridAdapter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.cursorgridadapter.demo2;
17 |
18 | import android.content.Context;
19 | import android.database.Cursor;
20 | import android.widget.Toast;
21 |
22 | import com.birin.cursorgridadapter.base.BaseEmployeeCursorGridAdapter;
23 | import com.birin.cursorgridadapter.datasetup.TestContentProviderSqlHelper;
24 |
25 | public class CardClickHandlingEmployeeCursorGridAdapter extends
26 | BaseEmployeeCursorGridAdapter {
27 |
28 | public CardClickHandlingEmployeeCursorGridAdapter(Context context,
29 | int maxCardsInaRow, Cursor c) {
30 | super(context, maxCardsInaRow, c);
31 | }
32 |
33 | @Override
34 | public void onCardClicked(Cursor cardData) {
35 | Toast.makeText(
36 | getContext(),
37 | "Employee card clicked "
38 | + cardData.getString(cardData
39 | .getColumnIndex(TestContentProviderSqlHelper.EMPLOYEE_NAME)),
40 | Toast.LENGTH_LONG).show();
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/listgridadapter/demo7/UnlimitedListItemsRotationSupportAutoLoadMoreMax100Items.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.listgridadapter.demo7;
17 |
18 | import android.os.Bundle;
19 |
20 | import com.birin.gridlistviewadapters.utils.OnLoadMoreRequestListener;
21 | import com.birin.listgridadapter.datasetup.RetainedDataFragment;
22 | import com.birin.listgridadapter.demo6.UnlimitedListItemsRotationSupportAutoLoadMore;
23 |
24 | public class UnlimitedListItemsRotationSupportAutoLoadMoreMax100Items extends
25 | UnlimitedListItemsRotationSupportAutoLoadMore implements
26 | OnLoadMoreRequestListener {
27 |
28 | @Override
29 | protected RetainedDataFragment getRetainedDataFragment() {
30 | RetainedDataFragment retainedDataFragment = super
31 | .getRetainedDataFragment();
32 | Bundle extraArguments = new Bundle();
33 | extraArguments.putInt(RetainedDataFragment.KEY_MAX_ITEMS, 100);
34 | retainedDataFragment.setArguments(extraArguments);
35 | return retainedDataFragment;
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdapters/src/com/birin/gridlistviewadapters/utils/MaxCardsInfo.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadapters.utils;
17 |
18 | /**
19 | * This class holds number of cards that needs to be supported in portrait &
20 | * landscape modes.
21 | *
22 | */
23 | public class MaxCardsInfo {
24 |
25 | private final int potraitMaxCards;
26 | private final int landscapeMaxCards;
27 |
28 | public MaxCardsInfo(final int POTRAIT_MAX_CARDS,
29 | final int LANDSCAPE_MAX_CARD) {
30 | if (POTRAIT_MAX_CARDS <= 0 || LANDSCAPE_MAX_CARD <= 0) {
31 | throw new ExceptionInInitializerError(
32 | "Max cards should be greater than zero. Potrait Max"
33 | + POTRAIT_MAX_CARDS + " Landscape Max "
34 | + LANDSCAPE_MAX_CARD);
35 | }
36 | this.potraitMaxCards = POTRAIT_MAX_CARDS;
37 | this.landscapeMaxCards = LANDSCAPE_MAX_CARD;
38 | }
39 |
40 | public int getPotraitMaxCards() {
41 | return potraitMaxCards;
42 | }
43 |
44 | public int getLandscapeMaxCards() {
45 | return landscapeMaxCards;
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/listgridadapter/demo3/CardClickHandlingFixedListItems.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.listgridadapter.demo3;
17 |
18 | import android.os.Bundle;
19 | import android.text.TextUtils;
20 | import android.widget.Toast;
21 |
22 | import com.birin.listgridadapter.base.BaseEmployeeListGridAdapter;
23 | import com.birin.listgridadapter.demo2.FixedListItems;
24 |
25 | public class CardClickHandlingFixedListItems extends FixedListItems {
26 |
27 | @Override
28 | protected void onCreate(Bundle savedInstanceState) {
29 | super.onCreate(savedInstanceState);
30 | String message = getToastMessage();
31 | if (TextUtils.isEmpty(message) == false) {
32 | Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG)
33 | .show();
34 | }
35 | }
36 |
37 | protected String getToastMessage() {
38 | return "Click on any card...";
39 | }
40 |
41 | protected BaseEmployeeListGridAdapter getListAdapter() {
42 | return new CardClickHandlingEmployeeListGridAdapter(
43 | getApplicationContext(), getMaxCardsInRow());
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/cursorgridadapter/demo2/CardClickHandlingFixedCursorItems.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.cursorgridadapter.demo2;
17 |
18 | import android.os.Bundle;
19 | import android.text.TextUtils;
20 | import android.widget.Toast;
21 |
22 | import com.birin.cursorgridadapter.base.BaseEmployeeCursorGridAdapter;
23 | import com.birin.cursorgridadapter.demo1.FixedCursorItems;
24 |
25 | public class CardClickHandlingFixedCursorItems extends FixedCursorItems {
26 |
27 | @Override
28 | protected void onCreate(Bundle savedInstanceState) {
29 | super.onCreate(savedInstanceState);
30 | String message = getToastMessage();
31 | if (TextUtils.isEmpty(message) == false) {
32 | Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG)
33 | .show();
34 | }
35 | }
36 |
37 | protected BaseEmployeeCursorGridAdapter getListViewAdapter() {
38 | return new CardClickHandlingEmployeeCursorGridAdapter(
39 | getApplicationContext(), getMaxCardsInRow(), null);
40 | }
41 |
42 | protected String getToastMessage() {
43 | return "Click on any card...";
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/Studio/GridListViewAdapters/gridlistviewadapters/src/main/java/com/birin/gridlistviewadapters/utils/MaxCardsInfo.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
4 | * not use this file except in compliance with the License. You may obtain a
5 | * copy of the License at
6 | * http://www.apache.org/licenses/LICENSE-2.0
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations
11 | * under the License.
12 | */
13 | package com.birin.gridlistviewadapters.utils;
14 |
15 | /**
16 | * This class holds number of cards that needs to be supported in portrait and
17 | * landscape modes.
18 | */
19 | public class MaxCardsInfo {
20 |
21 | private final int potraitMaxCards;
22 | private final int landscapeMaxCards;
23 |
24 | public MaxCardsInfo(final int POTRAIT_MAX_CARDS,
25 | final int LANDSCAPE_MAX_CARD) {
26 | if (POTRAIT_MAX_CARDS <= 0 || LANDSCAPE_MAX_CARD <= 0) {
27 | throw new ExceptionInInitializerError(
28 | "Max cards should be greater than zero. Potrait Max"
29 | + POTRAIT_MAX_CARDS + " Landscape Max "
30 | + LANDSCAPE_MAX_CARD);
31 | }
32 | this.potraitMaxCards = POTRAIT_MAX_CARDS;
33 | this.landscapeMaxCards = LANDSCAPE_MAX_CARD;
34 | }
35 |
36 | public int getPotraitMaxCards() {
37 | return potraitMaxCards;
38 | }
39 |
40 | public int getLandscapeMaxCards() {
41 | return landscapeMaxCards;
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdapters/src/com/birin/gridlistviewadapters/Card.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadapters;
17 |
18 | import android.view.View;
19 |
20 | /**
21 | * The Class Card, which bundles {@link View} with its ViewHolder, which enables
22 | * user to initialize their CardView & CardViewHolder within single method call
23 | * of {@link BaseGridAdapter#getNewCard(int)}.
24 | *
25 | *
26 | * It was even possible to separate out the initialization of both components
27 | * but it was bundled together for easy-initialization of CardViewHolder in case
28 | * of which CardView was created programmatically rather than XML inflation.
29 | *
30 | *
31 | * @param
32 | * the generic type which corresponds to a ViewHolder instance for
33 | * given card.
34 | */
35 | public class Card {
36 |
37 | private View cardView;
38 |
39 | private CVH cardViewHolder;
40 |
41 | public Card(View cardView, CVH cardViewHolder) {
42 | this.cardView = cardView;
43 | this.cardViewHolder = cardViewHolder;
44 | }
45 |
46 | public View getCardView() {
47 | return cardView;
48 | }
49 |
50 | public CVH getCardViewHolder() {
51 | return cardViewHolder;
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/Studio/GridListViewAdapters/gridlistviewadapters/src/main/java/com/birin/gridlistviewadapters/Card.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadapters;
17 |
18 | import android.view.View;
19 |
20 | /**
21 | * The Class Card, which bundles {@link View} with its ViewHolder, which enables
22 | * user to initialize their CardView and CardViewHolder within single method call
23 | * of {@link BaseGridAdapter#getNewCard(int)}.
24 | *
25 | *
26 | * It was even possible to separate out the initialization of both components
27 | * but it was bundled together for easy-initialization of CardViewHolder in case
28 | * of which CardView was created programmatically rather than XML inflation.
29 | *
30 | *
31 | * @param
32 | * the generic type which corresponds to a ViewHolder instance for
33 | * given card.
34 | */
35 | public class Card {
36 |
37 | private View cardView;
38 |
39 | private CVH cardViewHolder;
40 |
41 | public Card(View cardView, CVH cardViewHolder) {
42 | this.cardView = cardView;
43 | this.cardViewHolder = cardViewHolder;
44 | }
45 |
46 | public View getCardView() {
47 | return cardView;
48 | }
49 |
50 | public CVH getCardViewHolder() {
51 | return cardViewHolder;
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/res/layout/simplest_header_footer_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
25 |
26 |
34 |
35 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdapters/src/com/birin/gridlistviewadapters/dataholders/CardPositionInfo.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadapters.dataholders;
17 |
18 | import com.birin.gridlistviewadapters.utils.GridDataStructure;
19 |
20 | /**
21 | * This class holds single card's positional properties. This class is bundled
22 | * within {@link CardDataHolder} & passed to user to setup views present in
23 | * card. This card doesn't hold any data info but it only holds card's
24 | * positional properties like card-types, card's absolute position in grid
25 | * etc.Also this class will be used when {@link GridDataStructure} creates Grid.
26 | *
27 | * @see CardPositionInfo
28 | */
29 | public final class CardPositionInfo {
30 |
31 | public static final int TYPE_CONTENT = 111;
32 | public static final int TYPE_EMPTY = 112;
33 |
34 | private int type = TYPE_EMPTY;
35 | private int absolutePositionValue = -1;
36 |
37 | public CardPositionInfo(int type, int absolutePosition) {
38 | this.type = type;
39 | this.absolutePositionValue = absolutePosition;
40 | }
41 |
42 | public int getType() {
43 | return type;
44 | }
45 |
46 | public int getAbsolutePositionValue() {
47 | return absolutePositionValue;
48 | }
49 |
50 | public boolean isEmptyCard() {
51 | return (this.type == TYPE_EMPTY);
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/Studio/GridListViewAdapters/gridlistviewadapters/src/main/java/com/birin/gridlistviewadapters/dataholders/CardPositionInfo.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadapters.dataholders;
17 |
18 | import com.birin.gridlistviewadapters.utils.GridDataStructure;
19 |
20 | /**
21 | * This class holds single card's positional properties. This class is bundled
22 | * within {@link CardDataHolder} and passed to user to setup views present in
23 | * card. This card doesn't hold any data info but it only holds card's
24 | * positional properties like card-types, card's absolute position in grid
25 | * etc.Also this class will be used when {@link GridDataStructure} creates Grid.
26 | *
27 | * @see CardPositionInfo
28 | */
29 | public final class CardPositionInfo {
30 |
31 | public static final int TYPE_CONTENT = 111;
32 | public static final int TYPE_EMPTY = 112;
33 |
34 | private int type = TYPE_EMPTY;
35 | private int absolutePositionValue = -1;
36 |
37 | public CardPositionInfo(int type, int absolutePosition) {
38 | this.type = type;
39 | this.absolutePositionValue = absolutePosition;
40 | }
41 |
42 | public int getType() {
43 | return type;
44 | }
45 |
46 | public int getAbsolutePositionValue() {
47 | return absolutePositionValue;
48 | }
49 |
50 | public boolean isEmptyCard() {
51 | return (this.type == TYPE_EMPTY);
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/Extras/Documentations/ListGridAdapterQuickUsageGuide.md:
--------------------------------------------------------------------------------
1 | Defining POJO Classes
2 | ===
3 |
4 | ```java
5 |
6 | class CardData {
7 | // Class to hold data for single card.
8 | }
9 |
10 | class CardViewHolder {
11 | // To hold views present in card.
12 | }
13 |
14 | ```
15 | Linking your POJO to Adapter is as simplest as shown below.
16 | ==
17 |
18 | ```java
19 | class MySimpleGridAdapter extends ListGridAdapter {
20 |
21 | public MySimpleGridAdapter(Context context, int totalCardsInRow) {
22 | super(context, totalCardsInRow);
23 | }
24 |
25 | @Override
26 | protected Card getNewCard(int cardwidth) {
27 | // 1.Create Card-View programmatically (can be created by XML as well.)
28 | // 2.Setting up Card view holder.
29 | return new Card(view, viewHolder);
30 | }
31 |
32 | @Override
33 | protected void setCardView(CardDataHolder cardDataHolder,
34 | CardViewHolder cardViewHolder) {
35 | // Update view values using your data & viewHolder.
36 | }
37 |
38 | @Override
39 | protected void onCardClicked(CardData cardData) {
40 | // Callback when card gets clicked.
41 | }
42 |
43 | @Override
44 | protected void registerChildrenViewClickEvents(
45 | CardViewHolder cardViewHolder,
46 | ChildViewsClickHandler childViewsClickHandler) {
47 | // Register for child-view (present inside your card) clicks if required
48 | }
49 |
50 | @Override
51 | protected void onChildViewClicked(View clickedChildView, CardData cardData,
52 | int eventId) {
53 | // Click events for registerd chilren will be posted here
54 | }
55 |
56 | }
57 | ```
58 | Linking MySimpleGridAdapter to ListView
59 | ====
60 |
61 | ```java
62 | // Instantiating adapter
63 | final int MAX_CARDS = 2;
64 | MySimpleGridAdapter gridAdapter = new MySimpleGridAdapter(getApplicationContext(), MAX_CARDS);
65 |
66 | // Adding data to adapter
67 | gridAdapter.addItemsInGrid(dataList);
68 |
69 | // Attaching adapter to ListView
70 | listview.setAdapter(gridAdapter);
71 |
72 | ```
73 |
74 |
75 | Done
76 | ====
77 |
--------------------------------------------------------------------------------
/Extras/Documentations/CursorGridAdapterQuickUsageGuide.md:
--------------------------------------------------------------------------------
1 | Defining View Holder Class
2 | ===
3 |
4 | ```java
5 |
6 | class CardViewHolder {
7 | // To hold views present in card.
8 | }
9 |
10 | ```
11 | Linking your Cursor to Adapter is as simplest as shown below.
12 | ==
13 |
14 | ```java
15 | class MyCursorGridAdapter extends CursorGridAdapter {
16 |
17 | public MyCursorGridAdapter(Context context, int totalCardsInRow, Cursor c) {
18 | super(context, totalCardsInRow, c);
19 | }
20 |
21 | @Override
22 | protected Card getNewCard(int cardwidth) {
23 | // 1.Create Card-View programmatically (can be created by XML as well.)
24 | // 2.Setting up Card view holder.
25 | return new Card(view, viewHolder);
26 | }
27 |
28 | @Override
29 | protected void setCardView(CardDataHolder cardDataHolder,
30 | CardViewHolder cardViewHolder) {
31 | // Update view values using your data & viewHolder.
32 | }
33 |
34 | @Override
35 | protected void onCardClicked(Cursor cardData) {
36 | // Callback when card gets clicked.
37 | }
38 |
39 | @Override
40 | protected void registerChildrenViewClickEvents(
41 | CardViewHolder cardViewHolder,
42 | ChildViewsClickHandler childViewsClickHandler) {
43 | // Register for child-view (present inside your card) clicks if required
44 | }
45 |
46 | @Override
47 | protected void onChildViewClicked(View clickedChildView, Cursor cardData,
48 | int eventId) {
49 | // Click events for registerd chilren will be posted here
50 | }
51 |
52 | }
53 | ```
54 | Linking MySimpleGridAdapter to ListView
55 | ====
56 |
57 | ```java
58 | // Instantiating adapter
59 | final int MAX_CARDS = 2;
60 | MyCursorGridAdapter gridAdapter = new MyCursorGridAdapter(getApplicationContext(), MAX_CARDS,null);
61 |
62 | // Attaching adapter to ListView
63 | listview.setAdapter(gridAdapter);
64 |
65 | // Adding data to adapter on callback from CursorLoader.
66 | @Override
67 | public void onLoadFinished(Loader arg0, Cursor data) {
68 | gridAdapter.swapCursor(data);
69 | }
70 |
71 | ```
72 |
73 |
74 | Done
75 | ====
76 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/res/layout/card_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
25 |
26 |
34 |
35 |
40 |
41 |
42 |
50 |
51 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/cursorgridadapter/datasetup/TestContentProviderSqlHelper.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.cursorgridadapter.datasetup;
17 |
18 | import android.content.Context;
19 | import android.database.sqlite.SQLiteDatabase;
20 | import android.database.sqlite.SQLiteOpenHelper;
21 |
22 | public class TestContentProviderSqlHelper extends SQLiteOpenHelper {
23 |
24 | public static final String DATABASE_NAME = "TestDb";
25 | public static final int DATABASE_VERSION = 1;
26 |
27 | public static final String EMPLOYEE_TABLE_NAME = "Employee";
28 | public static final String INDEX = "_id";
29 | public static final String EMPLOYEE_NAME = "name";
30 | public static final String EMPLOYEE_COLUMN_CHAR = "char";
31 | public static final String EMPLOYEE_COLUMN_CHAR_COLOR = "char_color";
32 | public static final int EMPLOYEE_TABLE_IDENTIFIER = 1;
33 |
34 | public TestContentProviderSqlHelper(Context context) {
35 | super(context, DATABASE_NAME, null, DATABASE_VERSION);
36 | }
37 |
38 | @Override
39 | public void onCreate(SQLiteDatabase db) {
40 | db.execSQL("CREATE TABLE " + EMPLOYEE_TABLE_NAME + " " + "(" + INDEX
41 | + " INTEGER PRIMARY KEY AUTOINCREMENT," + EMPLOYEE_NAME
42 | + " TEXT" + "," + EMPLOYEE_COLUMN_CHAR + " TEXT DEFAULT "
43 | + Character.toString('a') + "," + EMPLOYEE_COLUMN_CHAR_COLOR
44 | + " INTEGER DEFAULT " + Integer.toString(0) + ");");
45 | }
46 |
47 | @Override
48 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
49 | db.execSQL("DROP TABLE IF EXISTS " + EMPLOYEE_TABLE_NAME);
50 | onCreate(db);
51 | }
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/listgridadapter/demo4/ChildAndCardClickHandlingEmployeeListGridAdapter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.listgridadapter.demo4;
17 |
18 | import android.content.Context;
19 | import android.view.View;
20 | import android.widget.Toast;
21 |
22 | import com.birin.gridlistviewadapters.utils.ChildViewsClickHandler;
23 | import com.birin.gridlistviewadaptersdemo.common.Constants;
24 | import com.birin.gridlistviewadaptersdemo.common.EmployeeCardViewHolder;
25 | import com.birin.listgridadapter.datasetup.Employee;
26 | import com.birin.listgridadapter.demo3.CardClickHandlingEmployeeListGridAdapter;
27 |
28 | public class ChildAndCardClickHandlingEmployeeListGridAdapter extends
29 | CardClickHandlingEmployeeListGridAdapter {
30 |
31 | public ChildAndCardClickHandlingEmployeeListGridAdapter(Context context,
32 | int totalItemsInRow) {
33 | super(context, totalItemsInRow);
34 | }
35 |
36 | @Override
37 | protected void registerChildrenViewClickEvents(
38 | EmployeeCardViewHolder cardViewHolder,
39 | ChildViewsClickHandler childViewsClickHandler) {
40 | childViewsClickHandler
41 | .registerChildViewForClickEvent(cardViewHolder.employeeName,
42 | Constants.TEXT_VIEW_CLICK_EVENT_ID);
43 | }
44 |
45 | @Override
46 | public void onChildViewClicked(View clickedChildView, Employee cardData,
47 | int eventId) {
48 | switch (eventId) {
49 | case Constants.TEXT_VIEW_CLICK_EVENT_ID:
50 | Toast.makeText(getContext(),
51 | "Child View Clicked : " + cardData.getEmployeeName(),
52 | Toast.LENGTH_LONG).show();
53 | break;
54 | default:
55 | break;
56 | }
57 | }
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/cursorgridadapter/demo3/ChildAndCardClickHandlingEmployeeCursorGridAdapter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.cursorgridadapter.demo3;
17 |
18 | import android.content.Context;
19 | import android.database.Cursor;
20 | import android.view.View;
21 | import android.widget.Toast;
22 |
23 | import com.birin.cursorgridadapter.datasetup.TestContentProviderSqlHelper;
24 | import com.birin.cursorgridadapter.demo2.CardClickHandlingEmployeeCursorGridAdapter;
25 | import com.birin.gridlistviewadapters.utils.ChildViewsClickHandler;
26 | import com.birin.gridlistviewadaptersdemo.common.Constants;
27 | import com.birin.gridlistviewadaptersdemo.common.EmployeeCardViewHolder;
28 |
29 | public class ChildAndCardClickHandlingEmployeeCursorGridAdapter extends
30 | CardClickHandlingEmployeeCursorGridAdapter {
31 |
32 | public ChildAndCardClickHandlingEmployeeCursorGridAdapter(Context context,
33 | int maxCardsInaRow, Cursor c) {
34 | super(context, maxCardsInaRow, c);
35 | }
36 |
37 | @Override
38 | protected void registerChildrenViewClickEvents(
39 | EmployeeCardViewHolder cardViewHolder,
40 | ChildViewsClickHandler childViewsClickHandler) {
41 | childViewsClickHandler
42 | .registerChildViewForClickEvent(cardViewHolder.employeeName,
43 | Constants.TEXT_VIEW_CLICK_EVENT_ID);
44 | }
45 |
46 | @Override
47 | public void onChildViewClicked(View clickedChildView, Cursor cardData,
48 | int eventId) {
49 | switch (eventId) {
50 | case Constants.TEXT_VIEW_CLICK_EVENT_ID:
51 | Toast.makeText(
52 | getContext(),
53 | "Child View Clicked : "
54 | + cardData.getString(cardData
55 | .getColumnIndex(TestContentProviderSqlHelper.EMPLOYEE_NAME)),
56 | Toast.LENGTH_LONG).show();
57 | break;
58 | default:
59 | break;
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdapters/src/com/birin/gridlistviewadapters/CursorFilter.java:
--------------------------------------------------------------------------------
1 | package com.birin.gridlistviewadapters;
2 | /*
3 | * Copyright (C) 2007 The Android Open Source Project
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 |
19 | import android.database.Cursor;
20 | import android.widget.Filter;
21 |
22 | /**
23 | * The CursorFilter delegates most of the work to the CursorAdapter.
24 | * Subclasses should override these delegate methods to run the queries
25 | * and convert the results into String that can be used by auto-completion
26 | * widgets.
27 | */
28 | class CursorFilter extends Filter {
29 |
30 | CursorFilterClient mClient;
31 |
32 | interface CursorFilterClient {
33 | CharSequence convertToString(Cursor cursor);
34 | Cursor runQueryOnBackgroundThread(CharSequence constraint);
35 | Cursor getCursor();
36 | void changeCursor(Cursor cursor);
37 | }
38 |
39 | CursorFilter(CursorFilterClient client) {
40 | mClient = client;
41 | }
42 |
43 | @Override
44 | public CharSequence convertResultToString(Object resultValue) {
45 | return mClient.convertToString((Cursor) resultValue);
46 | }
47 |
48 | @Override
49 | protected FilterResults performFiltering(CharSequence constraint) {
50 | Cursor cursor = mClient.runQueryOnBackgroundThread(constraint);
51 |
52 | FilterResults results = new FilterResults();
53 | if (cursor != null) {
54 | results.count = cursor.getCount();
55 | results.values = cursor;
56 | } else {
57 | results.count = 0;
58 | results.values = null;
59 | }
60 | return results;
61 | }
62 |
63 | @Override
64 | protected void publishResults(CharSequence constraint, FilterResults results) {
65 | Cursor oldCursor = mClient.getCursor();
66 |
67 | if (results.values != null && results.values != oldCursor) {
68 | mClient.changeCursor((Cursor) results.values);
69 | }
70 | }
71 | }
--------------------------------------------------------------------------------
/Studio/GridListViewAdapters/gridlistviewadapters/src/main/java/com/birin/gridlistviewadapters/CursorFilter.java:
--------------------------------------------------------------------------------
1 | package com.birin.gridlistviewadapters;
2 | /*
3 | * Copyright (C) 2007 The Android Open Source Project
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 |
19 | import android.database.Cursor;
20 | import android.widget.Filter;
21 |
22 | /**
23 | * The CursorFilter delegates most of the work to the CursorAdapter.
24 | * Subclasses should override these delegate methods to run the queries
25 | * and convert the results into String that can be used by auto-completion
26 | * widgets.
27 | */
28 | class CursorFilter extends Filter {
29 |
30 | CursorFilterClient mClient;
31 |
32 | interface CursorFilterClient {
33 | CharSequence convertToString(Cursor cursor);
34 | Cursor runQueryOnBackgroundThread(CharSequence constraint);
35 | Cursor getCursor();
36 | void changeCursor(Cursor cursor);
37 | }
38 |
39 | CursorFilter(CursorFilterClient client) {
40 | mClient = client;
41 | }
42 |
43 | @Override
44 | public CharSequence convertResultToString(Object resultValue) {
45 | return mClient.convertToString((Cursor) resultValue);
46 | }
47 |
48 | @Override
49 | protected FilterResults performFiltering(CharSequence constraint) {
50 | Cursor cursor = mClient.runQueryOnBackgroundThread(constraint);
51 |
52 | FilterResults results = new FilterResults();
53 | if (cursor != null) {
54 | results.count = cursor.getCount();
55 | results.values = cursor;
56 | } else {
57 | results.count = 0;
58 | results.values = null;
59 | }
60 | return results;
61 | }
62 |
63 | @Override
64 | protected void publishResults(CharSequence constraint, FilterResults results) {
65 | Cursor oldCursor = mClient.getCursor();
66 |
67 | if (results.values != null && results.values != oldCursor) {
68 | mClient.changeCursor((Cursor) results.values);
69 | }
70 | }
71 | }
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/gridlistviewadaptersdemo/BaseDemoMenuList.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadaptersdemo;
17 |
18 | import android.app.Activity;
19 | import android.content.Context;
20 | import android.content.Intent;
21 | import android.os.Bundle;
22 | import android.view.View;
23 | import android.widget.AdapterView;
24 | import android.widget.AdapterView.OnItemClickListener;
25 | import android.widget.ArrayAdapter;
26 | import android.widget.ListView;
27 |
28 | public abstract class BaseDemoMenuList extends Activity implements
29 | OnItemClickListener {
30 |
31 | private ListView demoMenuListView;
32 |
33 | @Override
34 | protected void onCreate(Bundle savedInstanceState) {
35 | super.onCreate(savedInstanceState);
36 | setContentView(getLayoutId());
37 | demoMenuListView = (ListView) findViewById(R.id.list_view);
38 | demoMenuListView.setAdapter(new DemoMenuListAdapter(
39 | getApplicationContext()));
40 | demoMenuListView.setOnItemClickListener(this);
41 | }
42 |
43 | protected int getLayoutId() {
44 | return R.layout.demo_menu_screen_layout;
45 | }
46 |
47 | private class DemoMenuListAdapter extends ArrayAdapter> {
48 |
49 | public DemoMenuListAdapter(Context context) {
50 | super(context, R.layout.demo_menu_listrow_layout, R.id.demo_name,
51 | getDemos());
52 | }
53 | }
54 |
55 | protected abstract Demo>[] getDemos();
56 |
57 | protected static class Demo {
58 |
59 | public Demo(String menuName, Class activity) {
60 | this.menuName = menuName;
61 | this.activity = activity;
62 | }
63 |
64 | public String toString() {
65 | return menuName;
66 | };
67 |
68 | Class activity;
69 | String menuName;
70 |
71 | }
72 |
73 | @Override
74 | public void onItemClick(AdapterView> parent, View arg1, int position,
75 | long id) {
76 | if (position < getDemos().length) {
77 | this.startActivity(new Intent(this, getDemos()[position].activity));
78 | }
79 | }
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/listgridadapter/demo8/UnlimitedListItemsRotationSupportClickToLoadMore.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.listgridadapter.demo8;
17 |
18 | import android.annotation.SuppressLint;
19 | import android.graphics.Color;
20 | import android.view.View;
21 | import android.view.View.OnClickListener;
22 | import android.widget.Button;
23 | import android.widget.LinearLayout;
24 | import android.widget.ProgressBar;
25 |
26 | import com.birin.gridlistviewadaptersdemo.R;
27 | import com.birin.listgridadapter.demo6.UnlimitedListItemsRotationSupportAutoLoadMore;
28 |
29 | public class UnlimitedListItemsRotationSupportClickToLoadMore extends
30 | UnlimitedListItemsRotationSupportAutoLoadMore implements
31 | OnClickListener {
32 |
33 | private Button loadMoreButton;
34 | private ProgressBar loadingProgress;
35 |
36 | @Override
37 | public void onLoadMoreRequested() {
38 | updateLoadMoreView();
39 | }
40 |
41 | @SuppressLint("InflateParams")
42 | @Override
43 | protected View getLoadMoreView() {
44 | LinearLayout loadMoreFooter = (LinearLayout) inflater.inflate(
45 | R.layout.load_more_button_footer, null);
46 | loadMoreFooter.setBackgroundColor(Color.LTGRAY);
47 | loadMoreButton = (Button) loadMoreFooter
48 | .findViewById(R.id.load_more_btn);
49 | loadingProgress = (ProgressBar) loadMoreFooter
50 | .findViewById(R.id.loading_progress_bar);
51 | loadMoreButton.setOnClickListener(this);
52 | return loadMoreFooter;
53 | }
54 |
55 | @Override
56 | protected void updateLoadMoreView() {
57 | if (dataRetainingFragment.canLoadMoreData() == false) {
58 | listView.removeFooterView(loadMoreFooterView);
59 | } else {
60 | boolean isTaskRunning = dataRetainingFragment.isTaskRunning();
61 | loadingProgress.setVisibility(isTaskRunning ? View.VISIBLE
62 | : View.GONE);
63 | loadMoreButton
64 | .setVisibility((isTaskRunning == false) ? View.VISIBLE
65 | : View.GONE);
66 | }
67 | }
68 |
69 | @Override
70 | public void onClick(View v) {
71 | switch (v.getId()) {
72 | case R.id.load_more_btn:
73 | super.onLoadMoreRequested();
74 | break;
75 | }
76 | }
77 |
78 | }
79 |
--------------------------------------------------------------------------------
/Studio/GridListViewAdapters/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/cursorgridadapter/demo6/UnlimitedCursorItemsRotationClickToLoadMore.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.cursorgridadapter.demo6;
17 |
18 | import android.annotation.SuppressLint;
19 | import android.graphics.Color;
20 | import android.view.View;
21 | import android.view.View.OnClickListener;
22 | import android.widget.Button;
23 | import android.widget.LinearLayout;
24 | import android.widget.ProgressBar;
25 |
26 | import com.birin.cursorgridadapter.demo5.UnlimitedCursorItemsRotationSupportAutoLoadMore;
27 | import com.birin.gridlistviewadaptersdemo.R;
28 |
29 | public class UnlimitedCursorItemsRotationClickToLoadMore extends
30 | UnlimitedCursorItemsRotationSupportAutoLoadMore implements
31 | OnClickListener {
32 |
33 | private Button loadMoreButton;
34 | private ProgressBar loadingProgress;
35 |
36 | @Override
37 | public void onLoadMoreRequested() {
38 | updateLoadMoreView();
39 | }
40 |
41 | @SuppressLint("InflateParams")
42 | @Override
43 | protected View getLoadMoreView() {
44 | LinearLayout loadMoreFooter = (LinearLayout) inflater.inflate(
45 | R.layout.load_more_button_footer, null);
46 | loadMoreFooter.setBackgroundColor(Color.LTGRAY);
47 | loadMoreButton = (Button) loadMoreFooter
48 | .findViewById(R.id.load_more_btn);
49 | loadingProgress = (ProgressBar) loadMoreFooter
50 | .findViewById(R.id.loading_progress_bar);
51 | loadMoreButton.setOnClickListener(this);
52 | return loadMoreFooter;
53 | }
54 |
55 | @Override
56 | protected void updateLoadMoreView() {
57 | if (dataRetainingFragment.canLoadMoreData(listviewAdapter
58 | .getAbsoluteCardsCount()) == false) {
59 | listView.removeFooterView(loadMoreFooterView);
60 | } else {
61 | boolean isTaskRunning = dataRetainingFragment.isTaskRunning();
62 | loadingProgress.setVisibility(isTaskRunning ? View.VISIBLE
63 | : View.GONE);
64 | loadMoreButton
65 | .setVisibility((isTaskRunning == false) ? View.VISIBLE
66 | : View.GONE);
67 | }
68 | }
69 |
70 | @Override
71 | public void onClick(View v) {
72 | switch (v.getId()) {
73 | case R.id.load_more_btn:
74 | super.onLoadMoreRequested();
75 | break;
76 | }
77 | }
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/listgridadapter/demo5/FixedListItemsRotationSupport.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.listgridadapter.demo5;
17 |
18 | import android.content.res.Configuration;
19 | import android.os.Bundle;
20 |
21 | import com.birin.gridlistviewadapters.utils.PositionCalculator;
22 | import com.birin.gridlistviewadaptersdemo.common.Constants;
23 | import com.birin.listgridadapter.demo4.ChildAndCardClickHandlingFixedListItems;
24 |
25 | public class FixedListItemsRotationSupport extends
26 | ChildAndCardClickHandlingFixedListItems {
27 |
28 | // //////////////////////////////////////////////////////////////////////////////
29 | // TO MAINTAIN CORRECT POSITION AFTER ORIENTATION CHANGE
30 | // //////////////////////////////////////////////////////////////////////////////
31 |
32 | @Override
33 | protected void onCreate(Bundle savedInstanceState) {
34 | super.onCreate(savedInstanceState);
35 | adjustScroll(savedInstanceState);
36 | }
37 |
38 | private int currentOrientation = Configuration.ORIENTATION_UNDEFINED;
39 |
40 | @Override
41 | protected void onSaveInstanceState(Bundle outState) {
42 | super.onSaveInstanceState(outState);
43 | int firstVisible = listView.getFirstVisiblePosition();
44 | outState.putInt(Constants.KEY_OLD_POSITION, firstVisible);
45 | outState.putInt(Constants.KEY_OLD_ORIENTATION, currentOrientation);
46 | }
47 |
48 | private void adjustScroll(Bundle savedInstanceState) {
49 | currentOrientation = getCurrentOrientation();
50 | if (null != savedInstanceState) {
51 | int oldPosition = savedInstanceState.getInt(
52 | Constants.KEY_OLD_POSITION, 0);
53 | int oldOrientation = savedInstanceState.getInt(
54 | Constants.KEY_OLD_ORIENTATION,
55 | Configuration.ORIENTATION_UNDEFINED);
56 | final int newPostion = new PositionCalculator(
57 | Constants.MAX_CARDS_INFO)
58 | .calculatePositionInNewOrientation(oldPosition,
59 | oldOrientation, currentOrientation);
60 | listView.post(new Runnable() {
61 |
62 | @Override
63 | public void run() {
64 | listView.setSelection(newPostion);
65 | }
66 | });
67 | }
68 | }
69 |
70 | // //////////////////////////////////////////////////////////////////////////////
71 | // TO MAINTAIN CORRECT POSITION AFTER ORIENTATION CHANGE
72 | // //////////////////////////////////////////////////////////////////////////////
73 |
74 | @Override
75 | protected String getToastMessage() {
76 | // Sending null to avoid showing toast.
77 | return null;
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/cursorgridadapter/demo4/FixedCursorItemsRotationSupport.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.cursorgridadapter.demo4;
17 |
18 | import android.content.res.Configuration;
19 | import android.os.Bundle;
20 |
21 | import com.birin.cursorgridadapter.demo3.ChildAndCardClickHandlingFixedCursorItems;
22 | import com.birin.gridlistviewadapters.utils.PositionCalculator;
23 | import com.birin.gridlistviewadaptersdemo.common.Constants;
24 |
25 | public class FixedCursorItemsRotationSupport extends
26 | ChildAndCardClickHandlingFixedCursorItems {
27 |
28 | // //////////////////////////////////////////////////////////////////////////////
29 | // TO MAINTAIN CORRECT POSITION AFTER ORIENTATION CHANGE
30 | // //////////////////////////////////////////////////////////////////////////////
31 |
32 | @Override
33 | protected void onCreate(Bundle savedInstanceState) {
34 | super.onCreate(savedInstanceState);
35 | adjustScroll(savedInstanceState);
36 | }
37 |
38 | private int currentOrientation = Configuration.ORIENTATION_UNDEFINED;
39 |
40 | @Override
41 | protected void onSaveInstanceState(Bundle outState) {
42 | super.onSaveInstanceState(outState);
43 | int firstVisible = listView.getFirstVisiblePosition();
44 | outState.putInt(Constants.KEY_OLD_POSITION, firstVisible);
45 | outState.putInt(Constants.KEY_OLD_ORIENTATION, currentOrientation);
46 | }
47 |
48 | private void adjustScroll(Bundle savedInstanceState) {
49 | currentOrientation = getCurrentOrientation();
50 | if (null != savedInstanceState) {
51 | int oldPosition = savedInstanceState.getInt(
52 | Constants.KEY_OLD_POSITION, 0);
53 | int oldOrientation = savedInstanceState.getInt(
54 | Constants.KEY_OLD_ORIENTATION,
55 | Configuration.ORIENTATION_UNDEFINED);
56 | final int newPostion = new PositionCalculator(
57 | Constants.MAX_CARDS_INFO)
58 | .calculatePositionInNewOrientation(oldPosition,
59 | oldOrientation, currentOrientation);
60 | listView.post(new Runnable() {
61 |
62 | @Override
63 | public void run() {
64 | listView.setSelection(newPostion);
65 | }
66 | });
67 | }
68 | }
69 |
70 | // //////////////////////////////////////////////////////////////////////////////
71 | // TO MAINTAIN CORRECT POSITION AFTER ORIENTATION CHANGE
72 | // //////////////////////////////////////////////////////////////////////////////
73 |
74 | @Override
75 | protected String getToastMessage() {
76 | // Sending null to avoid showing toast.
77 | return null;
78 | }
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/cursorgridadapter/demo5/UnlimitedCursorItemsRotationSupportAutoLoadMore.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.cursorgridadapter.demo5;
17 |
18 | import android.content.Loader;
19 | import android.database.Cursor;
20 | import android.graphics.Color;
21 | import android.os.Bundle;
22 | import android.view.View;
23 | import android.widget.ProgressBar;
24 |
25 | import com.birin.cursorgridadapter.datasetup.CursorRetainingFragment.DummyDataGeneratorCallback;
26 | import com.birin.cursorgridadapter.demo4.FixedCursorItemsRotationSupport;
27 | import com.birin.gridlistviewadapters.utils.OnLoadMoreRequestListener;
28 | import com.birin.gridlistviewadaptersdemo.R;
29 |
30 | public class UnlimitedCursorItemsRotationSupportAutoLoadMore extends
31 | FixedCursorItemsRotationSupport implements OnLoadMoreRequestListener,
32 | DummyDataGeneratorCallback {
33 |
34 | protected View loadMoreFooterView;
35 |
36 | @Override
37 | protected void onCreate(Bundle savedInstanceState) {
38 | super.onCreate(savedInstanceState);
39 | listviewAdapter.setOnLoadMoreRequestListener(this);
40 | }
41 |
42 | @Override
43 | public void onLoadMoreRequested() {
44 | dataRetainingFragment.runAddDataTaskOnLoadMoreRequest();
45 | }
46 |
47 | @Override
48 | protected void bindAdapterToList() {
49 | loadMoreFooterView = getLoadMoreView();
50 | if (dataRetainingFragment.canLoadMoreData(listviewAdapter.getCount()) == true) {
51 | listView.addFooterView(loadMoreFooterView);
52 | updateLoadMoreView();
53 | }
54 | super.bindAdapterToList();
55 | }
56 |
57 | protected View getLoadMoreView() {
58 | ProgressBar loadMoreProgress = (ProgressBar) inflater.inflate(
59 | R.layout.auto_load_more_footer, null);
60 | loadMoreProgress.setBackgroundColor(Color.LTGRAY);
61 | return loadMoreProgress;
62 | }
63 |
64 | @Override
65 | public void onLoadFinished(Loader arg0, Cursor data) {
66 | super.onLoadFinished(arg0, data);
67 | updateLoadMoreView();
68 | }
69 |
70 | @Override
71 | public void onDataGeneratorTaskExecuting() {
72 | updateLoadMoreView();
73 | }
74 |
75 | protected void updateLoadMoreView() {
76 | if (dataRetainingFragment.canLoadMoreData(listviewAdapter.getCount()) == false) {
77 | listView.removeFooterView(loadMoreFooterView);
78 | } else {
79 | boolean isLoadingMoreData = dataRetainingFragment.isTaskRunning();
80 | if (isLoadingMoreData == true) {
81 | loadMoreFooterView.setVisibility(View.VISIBLE);
82 | } else {
83 | loadMoreFooterView.setVisibility(View.GONE);
84 | }
85 | }
86 | }
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/gridlistviewadaptersdemo/JavaUtilListDataDemos.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadaptersdemo;
17 |
18 | import com.birin.listgridadapter.demo1.SimplestListGridAdapterUsageDemoActivity;
19 | import com.birin.listgridadapter.demo2.FixedListItems;
20 | import com.birin.listgridadapter.demo3.CardClickHandlingFixedListItems;
21 | import com.birin.listgridadapter.demo4.ChildAndCardClickHandlingFixedListItems;
22 | import com.birin.listgridadapter.demo5.FixedListItemsRotationSupport;
23 | import com.birin.listgridadapter.demo6.UnlimitedListItemsRotationSupportAutoLoadMore;
24 | import com.birin.listgridadapter.demo7.UnlimitedListItemsRotationSupportAutoLoadMoreMax100Items;
25 | import com.birin.listgridadapter.demo8.UnlimitedListItemsRotationSupportClickToLoadMore;
26 |
27 | public class JavaUtilListDataDemos extends BaseDemoMenuList {
28 |
29 | public static final Demo>[] AVAILABLE_DEMOS = {
30 | // From java.util.List
31 | new Demo(
32 | "Demo 1 : Step by step guide for basic usage of ListGridAdapter + supporting header&footers"
33 | + "\n(Recommended for beginner users of GridListViewAdapters lib )",
34 | SimplestListGridAdapterUsageDemoActivity.class),
35 | new Demo("Demo 2 : Fixed Items from List",
36 | FixedListItems.class),
37 | new Demo(
38 | "Demo 3 : Fixed Items from List + Handling card clicks",
39 | CardClickHandlingFixedListItems.class),
40 | new Demo(
41 | "Demo 4 : Fixed Items from List + Handling card clicks + Handling children view clicks present in card",
42 | ChildAndCardClickHandlingFixedListItems.class),
43 | new Demo(
44 | "Demo 5 : Fixed Items from List + Rotation Support",
45 | FixedListItemsRotationSupport.class),
46 | new Demo(
47 | "Demo 6 : Unlimited Items List with auto load more + Rotation Support",
48 | UnlimitedListItemsRotationSupportAutoLoadMore.class),
49 | new Demo(
50 | "Demo 7 : Unlimited Items List with click to load more + Rotation Support",
51 | UnlimitedListItemsRotationSupportClickToLoadMore.class),
52 | new Demo(
53 | "Demo 8 : 100 Items List with auto load more + Rotation Support",
54 | UnlimitedListItemsRotationSupportAutoLoadMoreMax100Items.class) };
55 |
56 | @Override
57 | protected Demo>[] getDemos() {
58 | return AVAILABLE_DEMOS;
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/listgridadapter/demo6/UnlimitedListItemsRotationSupportAutoLoadMore.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.listgridadapter.demo6;
17 |
18 | import java.util.ArrayList;
19 |
20 | import android.annotation.SuppressLint;
21 | import android.graphics.Color;
22 | import android.os.Bundle;
23 | import android.view.View;
24 | import android.widget.ProgressBar;
25 |
26 | import com.birin.gridlistviewadapters.utils.OnLoadMoreRequestListener;
27 | import com.birin.gridlistviewadaptersdemo.R;
28 | import com.birin.listgridadapter.datasetup.Employee;
29 | import com.birin.listgridadapter.demo5.FixedListItemsRotationSupport;
30 |
31 | public class UnlimitedListItemsRotationSupportAutoLoadMore extends
32 | FixedListItemsRotationSupport implements OnLoadMoreRequestListener {
33 |
34 | protected View loadMoreFooterView;
35 |
36 | @Override
37 | protected void onCreate(Bundle savedInstanceState) {
38 | super.onCreate(savedInstanceState);
39 | gridAdapter.setOnLoadMoreRequestListener(this);
40 | }
41 |
42 | @Override
43 | public void onLoadMoreRequested() {
44 | dataRetainingFragment.runAddDataTask();
45 | }
46 |
47 | @Override
48 | protected void bindAdapterToList() {
49 | loadMoreFooterView = getLoadMoreView();
50 | if (dataRetainingFragment.canLoadMoreData() == true) {
51 | listView.addFooterView(loadMoreFooterView);
52 | updateLoadMoreView();
53 | }
54 | super.bindAdapterToList();
55 | }
56 |
57 | @SuppressLint("InflateParams")
58 | protected View getLoadMoreView() {
59 | ProgressBar loadMoreProgress = (ProgressBar) inflater.inflate(
60 | R.layout.auto_load_more_footer, null);
61 | loadMoreProgress.setBackgroundColor(Color.LTGRAY);
62 | return loadMoreProgress;
63 | }
64 |
65 | @Override
66 | public void onNewDummyDataGenerated(ArrayList newDataList) {
67 | super.onNewDummyDataGenerated(newDataList);
68 | updateLoadMoreView();
69 | }
70 |
71 | @Override
72 | public void onDataGeneratorTaskExecuting() {
73 | super.onDataGeneratorTaskExecuting();
74 | updateLoadMoreView();
75 | }
76 |
77 | @Override
78 | public void onDataGeneratorTaskCancelled() {
79 | super.onDataGeneratorTaskCancelled();
80 | updateLoadMoreView();
81 | }
82 |
83 | protected void updateLoadMoreView() {
84 | if (dataRetainingFragment.canLoadMoreData() == false) {
85 | listView.removeFooterView(loadMoreFooterView);
86 | } else {
87 | boolean isLoadingMoreData = dataRetainingFragment.isTaskRunning();
88 | if (isLoadingMoreData == true) {
89 | loadMoreFooterView.setVisibility(View.VISIBLE);
90 | } else {
91 | loadMoreFooterView.setVisibility(View.GONE);
92 | }
93 | }
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/Studio/GridListViewAdapters/gridlistviewadapters/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 | apply plugin: 'com.jfrog.bintray'
4 |
5 | version = "1.0.0"
6 |
7 | repositories {
8 | maven {
9 | url "https://dl.bintray.com/birajpatel/gridlistviewadapters"
10 | }
11 | }
12 |
13 | android {
14 | compileSdkVersion 21
15 | buildToolsVersion "21.1.2"
16 |
17 | defaultConfig {
18 | minSdkVersion 4
19 | targetSdkVersion 4
20 | }
21 |
22 | buildTypes {
23 | release {
24 | minifyEnabled false
25 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
26 | }
27 | }
28 | }
29 |
30 | def siteUrl = 'https://github.com/birajpatel/GridListViewAdapters' // Homepage URL of the library
31 | def gitUrl = 'https://github.com/birajpatel/GridListViewAdapters.git' // Git repository URL
32 | group = "com.birin" // Maven Group ID for the artifact
33 |
34 |
35 | install {
36 | repositories.mavenInstaller {
37 | // This generates POM.xml with proper parameters
38 | pom {
39 | project {
40 | packaging 'aar'
41 |
42 | // Add your description here
43 | name 'Using multi-row-type listview most easier'
44 | url siteUrl
45 |
46 | // Set your license
47 | licenses {
48 | license {
49 | name 'The Apache Software License, Version 2.0'
50 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
51 | }
52 | }
53 | developers {
54 | developer {
55 | id 'birajpatel'
56 | name 'biraj'
57 | email 'biraj.d.patel@gmail.com'
58 | }
59 | }
60 | scm {
61 | connection gitUrl
62 | developerConnection gitUrl
63 | url siteUrl
64 |
65 | }
66 | }
67 | }
68 | }
69 | }
70 |
71 | task sourcesJar(type: Jar) {
72 | from android.sourceSets.main.java.srcDirs
73 | classifier = 'sources'
74 | }
75 |
76 | task javadoc(type: Javadoc) {
77 | source = android.sourceSets.main.java.srcDirs
78 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
79 | }
80 |
81 | task javadocJar(type: Jar, dependsOn: javadoc) {
82 | classifier = 'javadoc'
83 | from javadoc.destinationDir
84 | }
85 | artifacts {
86 | archives javadocJar
87 | archives sourcesJar
88 | }
89 |
90 | Properties properties = new Properties()
91 | properties.load(project.rootProject.file('local.properties').newDataInputStream())
92 |
93 | bintray {
94 | user = properties.getProperty("bintray.user")
95 | key = properties.getProperty("bintray.apikey")
96 |
97 | configurations = ['archives']
98 | pkg {
99 | repo = "gridlistviewadapters"
100 | name = "GridListViewAdapters"
101 | websiteUrl = siteUrl
102 | vcsUrl = gitUrl
103 | licenses = ["Apache-2.0"]
104 | publish = true
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/gridlistviewadaptersdemo/common/CharacterDrawable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadaptersdemo.common;
17 |
18 | import android.annotation.SuppressLint;
19 | import android.graphics.Canvas;
20 | import android.graphics.Color;
21 | import android.graphics.ColorFilter;
22 | import android.graphics.Paint;
23 | import android.graphics.PixelFormat;
24 | import android.graphics.drawable.ColorDrawable;
25 | import android.os.Build;
26 | import android.widget.ImageView;
27 |
28 | public class CharacterDrawable extends ColorDrawable {
29 |
30 | private char character;
31 | private int color;
32 | private final Paint textPaint;
33 |
34 | public CharacterDrawable(CharacterDrawableInfo info) {
35 | super(info.color);
36 | color = info.color;
37 | character = info.newChar;
38 | textPaint = new Paint();
39 | textPaint.setColor(Color.BLACK);
40 | textPaint.setStyle(Paint.Style.FILL);
41 | textPaint.setTextAlign(Paint.Align.CENTER);
42 | textPaint.setAntiAlias(true);
43 | textPaint.setFakeBoldText(true);
44 | }
45 |
46 | @Override
47 | public void draw(Canvas canvas) {
48 | super.draw(canvas);
49 |
50 | // draw text
51 | int width = canvas.getWidth();
52 | int height = canvas.getHeight();
53 | textPaint.setTextSize(height / 2);
54 | canvas.drawText(String.valueOf(character), width / 2, height / 2
55 | - ((textPaint.descent() + textPaint.ascent()) / 2), textPaint);
56 | }
57 |
58 | @Override
59 | public void setAlpha(int alpha) {
60 | textPaint.setAlpha(alpha);
61 | }
62 |
63 | @Override
64 | public void setColorFilter(ColorFilter cf) {
65 | textPaint.setColorFilter(cf);
66 | }
67 |
68 | @Override
69 | public int getOpacity() {
70 | return PixelFormat.TRANSLUCENT;
71 | }
72 |
73 | public void update(CharacterDrawableInfo info) {
74 | if (info.newChar != character || info.color != color) {
75 | character = info.newChar;
76 | this.color = info.color;
77 | setColor(color);
78 | invalidateSelf();
79 | }
80 | }
81 |
82 | @SuppressLint("NewApi")
83 | @SuppressWarnings("deprecation")
84 | public static void tagNewDrawableToImageView(ImageView imageview) {
85 | CharacterDrawable drawable = new CharacterDrawable(
86 | new CharacterDrawableInfo());
87 | imageview.setTag(drawable);
88 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
89 | imageview.setBackground(drawable);
90 | } else {
91 | imageview.setBackgroundDrawable(drawable);
92 | }
93 | }
94 |
95 | public static void updateTaggedDrawableStateFromImageView(
96 | ImageView imageview, CharacterDrawableInfo info) {
97 | CharacterDrawable drawable = (CharacterDrawable) imageview.getTag();
98 | drawable.update(info);
99 | }
100 |
101 | public static class CharacterDrawableInfo {
102 | public int color;
103 | public char newChar;
104 | }
105 | }
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdapters/src/com/birin/gridlistviewadapters/ListGridAdapter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadapters;
17 |
18 | import java.util.ArrayList;
19 | import java.util.List;
20 |
21 | import android.content.Context;
22 |
23 | public abstract class ListGridAdapter extends BaseGridAdapter {
24 |
25 | private List dataList = new ArrayList();
26 |
27 | public ListGridAdapter(Context context, int totalCardsInRow) {
28 | super(context, totalCardsInRow);
29 | }
30 |
31 | @Override
32 | public T getCardData(int absoluteCardPosition) {
33 | validatePositionOrThrow(absoluteCardPosition);
34 | return dataList.get(absoluteCardPosition);
35 | }
36 |
37 | @Override
38 | public void validatePositionOrThrow(int position) {
39 | if (position < 0 || position >= getAbsoluteCardsCount()) {
40 | throw new IndexOutOfBoundsException("Position requested "
41 | + position + " Available list size "
42 | + getAbsoluteCardsCount());
43 | }
44 | }
45 |
46 | @Override
47 | public int getAbsoluteCardsCount() {
48 | if (null != dataList) {
49 | return dataList.size();
50 | }
51 | return 0;
52 | }
53 |
54 | /**
55 | * Adds the new item into existing list.
56 | *
57 | * @param dataList
58 | * The new data list to be appended at end of current list
59 | *
60 | * @see ListGridAdapter#swapDataList(List)
61 | *
62 | */
63 | public void addItemsInGrid(List newDataList) {
64 | int newDataSize = 0;
65 | if (newDataList != null && newDataList.isEmpty() == false) {
66 | this.dataList.addAll(newDataList);
67 | newDataSize = getAbsoluteCardsCount();
68 | }
69 | updateGridWithNewSize(newDataSize);
70 | }
71 |
72 | /**
73 | * if you want to change the complete data list then consider using this
74 | * function, this will clear current data & replace with new data list
75 | * passing null instead of valid data will simply clear whole data.
76 | *
77 | * @param dataList
78 | * the new data list.
79 | */
80 | public void swapDataList(List newDataList) {
81 | this.dataList.clear();
82 | invalidateStructure();
83 | addItemsInGrid(newDataList);
84 | }
85 |
86 | /**
87 | * Deletes item from list for given position
88 | *
89 | * @param position
90 | * the position at which item needs to be removed.
91 | */
92 | public void deleteItemFromList(int position) {
93 | validatePositionOrThrow(position);
94 | dataList.remove(position);
95 | invalidateStructure();
96 | updateGridWithNewSize(getAbsoluteCardsCount());
97 | }
98 |
99 | /**
100 | * Deletes given object from list.
101 | *
102 | * @param data
103 | * the object to be deleted.
104 | * @return true, if successful
105 | */
106 | public boolean deleteItemFromList(T data) {
107 | boolean isRemoved = dataList.remove(data);
108 | if (isRemoved == true) {
109 | invalidateStructure();
110 | updateGridWithNewSize(getAbsoluteCardsCount());
111 | }
112 | return isRemoved;
113 | }
114 |
115 | /**
116 | * Clears the complete data list & updates UI.
117 | */
118 | public void clearList() {
119 | swapDataList(null);
120 | }
121 | }
122 |
--------------------------------------------------------------------------------
/Extras/Documentations/debugging_exceptions.md:
--------------------------------------------------------------------------------
1 | Exceptions
2 | ==========
3 |
4 | 1
5 | ===
6 | **Exception** : IllegalArgumentException, Event-Id should be a positive integer
7 |
8 | **Thrower** : EfficientChildViewsClickHandler
9 |
10 | **Fix** : Whenever you are registering a child present inside row for EffecientClicking then event-Ids should be positive identifiers for click events.
11 |
12 | 2
13 | ===
14 | **Exception** : IllegalArgumentException, Unable to tag position data to getClass().getSimpleName() in your view-holder object currently only View, java.util.List of Custom-View-Wrappers, Array of Custom-View-wrappers are supported within ViewHolder class if you have custom datastructure within your ViewHolder class consider passing subclass of ViewHolderPositionTagger with your handling in handleCustomDataObject(), and passing that subclass to adapter by overriding getPositionTagger() method in your adapter this class.
15 |
16 | **Thrower** : ViewHolderPositionTagger
17 |
18 | **Fix** : Whenever you pass a ViewHolder instance, its instance is scanned (by Using Java Reflections) for "View" Object references present inside,This scanning is performed so that it can tag row position value to child views which are registered for efficient clicking, Currently library can only scan few type of objects namely
19 | 1) Views present directly as member variables in ViewHolder.
20 | 2) Array of View/View-wrapping-POJOs present as member variables in ViewHolder.
21 | 3) java.util.List of View/View-wrapping-POJOs present as member variables in ViewHolder.
22 | If you pass "Custom objects" within your View Holder then library doesn't know about your custom Objects so it will throw error to fix this error you need to make subclass of ViewHolderPositionTagger & override handleCustomDataObject() method & traverse your custom object to find Objects wrapped in it once Object is recieved call tagObjectIfViewInstanceOrScanObjectForEnclosedViews() method to tag it. Pass this custom class to adapter via overriding getPositionTagger() method.
23 |
24 | 3
25 | =
26 | **Exception** : IllegalArgumentException, Invalid number of cards in a row should be greater than 0
27 |
28 | **Thrower** : GridDataStructure
29 |
30 | **Fix** : Pass correct number of cards in your adapter. (Greater than 0)
31 |
32 | 4
33 | =
34 | **Exception** : ExceptionInInitializerError, Max cards should be greater than zero. Potrait Max # Landscape Max #
35 |
36 | **Thrower** : GridDataStructure
37 |
38 | **Fix** : Pass correct number of cards in your in MaxCardsInfo constructor (Greater than 0)
39 |
40 | 5
41 | =
42 | **Exception** : UnsupportedOperationException, Unsupported Orientation: # , Valid ones are Configuration.ORIENTATION_LANDSCAPE, Configuration.ORIENTATION_PORTRAIT
43 |
44 | **Thrower** : GridDataStructure
45 |
46 | **Fix** : Only Landscape & Portrait modes are supported.
47 |
48 |
49 | 6
50 | ===
51 | **Exception** : IllegalStateException, this should only be called when the cursor is valid
52 |
53 | **Thrower** : CursorGridAdapter
54 |
55 | **Fix** : this crash is literally very RARE & should never come in practical cases.
56 |
57 | 7
58 | ===
59 | **Exception** : IllegalStateException, couldn't move cursor to position
60 |
61 | **Thrower** : CursorGridAdapter
62 |
63 | **Fix** : this crash is literally very RARE & should never come in practical cases.
64 |
65 | 8
66 | ===
67 | **Exception** : IndexOutOfBoundsException, Position requested #Position Available Cursor size #Total
68 |
69 | **Thrower** : CursorGridAdapter
70 |
71 | **Fix** : this crash is when user tries to request #Position of data when its not avaiable in Cursor, for example requesting data for position=100 from a Cursor whose size is less than 100 will result in such crash.
72 |
73 | 9
74 | ===
75 | **Exception** : IndexOutOfBoundsException, Position requested #Position Available list size #Total
76 |
77 | **Thrower** : ListGridAdapter
78 |
79 | **Fix** : this crash is when user tries to request #Position of data when its not avaiable in list, for example requesting data for position=100 from a list whose size is less than 100 will result in such crash.
80 |
--------------------------------------------------------------------------------
/Studio/GridListViewAdapters/gridlistviewadapters/src/main/java/com/birin/gridlistviewadapters/ListGridAdapter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadapters;
17 |
18 | import android.content.Context;
19 |
20 | import java.util.ArrayList;
21 | import java.util.List;
22 |
23 | public abstract class ListGridAdapter extends BaseGridAdapter {
24 |
25 | private List dataList = new ArrayList();
26 |
27 | public ListGridAdapter(Context context, int totalCardsInRow) {
28 | super(context, totalCardsInRow);
29 | }
30 |
31 | @Override
32 | public T getCardData(int absoluteCardPosition) {
33 | validatePositionOrThrow(absoluteCardPosition);
34 | return dataList.get(absoluteCardPosition);
35 | }
36 |
37 | @Override
38 | public void validatePositionOrThrow(int position) {
39 | if (position < 0 || position >= getAbsoluteCardsCount()) {
40 | throw new IndexOutOfBoundsException("Position requested "
41 | + position + " Available list size "
42 | + getAbsoluteCardsCount());
43 | }
44 | }
45 |
46 | @Override
47 | public int getAbsoluteCardsCount() {
48 | if (null != dataList) {
49 | return dataList.size();
50 | }
51 | return 0;
52 | }
53 |
54 | /**
55 | * Adds the new item into existing list.
56 | *
57 | * @param newDataList The new data list to be appended at end of current list
58 | * @see ListGridAdapter#swapDataList(List)
59 | */
60 | public void addItemsInGrid(List newDataList) {
61 | int newDataSize = 0;
62 | if (newDataList != null && newDataList.isEmpty() == false) {
63 | this.dataList.addAll(newDataList);
64 | newDataSize = getAbsoluteCardsCount();
65 | }
66 | updateGridWithNewSize(newDataSize);
67 | }
68 |
69 | /**
70 | * if you want to change the complete data list then consider using this
71 | * function, this will clear current data and replace with new data list
72 | * passing null instead of valid data will simply clear whole data.
73 | *
74 | * @param newDataList the new data list.
75 | */
76 | public void swapDataList(List newDataList) {
77 | this.dataList.clear();
78 | invalidateStructure();
79 | addItemsInGrid(newDataList);
80 | }
81 |
82 | /**
83 | * Deletes item from list for given position
84 | *
85 | * @param position the position at which item needs to be removed.
86 | */
87 | public void deleteItemFromList(int position) {
88 | validatePositionOrThrow(position);
89 | dataList.remove(position);
90 | invalidateStructure();
91 | updateGridWithNewSize(getAbsoluteCardsCount());
92 | }
93 |
94 | /**
95 | * Deletes given object from list.
96 | *
97 | * @param data the object to be deleted.
98 | * @return true, if successful
99 | */
100 | public boolean deleteItemFromList(T data) {
101 | boolean isRemoved = dataList.remove(data);
102 | if (isRemoved == true) {
103 | invalidateStructure();
104 | updateGridWithNewSize(getAbsoluteCardsCount());
105 | }
106 | return isRemoved;
107 | }
108 |
109 | /**
110 | * Clears the complete data list and updates UI.
111 | */
112 | public void clearList() {
113 | swapDataList(null);
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/listgridadapter/base/BaseListGridActivity.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.listgridadapter.base;
17 |
18 | import java.util.ArrayList;
19 |
20 | import android.app.Activity;
21 | import android.app.FragmentManager;
22 | import android.os.Bundle;
23 | import android.view.LayoutInflater;
24 | import android.view.View;
25 | import android.widget.ListView;
26 |
27 | import com.birin.gridlistviewadapters.utils.PositionCalculator;
28 | import com.birin.gridlistviewadaptersdemo.R;
29 | import com.birin.gridlistviewadaptersdemo.common.Constants;
30 | import com.birin.listgridadapter.datasetup.Employee;
31 | import com.birin.listgridadapter.datasetup.RetainedDataFragment;
32 | import com.birin.listgridadapter.datasetup.RetainedDataFragment.DummyDataGeneratorCallback;
33 |
34 | public class BaseListGridActivity extends Activity implements
35 | DummyDataGeneratorCallback {
36 |
37 | protected ListView listView;
38 | protected BaseEmployeeListGridAdapter gridAdapter;
39 | protected RetainedDataFragment dataRetainingFragment;
40 | protected LayoutInflater inflater;
41 |
42 | @Override
43 | protected void onCreate(Bundle savedInstanceState) {
44 | super.onCreate(savedInstanceState);
45 | inflater = LayoutInflater.from(getApplicationContext());
46 | setContentView(R.layout.activity_main);
47 | loadRetainedFragment();
48 | listView = (ListView) findViewById(R.id.list_view);
49 | gridAdapter = getListAdapter();
50 | bindAdapterToList();
51 | addItemsInGrid(dataRetainingFragment.getListData());
52 | }
53 |
54 | protected BaseEmployeeListGridAdapter getListAdapter() {
55 | return new BaseEmployeeListGridAdapter(getApplicationContext(),
56 | getMaxCardsInRow());
57 | }
58 |
59 | protected void bindAdapterToList() {
60 | listView.setAdapter(gridAdapter);
61 | }
62 |
63 | private void loadRetainedFragment() {
64 | FragmentManager fm = getFragmentManager();
65 | dataRetainingFragment = (RetainedDataFragment) fm
66 | .findFragmentByTag(Constants.TAG_RETAINED_FRAGMENT);
67 |
68 | // If the Fragment is non-null, then it is currently being
69 | // retained across a configuration
70 | // changgenerateSomeDummyDataAndAddToList();e.
71 | if (dataRetainingFragment == null) {
72 | dataRetainingFragment = getRetainedDataFragment();
73 | fm.beginTransaction()
74 | .add(dataRetainingFragment, Constants.TAG_RETAINED_FRAGMENT)
75 | .commit();
76 | }
77 | }
78 |
79 | protected RetainedDataFragment getRetainedDataFragment() {
80 | return new RetainedDataFragment();
81 | }
82 |
83 | protected int getMaxCardsInRow() {
84 | return PositionCalculator.getMaxCardsFor(getCurrentOrientation(),
85 | Constants.MAX_CARDS_INFO);
86 | }
87 |
88 | protected int getCurrentOrientation() {
89 | return getResources().getConfiguration().orientation;
90 | }
91 |
92 | @Override
93 | public void onNewDummyDataGenerated(ArrayList newDataList) {
94 | addItemsInGrid(newDataList);
95 | }
96 |
97 | protected void addItemsInGrid(ArrayList newDataList) {
98 | if (null != newDataList && newDataList.isEmpty() == false) {
99 | findViewById(R.id.place_holder_text).setVisibility(View.GONE);
100 | listView.setVisibility(View.VISIBLE);
101 | gridAdapter.addItemsInGrid(newDataList);
102 | }
103 | }
104 |
105 | @Override
106 | public void onDataGeneratorTaskExecuting() {
107 |
108 | }
109 |
110 | @Override
111 | public void onDataGeneratorTaskCancelled() {
112 |
113 | }
114 |
115 | }
116 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdapters/src/com/birin/gridlistviewadapters/utils/ChildViewsClickHandler.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadapters.utils;
17 |
18 | import android.view.View;
19 | import android.view.View.OnClickListener;
20 |
21 | import com.birin.gridlistviewadapters.R;
22 |
23 | /**
24 | * The Class ChildViewsClickHandler, is a class which is responsible for setting
25 | * a single OnClickListener class instance to all views which want to receive
26 | * click events. This is achieved by tagging views with its click event-id & its
27 | * current card position, when click event occurs this card absolute position
28 | * data & event id is retrieved from view tag & passed to client in callback.
29 | *
30 | * Whenever {@link getNewCard()} method is called ,a new CardViewHolder is
31 | * returned & user registers few children of the card for clicking, by calling
32 | * {@link #registerChildViewForClickEvent(View, int)} method, by using Java
33 | * Reflections all the views within ViewHolder are scanned in each getView
34 | * method call by using {@link ViewHolderPositionTagger} we will tag those child
35 | * views with card's absolute position if they were registered for click events
36 | * so that this position can be used to identify when click event happens.
37 | *
38 | */
39 | public final class ChildViewsClickHandler {
40 |
41 | private static int TAG_VIEW_POSITION = R.string.key_view_position;
42 | private static int TAG_CLICK_EVENT_ID = R.string.key_click_event_id;
43 |
44 | private OnClickListener onClickListener;
45 |
46 | public ChildViewsClickHandler(OnClickListener clickEventListener) {
47 | this.onClickListener = clickEventListener;
48 | }
49 |
50 | /**
51 | * Register child view for click event,Its recommended that ONLY children
52 | * view present inside a card should register for these events for parent
53 | * card view click events use {@link onCardClicked} call-back provided by
54 | * GridAdapters.
55 | *
56 | * @param childView
57 | * the child view (present within CardViewHolder) which needs to
58 | * receive click event.
59 | * @param clickEventId
60 | * the integer identifier for this childView's click event, this
61 | * Id should be a >= 0
62 | */
63 | public void registerChildViewForClickEvent(View childView, int clickEventId) {
64 | if (clickEventId < 0) {
65 | throw new IllegalArgumentException(
66 | "Event-Id should be a positive integer");
67 | }
68 | tagEventIdValueToView(childView, clickEventId);
69 | childView.setOnClickListener(onClickListener);
70 | }
71 |
72 | public static int getPositionFromViewTag(View view) {
73 | return getIntValueFromViewTag(view, TAG_VIEW_POSITION);
74 | }
75 |
76 | public static int getEventIdFromViewTag(View view) {
77 | return getIntValueFromViewTag(view, TAG_CLICK_EVENT_ID);
78 | }
79 |
80 | public static void tagPositionValueToView(View view, int value) {
81 | tagIntValueToView(view, TAG_VIEW_POSITION, value);
82 | }
83 |
84 | public static void tagEventIdValueToView(View view, int value) {
85 | tagIntValueToView(view, TAG_CLICK_EVENT_ID, value);
86 | }
87 |
88 | private static int getIntValueFromViewTag(View view, int key) {
89 | Object taggedValue;
90 | if (null != view && null != (taggedValue = view.getTag(key))
91 | && taggedValue instanceof Integer) {
92 | return (Integer) taggedValue;
93 | } else {
94 | return -1;
95 | }
96 | }
97 |
98 | private static void tagIntValueToView(View view, int key, int value) {
99 | if (null != view) {
100 | view.setTag(key, value);
101 | }
102 | }
103 |
104 | }
105 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/cursorgridadapter/base/BaseCursorGridActivity.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.cursorgridadapter.base;
17 |
18 | import android.app.Activity;
19 | import android.app.FragmentManager;
20 | import android.app.LoaderManager.LoaderCallbacks;
21 | import android.content.CursorLoader;
22 | import android.content.Loader;
23 | import android.database.Cursor;
24 | import android.os.Bundle;
25 | import android.view.LayoutInflater;
26 | import android.view.View;
27 | import android.widget.ListView;
28 |
29 | import com.birin.cursorgridadapter.datasetup.CursorRetainingFragment;
30 | import com.birin.cursorgridadapter.datasetup.TestContentProvider;
31 | import com.birin.gridlistviewadapters.utils.PositionCalculator;
32 | import com.birin.gridlistviewadaptersdemo.R;
33 | import com.birin.gridlistviewadaptersdemo.common.Constants;
34 |
35 | public abstract class BaseCursorGridActivity extends Activity implements
36 | LoaderCallbacks {
37 |
38 | protected ListView listView;
39 | protected BaseEmployeeCursorGridAdapter listviewAdapter;
40 | protected CursorRetainingFragment dataRetainingFragment;
41 | protected LayoutInflater inflater;
42 |
43 | @Override
44 | protected void onCreate(Bundle savedInstanceState) {
45 | super.onCreate(savedInstanceState);
46 | setContentView(R.layout.activity_main);
47 | inflater = LayoutInflater.from(getApplicationContext());
48 | loadRetainedFragment();
49 | listView = (ListView) findViewById(R.id.list_view);
50 | listviewAdapter = getListViewAdapter();
51 | bindAdapterToList();
52 | getLoaderManager().initLoader(0, null, this);
53 | }
54 |
55 | protected void bindAdapterToList() {
56 | listView.setAdapter(listviewAdapter);
57 | }
58 |
59 | private void loadRetainedFragment() {
60 | FragmentManager fm = getFragmentManager();
61 | dataRetainingFragment = (CursorRetainingFragment) fm
62 | .findFragmentByTag(Constants.TAG_RETAINED_FRAGMENT);
63 |
64 | // If the Fragment is non-null, then it is currently being
65 | // retained across a configuration
66 | // changgenerateSomeDummyDataAndAddToList();e.
67 | if (dataRetainingFragment == null) {
68 | dataRetainingFragment = getCursorRetainingFragment();
69 | if (dataRetainingFragment != null) {
70 | fm.beginTransaction()
71 | .add(dataRetainingFragment,
72 | Constants.TAG_RETAINED_FRAGMENT).commit();
73 | }
74 | }
75 | }
76 |
77 | protected int getMaxCardsInRow() {
78 | return PositionCalculator.getMaxCardsFor(getCurrentOrientation(),
79 | Constants.MAX_CARDS_INFO);
80 | }
81 |
82 | protected int getCurrentOrientation() {
83 | return getResources().getConfiguration().orientation;
84 | }
85 |
86 | @Override
87 | public Loader onCreateLoader(int arg0, Bundle arg1) {
88 | return new CursorLoader(getApplicationContext(),
89 | TestContentProvider.CONTENT_URI_EMPLOYEE, null, null, null,
90 | null);
91 | }
92 |
93 | @Override
94 | public void onLoadFinished(Loader arg0, Cursor data) {
95 | if (null != data && data.getCount() > 0) {
96 | findViewById(R.id.place_holder_text).setVisibility(View.GONE);
97 | listView.setVisibility(View.VISIBLE);
98 | listviewAdapter.swapCursor(data);
99 | }
100 | }
101 |
102 | @Override
103 | public void onLoaderReset(Loader arg0) {
104 | listviewAdapter.swapCursor(null);
105 | }
106 |
107 | protected BaseEmployeeCursorGridAdapter getListViewAdapter() {
108 | return new BaseEmployeeCursorGridAdapter(getApplicationContext(),
109 | getMaxCardsInRow(), null);
110 | }
111 |
112 | protected CursorRetainingFragment getCursorRetainingFragment() {
113 | return new CursorRetainingFragment();
114 | }
115 |
116 | }
117 |
--------------------------------------------------------------------------------
/Studio/GridListViewAdapters/gridlistviewadapters/src/main/java/com/birin/gridlistviewadapters/utils/ChildViewsClickHandler.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
4 | * not use this file except in compliance with the License. You may obtain a
5 | * copy of the License at
6 | * http://www.apache.org/licenses/LICENSE-2.0
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations
11 | * under the License.
12 | */
13 | package com.birin.gridlistviewadapters.utils;
14 |
15 | import android.view.View;
16 | import android.view.View.OnClickListener;
17 |
18 | import com.birin.gridlistviewadapters.R;
19 |
20 |
21 | /**
22 | * The Class ChildViewsClickHandler, is a class which is responsible for setting
23 | * a single OnClickListener class instance to all views which want to receive
24 | * click events. This is achieved by tagging views with its click event-id and its
25 | * current card position, when click event occurs this card absolute position
26 | * data and event id is retrieved from view tag and passed to client in callback.
27 | * Whenever getNewMethod method is called ,a new CardViewHolder is
28 | * returned and user registers few children of the card for clicking, by calling
29 | * {@link #registerChildViewForClickEvent(View, int)} method, by using Java
30 | * Reflections all the views within ViewHolder are scanned in each getView
31 | * method call by using {@link ViewHolderPositionTagger} we will tag those child
32 | * views with card's absolute position if they were registered for click events
33 | * so that this position can be used to identify when click event happens.
34 | */
35 | public final class ChildViewsClickHandler {
36 |
37 | private static int TAG_VIEW_POSITION = R.string.key_view_position;
38 | private static int TAG_CLICK_EVENT_ID = R.string.key_click_event_id;
39 |
40 | private OnClickListener onClickListener;
41 |
42 | public ChildViewsClickHandler(OnClickListener clickEventListener) {
43 | this.onClickListener = clickEventListener;
44 | }
45 |
46 | /**
47 | * Register child view for click event,Its recommended that ONLY children
48 | * view present inside a card should register for these events for parent
49 | * card view click events use onCardClicked call-back provided by
50 | * GridAdapters.
51 | *
52 | * @param childView the child view (present within CardViewHolder) which needs to
53 | * receive click event.
54 | * @param clickEventId the integer identifier for this childView's click event, this
55 | * Id should be a greater than equal to 0
56 | */
57 | public void registerChildViewForClickEvent(View childView, int clickEventId) {
58 | if (clickEventId < 0) {
59 | throw new IllegalArgumentException(
60 | "Event-Id should be a positive integer");
61 | }
62 | tagEventIdValueToView(childView, clickEventId);
63 | childView.setOnClickListener(onClickListener);
64 | }
65 |
66 | public static int getPositionFromViewTag(View view) {
67 | return getIntValueFromViewTag(view, TAG_VIEW_POSITION);
68 | }
69 |
70 | public static int getEventIdFromViewTag(View view) {
71 | return getIntValueFromViewTag(view, TAG_CLICK_EVENT_ID);
72 | }
73 |
74 | public static void tagPositionValueToView(View view, int value) {
75 | tagIntValueToView(view, TAG_VIEW_POSITION, value);
76 | }
77 |
78 | public static void tagEventIdValueToView(View view, int value) {
79 | tagIntValueToView(view, TAG_CLICK_EVENT_ID, value);
80 | }
81 |
82 | private static int getIntValueFromViewTag(View view, int key) {
83 | Object taggedValue;
84 | if (null != view && null != (taggedValue = view.getTag(key))
85 | && taggedValue instanceof Integer) {
86 | return (Integer) taggedValue;
87 | } else {
88 | return -1;
89 | }
90 | }
91 |
92 | private static void tagIntValueToView(View view, int key, int value) {
93 | if (null != view) {
94 | view.setTag(key, value);
95 | }
96 | }
97 |
98 | }
99 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/gridlistviewadaptersdemo/CursorDataDemos.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadaptersdemo;
17 |
18 | import android.app.ProgressDialog;
19 | import android.os.AsyncTask;
20 | import android.os.Bundle;
21 | import android.view.View;
22 | import android.view.View.OnClickListener;
23 |
24 | import com.birin.cursorgridadapter.datasetup.TestContentProvider;
25 | import com.birin.cursorgridadapter.demo1.FixedCursorItems;
26 | import com.birin.cursorgridadapter.demo2.CardClickHandlingFixedCursorItems;
27 | import com.birin.cursorgridadapter.demo3.ChildAndCardClickHandlingFixedCursorItems;
28 | import com.birin.cursorgridadapter.demo4.FixedCursorItemsRotationSupport;
29 | import com.birin.cursorgridadapter.demo5.UnlimitedCursorItemsRotationSupportAutoLoadMore;
30 | import com.birin.cursorgridadapter.demo6.UnlimitedCursorItemsRotationClickToLoadMore;
31 | import com.birin.gridlistviewadaptersdemo.common.Constants;
32 |
33 | public class CursorDataDemos extends BaseDemoMenuList {
34 |
35 | public static final Demo>[] AVAILABLE_DEMOS = {
36 | // From using Cursor Loader mechanism
37 | new Demo("Demo 1 : Fixed Items from Cursor",
38 | FixedCursorItems.class),
39 | new Demo(
40 | "Demo 2 : Fixed Items + Handling card click events",
41 | CardClickHandlingFixedCursorItems.class),
42 | new Demo(
43 | "Demo 3 : Fixed Items + Handling card clicks + Handling children view clicks present in card",
44 | ChildAndCardClickHandlingFixedCursorItems.class),
45 | new Demo(
46 | "Demo 4 : Fixed Items + Rotation Support",
47 | FixedCursorItemsRotationSupport.class),
48 | new Demo(
49 | "Demo 5 : Unlimited Items with auto load more + Rotation Support",
50 | UnlimitedCursorItemsRotationSupportAutoLoadMore.class),
51 | new Demo(
52 | "Demo 6 : Unlimited Items with click to load more + Rotation Support",
53 | UnlimitedCursorItemsRotationClickToLoadMore.class) };
54 |
55 | @Override
56 | protected Demo>[] getDemos() {
57 | return AVAILABLE_DEMOS;
58 | }
59 |
60 | @Override
61 | protected void onCreate(Bundle savedInstanceState) {
62 | super.onCreate(savedInstanceState);
63 | findViewById(R.id.clear_db).setOnClickListener(new OnClickListener() {
64 |
65 | @Override
66 | public void onClick(View v) {
67 | new EmployeeDataCleanerTask().execute();
68 | }
69 | });
70 |
71 | }
72 |
73 | @Override
74 | protected int getLayoutId() {
75 | return R.layout.cursor_demo_menu_screen_layout;
76 | }
77 |
78 | class EmployeeDataCleanerTask extends AsyncTask {
79 |
80 | ProgressDialog cleanerProgress;
81 |
82 | @Override
83 | protected void onPreExecute() {
84 | super.onPreExecute();
85 | cleanerProgress = new ProgressDialog(CursorDataDemos.this);
86 | cleanerProgress.setCancelable(false);
87 | cleanerProgress
88 | .setMessage(getString(R.string.cleaning_employee_table_dialog_msg));
89 | cleanerProgress.show();
90 | }
91 |
92 | @Override
93 | protected Void doInBackground(Void... params) {
94 | try {
95 | Thread.sleep(Constants.DUMMY_DELAY_IN_MILLIS);
96 | } catch (InterruptedException e) {
97 | e.printStackTrace();
98 | }
99 | getContentResolver().delete(
100 | TestContentProvider.CONTENT_URI_EMPLOYEE, null, null);
101 | return null;
102 | }
103 |
104 | @Override
105 | protected void onPostExecute(Void result) {
106 | if (cleanerProgress != null && cleanerProgress.isShowing()) {
107 | cleanerProgress.dismiss();
108 | }
109 | }
110 |
111 | }
112 |
113 | }
114 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/listgridadapter/base/BaseEmployeeListGridAdapter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.listgridadapter.base;
17 |
18 | import android.annotation.SuppressLint;
19 | import android.content.Context;
20 | import android.view.View;
21 | import android.widget.ImageView;
22 | import android.widget.RelativeLayout;
23 | import android.widget.TextView;
24 |
25 | import com.birin.gridlistviewadapters.Card;
26 | import com.birin.gridlistviewadapters.ListGridAdapter;
27 | import com.birin.gridlistviewadapters.dataholders.CardDataHolder;
28 | import com.birin.gridlistviewadapters.utils.ChildViewsClickHandler;
29 | import com.birin.gridlistviewadaptersdemo.R;
30 | import com.birin.gridlistviewadaptersdemo.common.CharacterDrawable;
31 | import com.birin.gridlistviewadaptersdemo.common.CharacterDrawable.CharacterDrawableInfo;
32 | import com.birin.gridlistviewadaptersdemo.common.EmployeeCardViewHolder;
33 | import com.birin.listgridadapter.datasetup.Employee;
34 |
35 | public class BaseEmployeeListGridAdapter extends
36 | ListGridAdapter {
37 |
38 | public BaseEmployeeListGridAdapter(Context context, int totalItemsInRow) {
39 | super(context, totalItemsInRow);
40 | }
41 |
42 | @SuppressLint("InflateParams")
43 | @Override
44 | protected Card getNewCard(int cardwidth) {
45 | View cardView = getLayoutInflater().inflate(R.layout.card_layout, null);
46 | EmployeeCardViewHolder cardViewHolder = initViewHolderUsing(cardView,
47 | cardwidth);
48 | return new Card(cardView, cardViewHolder);
49 | }
50 |
51 | @Override
52 | protected void setCardView(CardDataHolder cardDataHolder,
53 | EmployeeCardViewHolder cardViewHolder) {
54 | setCardViews(cardDataHolder, cardViewHolder);
55 | }
56 |
57 | @Override
58 | public void onCardClicked(Employee cardData) {
59 | // Not handling
60 | }
61 |
62 | @Override
63 | protected void registerChildrenViewClickEvents(
64 | EmployeeCardViewHolder cardViewHolder,
65 | ChildViewsClickHandler childViewsClickHandler) {
66 | // No child registered.
67 | }
68 |
69 | @Override
70 | public void onChildViewClicked(View clickedChildView, Employee cardData,
71 | int eventId) {
72 | // No child registered.
73 | }
74 |
75 | // Helper Methods.
76 |
77 | private void setCardViews(CardDataHolder cardDataHolder,
78 | EmployeeCardViewHolder cardViewHolder) {
79 | Employee employee = cardDataHolder.getData();
80 | cardViewHolder.employeeName.setText(employee.getEmployeeName());
81 | CharacterDrawableInfo info = new CharacterDrawableInfo();
82 | info.color = employee.getIconCharColor();
83 | info.newChar = employee.getIconChar();
84 | CharacterDrawable.updateTaggedDrawableStateFromImageView(
85 | cardViewHolder.employeeImage, info);
86 | }
87 |
88 | protected EmployeeCardViewHolder initViewHolderUsing(View cardView,
89 | int cardwidth) {
90 | EmployeeCardViewHolder cardViewHolder = new EmployeeCardViewHolder();
91 | initNameView(cardViewHolder, cardView);
92 | initImage(cardViewHolder, cardView);
93 | initImageContainer(cardViewHolder, cardView, cardwidth);
94 | return cardViewHolder;
95 | }
96 |
97 | private void initNameView(EmployeeCardViewHolder cardViewHolder,
98 | View cardView) {
99 | cardViewHolder.employeeName = (TextView) cardView
100 | .findViewById(R.id.name);
101 | }
102 |
103 | private void initImage(EmployeeCardViewHolder cardViewHolder, View cardView) {
104 | cardViewHolder.employeeImage = (ImageView) cardView
105 | .findViewById(R.id.sample_image);
106 | CharacterDrawable
107 | .tagNewDrawableToImageView(cardViewHolder.employeeImage);
108 | }
109 |
110 | private void initImageContainer(EmployeeCardViewHolder cardViewHolder,
111 | View cardView, int cardwidth) {
112 | cardViewHolder.employeeImageContainer = (RelativeLayout) cardView
113 | .findViewById(R.id.image_container);
114 | cardViewHolder.employeeImageContainer.getLayoutParams().height = cardwidth;
115 | }
116 |
117 | }
118 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
21 |
22 |
25 |
26 |
31 |
35 |
36 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
50 |
51 |
54 |
55 |
56 |
57 |
60 |
61 |
64 |
65 |
68 |
69 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
86 |
87 |
90 |
91 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/cursorgridadapter/base/BaseEmployeeCursorGridAdapter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.cursorgridadapter.base;
17 |
18 | import android.annotation.SuppressLint;
19 | import android.content.Context;
20 | import android.database.Cursor;
21 | import android.view.View;
22 | import android.widget.ImageView;
23 | import android.widget.RelativeLayout;
24 | import android.widget.TextView;
25 |
26 | import com.birin.cursorgridadapter.datasetup.TestContentProviderSqlHelper;
27 | import com.birin.gridlistviewadapters.Card;
28 | import com.birin.gridlistviewadapters.CursorGridAdapter;
29 | import com.birin.gridlistviewadapters.dataholders.CardDataHolder;
30 | import com.birin.gridlistviewadapters.utils.ChildViewsClickHandler;
31 | import com.birin.gridlistviewadaptersdemo.R;
32 | import com.birin.gridlistviewadaptersdemo.common.CharacterDrawable;
33 | import com.birin.gridlistviewadaptersdemo.common.CharacterDrawable.CharacterDrawableInfo;
34 | import com.birin.gridlistviewadaptersdemo.common.EmployeeCardViewHolder;
35 |
36 | public class BaseEmployeeCursorGridAdapter extends
37 | CursorGridAdapter {
38 |
39 | public BaseEmployeeCursorGridAdapter(Context context, int maxCardsInaRow,
40 | Cursor c) {
41 | super(context, maxCardsInaRow, c);
42 | }
43 |
44 | @SuppressLint("InflateParams")
45 | @Override
46 | protected Card getNewCard(int cardwidth) {
47 | View cardView = getLayoutInflater().inflate(R.layout.card_layout, null);
48 | EmployeeCardViewHolder cardViewHolder = initViewHolderUsing(cardView,
49 | cardwidth);
50 | return new Card(cardView, cardViewHolder);
51 | }
52 |
53 | @Override
54 | protected void setCardView(CardDataHolder cardDataHolder,
55 | EmployeeCardViewHolder cardViewHolder) {
56 | setCardViews(cardDataHolder, cardViewHolder);
57 | }
58 |
59 | @Override
60 | public void onCardClicked(Cursor cardData) {
61 | // Not handling
62 | }
63 |
64 | @Override
65 | protected void registerChildrenViewClickEvents(
66 | EmployeeCardViewHolder cardViewHolder,
67 | ChildViewsClickHandler childViewsClickHandler) {
68 | // No child registered.
69 | }
70 |
71 | @Override
72 | public void onChildViewClicked(View clickedChildView, Cursor cardData,
73 | int eventId) {
74 | // No child registered.
75 | }
76 |
77 | // Helper Methods.
78 |
79 | private void setCardViews(CardDataHolder cardDataHolder,
80 | EmployeeCardViewHolder cardViewHolder) {
81 | Cursor data = cardDataHolder.getData();
82 | cardViewHolder.employeeName
83 | .setText(data.getString(data
84 | .getColumnIndexOrThrow(TestContentProviderSqlHelper.EMPLOYEE_NAME)));
85 | CharacterDrawableInfo info = new CharacterDrawableInfo();
86 | info.color = data
87 | .getInt(data
88 | .getColumnIndexOrThrow(TestContentProviderSqlHelper.EMPLOYEE_COLUMN_CHAR_COLOR));
89 | info.newChar = data
90 | .getString(
91 | data.getColumnIndexOrThrow(TestContentProviderSqlHelper.EMPLOYEE_COLUMN_CHAR))
92 | .charAt(0);
93 | CharacterDrawable.updateTaggedDrawableStateFromImageView(
94 | cardViewHolder.employeeImage, info);
95 | }
96 |
97 | private EmployeeCardViewHolder initViewHolderUsing(View cardView,
98 | int cardwidth) {
99 | EmployeeCardViewHolder cardViewHolder = new EmployeeCardViewHolder();
100 | initNameView(cardViewHolder, cardView);
101 | initImage(cardViewHolder, cardView);
102 | initImageContainer(cardViewHolder, cardView, cardwidth);
103 | return cardViewHolder;
104 | }
105 |
106 | private void initNameView(EmployeeCardViewHolder cardViewHolder,
107 | View cardView) {
108 | cardViewHolder.employeeName = (TextView) cardView
109 | .findViewById(R.id.name);
110 | }
111 |
112 | private void initImage(EmployeeCardViewHolder cardViewHolder, View cardView) {
113 | cardViewHolder.employeeImage = (ImageView) cardView
114 | .findViewById(R.id.sample_image);
115 | CharacterDrawable
116 | .tagNewDrawableToImageView(cardViewHolder.employeeImage);
117 | }
118 |
119 | private void initImageContainer(EmployeeCardViewHolder cardViewHolder,
120 | View cardView, int cardwidth) {
121 | cardViewHolder.employeeImageContainer = (RelativeLayout) cardView
122 | .findViewById(R.id.image_container);
123 | cardViewHolder.employeeImageContainer.getLayoutParams().height = cardwidth;
124 | }
125 |
126 | }
127 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | GridListViewAdapters
2 | ====================
3 | [](https://android-arsenal.com/details/1/1035)
4 |
5 | This libarary enables you to implement GridView like card layout within ListView with added capabilites like Paginations, Addition of Header-Footer Views, also simplifies implemention for both List & Cursor data.
6 |
7 | This library is designed on [ViewHolder] (http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder) design pattern, it provides an easier way of linking linear data to grid alligned Views. The GridListViewAdapters will do most of tedious work for you & all you have to do is create your small modular classes & link them together.
8 |
9 |
10 |
11 |
12 |
13 | **Looking for ListView with multiple row-types support, check [EasyListViewAdapters](https://github.com/birajpatel/EasyListViewAdapters).**
14 |
15 | Features
16 | ========
17 |
18 | * Enables user to add **Headers & Footers** with your Grid, as its a ListView afterall.
19 | * Supports **flexible number of cards** in both screen orientation.
20 | * Provides readymade PositionCalculator class which enables user to **maintain correct position after orientation** change(In case number of cards in both orienations are different).
21 | * Library takes care of recycling all views, that **ensures performance** & helps your list view scroll smoothly.
22 | * **Auto converts** user's linear data to grid-struture & renders it.
23 | * **No data browsing**, Library takes care of browsing data through Grid-structure when View is being drawn or event occurs so that Users does not have to look for their data to take actions.
24 | * Card views can be created by using **XML or Java** (doesn't restrict to XML-Only Approach).
25 | * Load More callbacks can be registered to implement **paginatation support** to your list.
26 | * **Handles empty cards** as well during pagination & auto replaces empty cards when more data is added.
27 | * **Handling children viewclicks**, you can also register for Children(present inside your Cards) view click events. All these Views are registered with single OnClickListner so that this mechanism is very **memory efficient** when click event occurs users you gets clickedChildView, cardData,int eventId as callback params.
28 |
29 | Adding to your project using gradle
30 | ============
31 | ```groovy
32 | repositories {
33 | maven {
34 | url "https://dl.bintray.com/birajpatel/gridlistviewadapters/"
35 | }
36 | }
37 | ```
38 | ```groovy
39 | compile 'com.birin:gridlistviewadapters:1.0.0'
40 | ```
41 |
42 |
43 | Compatibility
44 | =========
45 | * **Library** : API 4+(DONUT)
46 | * **LibrarySample** : API 11+(HONEYCOMB)
47 |
48 | Supported Data-types
49 | ====================
50 | * **ListGridAdapter** : works with java.util.List of data.
51 | * **CursorGridAdapter** : works with android.database.Cursor
52 |
53 | **Note** : **CursorGridAdapter** doesn't provide any mechanism to requery attached Cursor (Like traditional CursorAdapter), This behaviour is intentionally omitted as it has been deprecated from Android Sources as well.Instead use [CursorLoader Mechanism](https://developer.android.com/training/load-data-background/setup-loader.html).
54 |
55 |
56 | Using ListGridAdapter
57 | =====
58 |
59 | Check [Quick-Usage-Guide](/Extras/Documentations/ListGridAdapterQuickUsageGuide.md)
60 |
61 | Using CursorGridAdapter
62 | =====
63 |
64 | Check [Quick-Usage-Guide](/Extras/Documentations/CursorGridAdapterQuickUsageGuide.md)
65 |
66 |
67 | How to understand Sample-App
68 | ====================
69 |
70 |
71 |
72 | * **1** For basic step by step explaination read [SimplestListGridAdapterUsageDemoActivity.java](https://github.com/birajpatel/GridListViewAdapters/blob/master/Eclipse/GridListViewAdaptersDemo/src/com/birin/listgridadapter/demo1/SimplestListGridAdapterUsageDemoActivity.java)
73 | * **2** Read other ListGridAdapter's Demo.
74 | * **3** Read other CursorGridAdapter's Demo.
75 |
76 |
77 | Progaurd
78 | ========
79 | No need to add any extra config.
80 |
81 |
82 | Debugging
83 | =========
84 |
85 | See the **[Debugging Exceptions](/Extras/Documentations/debugging_exceptions.md)** to know about all Exceptions thrown by library & how to fix them.
86 |
87 | License
88 | =======
89 | Copyright 2014-present Biraj Patel
90 |
91 | Licensed under the Apache License, Version 2.0 (the "License");
92 | you may not use this file except in compliance with the License.
93 | You may obtain a copy of the License at
94 |
95 | http://www.apache.org/licenses/LICENSE-2.0
96 |
97 | Unless required by applicable law or agreed to in writing, software
98 | distributed under the License is distributed on an "AS IS" BASIS,
99 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
100 | See the License for the specific language governing permissions and
101 | limitations under the License.
102 |
103 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdapters/src/com/birin/gridlistviewadapters/utils/PositionCalculator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadapters.utils;
17 |
18 | import android.content.res.Configuration;
19 |
20 | /**
21 | *
22 | * Often we want to support different number of cards in Portrait & Landscape
23 | * screen mode.For example 3 cards in Row for Portrait mode & 4 cards in
24 | * Landscape mode, The challenging part is since number of card is different the
25 | * first visible card's row will also vary. For Example for Portrait
26 | * mode(3-card-row) Card#25 is present in 9th Row, but same card#25 is present
27 | * in 7th row in Landscape mode (4-card-row).
28 | *
29 | *
30 | *
31 | * This class enables you to calculate the new row position for a card(which was
32 | * visible at first in old orientation.)
33 | *
34 | *
35 | * @see library usage demos for usage of this class.
36 | *
37 | */
38 | public class PositionCalculator {
39 |
40 | private final MaxCardsInfo maxCardsInfo;
41 |
42 | public PositionCalculator(MaxCardsInfo info) {
43 | this.maxCardsInfo = info;
44 | }
45 |
46 | /**
47 | * Calculate correct row position after orientation change.
48 | *
49 | * @param oldRowPosition
50 | * row position The visible row position in old orientation
51 | * @param oldOrientation
52 | * orientation value for the old orientation
53 | * {@link Configuration#ORIENTATION_LANDSCAPE} or
54 | * {@link Configuration#ORIENTATION_PORTRAIT}
55 | * @param newOrientation
56 | * orientation value for the new orientation
57 | * {@link Configuration#ORIENTATION_PORTRAIT} or
58 | * {@link Configuration#ORIENTATION_LANDSCAPE}
59 | * @return the new row position to be selected after orientation change.
60 | * @see JavaDocs for this class & library usage samples.
61 | */
62 | public int calculatePositionInNewOrientation(
63 | final int VISIBLE_POSITION_IN_OLD_ORIENTATION,
64 | final int OLD_ORIENTATION, final int NEW_ORIENTATION) {
65 | int positionInNewOrientation = VISIBLE_POSITION_IN_OLD_ORIENTATION;
66 | if (OLD_ORIENTATION != NEW_ORIENTATION) {
67 | validateOrientation(OLD_ORIENTATION);
68 | validateOrientation(NEW_ORIENTATION);
69 | int maxCardsInOldOrientation = getMaxCardsFor(OLD_ORIENTATION);
70 | int maxCardsInNewOrientation = getMaxCardsFor(NEW_ORIENTATION);
71 | int firstVisibleCardIndexInOldOrientation = (VISIBLE_POSITION_IN_OLD_ORIENTATION * maxCardsInOldOrientation);
72 | firstVisibleCardIndexInOldOrientation += 1; // 1 based index
73 | positionInNewOrientation = calculateBucket(
74 | firstVisibleCardIndexInOldOrientation,
75 | maxCardsInNewOrientation);
76 |
77 | }
78 | return positionInNewOrientation;
79 | }
80 |
81 | private int calculateBucket(int firstVisibleCardIndexInOldOrientation,
82 | int maxCardsInNewOrientation) {
83 | int quotient = (firstVisibleCardIndexInOldOrientation / maxCardsInNewOrientation);
84 | int remainder = (firstVisibleCardIndexInOldOrientation % maxCardsInNewOrientation);
85 | quotient -= 1; // 0 based rows
86 | if (remainder == 0) {
87 | // First visible card is already present in quotient row.
88 | } else {
89 | // First visible card is present in next row of quotient row.
90 | quotient += 1;
91 | }
92 | return quotient;
93 | }
94 |
95 | /**
96 | * Determine the max supported cards in current orientation.
97 | *
98 | * @param orientation
99 | * . the current orientation
100 | * @param maxCardsInfo
101 | * the max cards info which holds max supported cards info in
102 | * both orientation.
103 | * @return the max cards for given orientation mode.
104 | * @see MaxCardsInfo
105 | */
106 | public static int getMaxCardsFor(final int ORIENTATION,
107 | MaxCardsInfo maxCardsInfo) {
108 | int maxCards = 0;
109 | if (ORIENTATION == Configuration.ORIENTATION_PORTRAIT) {
110 | maxCards = maxCardsInfo.getPotraitMaxCards();
111 | } else if (ORIENTATION == Configuration.ORIENTATION_LANDSCAPE) {
112 | maxCards = maxCardsInfo.getLandscapeMaxCards();
113 | }
114 | return maxCards;
115 | }
116 |
117 | private int getMaxCardsFor(final int ORIENTATION) {
118 | return getMaxCardsFor(ORIENTATION, maxCardsInfo);
119 | }
120 |
121 | private void validateOrientation(final int ORIENTATION) {
122 | boolean isValid = (ORIENTATION == Configuration.ORIENTATION_LANDSCAPE || ORIENTATION == Configuration.ORIENTATION_PORTRAIT);
123 | if (isValid == false) {
124 | throw new UnsupportedOperationException(
125 | "Unsupported Orientation: "
126 | + ORIENTATION
127 | + ", Valid ones are Configuration.ORIENTATION_LANDSCAPE, Configuration.ORIENTATION_PORTRAIT");
128 | }
129 | }
130 |
131 | }
132 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/listgridadapter/datasetup/RetainedDataFragment.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.listgridadapter.datasetup;
17 |
18 | import java.util.ArrayList;
19 |
20 | import android.app.Activity;
21 | import android.app.Fragment;
22 | import android.os.AsyncTask;
23 | import android.os.Bundle;
24 |
25 | /**
26 | * Helper fragment class which will be used to hold data across configuration
27 | * changes of activity life-cycle as mentioned in below link
28 | */
29 | // http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html
30 | public class RetainedDataFragment extends Fragment {
31 |
32 | private DummyDataGeneratorCallback mCallbacks;
33 | private DummyTask mTask;
34 | private ArrayList listData = new ArrayList();
35 | private boolean isTaskRunning;
36 | public static final String KEY_MAX_ITEMS = "KEY_MAX_ITEMS";
37 |
38 | /**
39 | * Hold a reference to the parent Activity so we can report the task's
40 | * current progress and results. The Android framework will pass us a
41 | * reference to the newly created Activity after each configuration change.
42 | */
43 | @Override
44 | public void onAttach(Activity activity) {
45 | super.onAttach(activity);
46 | mCallbacks = (DummyDataGeneratorCallback) activity;
47 | }
48 |
49 | /**
50 | * This method will only be called once when the retained Fragment is first
51 | * created.
52 | */
53 | @Override
54 | public void onCreate(Bundle savedInstanceState) {
55 | super.onCreate(savedInstanceState);
56 |
57 | // Retain this fragment across configuration changes.
58 | setRetainInstance(true);
59 | runAddDataTask();
60 | }
61 |
62 | /**
63 | * Set the callback to null so we don't accidentally leak the Activity
64 | * instance.
65 | */
66 | @Override
67 | public void onDetach() {
68 | super.onDetach();
69 | mCallbacks = null;
70 | }
71 |
72 | public ArrayList getListData() {
73 | return listData;
74 | }
75 |
76 | public boolean isTaskRunning() {
77 | return isTaskRunning;
78 | }
79 |
80 | private int getMaxAllowedItems() {
81 | int maxAllowed = Integer.MAX_VALUE;
82 | Bundle extras = getArguments();
83 | if (null != extras && extras.containsKey(KEY_MAX_ITEMS)) {
84 | maxAllowed = extras.getInt(KEY_MAX_ITEMS);
85 | }
86 | return maxAllowed;
87 | }
88 |
89 | public void runAddDataTask() {
90 | if (isTaskRunning() == false && canLoadMoreData()) {
91 | if (null != mTask) {
92 | mTask.cancel(true);
93 | mTask = null;
94 | }
95 | // Create and execute the background task.
96 | mTask = new DummyTask();
97 | mTask.execute();
98 | }
99 | }
100 |
101 | public boolean canLoadMoreData() {
102 | return listData.size() < getMaxAllowedItems();
103 | }
104 |
105 | /**
106 | * A dummy task that performs some (dumb) background work and proxies
107 | * progress updates and results back to the Activity.
108 | *
109 | * Note that we need to check if the callbacks are null in each method in
110 | * case they are invoked after the Activity's and Fragment's onDestroy()
111 | * method have been called.
112 | */
113 | private class DummyTask extends
114 | AsyncTask> {
115 |
116 | @Override
117 | protected void onPreExecute() {
118 | isTaskRunning = true;
119 | if (mCallbacks != null) {
120 | mCallbacks.onDataGeneratorTaskExecuting();
121 | }
122 | }
123 |
124 | /**
125 | * Note that we do NOT call the callback object's methods directly from
126 | * the background thread, as this could result in a race condition.
127 | */
128 | @Override
129 | protected ArrayList doInBackground(Void... ignore) {
130 | try {
131 | Thread.sleep(1500);
132 | } catch (InterruptedException e) {
133 | e.printStackTrace();
134 | }
135 | return generateNewDummyDataAndAddToList();
136 | }
137 |
138 | @Override
139 | protected void onCancelled() {
140 | isTaskRunning = false;
141 | if (mCallbacks != null) {
142 | mCallbacks.onDataGeneratorTaskCancelled();
143 | }
144 | }
145 |
146 | @Override
147 | protected void onPostExecute(ArrayList newDataList) {
148 | isTaskRunning = false;
149 | if (mCallbacks != null) {
150 | mCallbacks.onNewDummyDataGenerated(newDataList);
151 | }
152 | }
153 |
154 | private ArrayList generateNewDummyDataAndAddToList() {
155 | int currentSize = listData.size();
156 | ArrayList newDummyData = new ArrayList();
157 | for (int i = currentSize; i < (currentSize + 20); i++) {
158 | newDummyData.add(new Employee(i));
159 | }
160 | listData.addAll(newDummyData);
161 | return newDummyData;
162 | }
163 |
164 | }
165 |
166 | /**
167 | * Callback interface through which the fragment will report the task's
168 | * progress and results back to the Activity.
169 | */
170 | public interface DummyDataGeneratorCallback {
171 |
172 | public void onDataGeneratorTaskExecuting();
173 |
174 | public void onDataGeneratorTaskCancelled();
175 |
176 | public void onNewDummyDataGenerated(ArrayList newDataList);
177 |
178 | }
179 |
180 | }
181 |
--------------------------------------------------------------------------------
/Studio/GridListViewAdapters/gridlistviewadapters/src/main/java/com/birin/gridlistviewadapters/utils/PositionCalculator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
4 | * not use this file except in compliance with the License. You may obtain a
5 | * copy of the License at
6 | * http://www.apache.org/licenses/LICENSE-2.0
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations
11 | * under the License.
12 | */
13 | package com.birin.gridlistviewadapters.utils;
14 |
15 | import android.content.res.Configuration;
16 |
17 | /**
18 | * Often we want to support different number of cards in Portrait and Landscape
19 | * screen mode.For example 3 cards in Row for Portrait mode and 4 cards in
20 | * Landscape mode, The challenging part is since number of card is different the
21 | * first visible card's row will also vary. For Example for Portrait
22 | * mode(3-card-row) Card#25 is present in 9th Row, but same card#25 is present
23 | * in 7th row in Landscape mode (4-card-row).
24 | * This class enables you to calculate the new row position for a card(which was
25 | * visible at first in old orientation.)
26 | */
27 | public class PositionCalculator {
28 |
29 | private final MaxCardsInfo maxCardsInfo;
30 |
31 | public PositionCalculator(MaxCardsInfo info) {
32 | this.maxCardsInfo = info;
33 | }
34 |
35 | /**
36 | * Calculate correct row position after orientation change.
37 | *
38 | * @param VISIBLE_POSITION_IN_OLD_ORIENTATION row position The visible row position in old orientation
39 | * @param OLD_ORIENTATION orientation value for the old orientation
40 | * {@link Configuration#ORIENTATION_LANDSCAPE} or
41 | * {@link Configuration#ORIENTATION_PORTRAIT}
42 | * @param NEW_ORIENTATION orientation value for the new orientation
43 | * {@link Configuration#ORIENTATION_PORTRAIT} or
44 | * {@link Configuration#ORIENTATION_LANDSCAPE}
45 | * @return the new row position to be selected after orientation change.
46 | */
47 | public int calculatePositionInNewOrientation(
48 | final int VISIBLE_POSITION_IN_OLD_ORIENTATION,
49 | final int OLD_ORIENTATION, final int NEW_ORIENTATION) {
50 | int positionInNewOrientation = VISIBLE_POSITION_IN_OLD_ORIENTATION;
51 | if (OLD_ORIENTATION != NEW_ORIENTATION) {
52 | validateOrientation(OLD_ORIENTATION);
53 | validateOrientation(NEW_ORIENTATION);
54 | int maxCardsInOldOrientation = getMaxCardsFor(OLD_ORIENTATION);
55 | int maxCardsInNewOrientation = getMaxCardsFor(NEW_ORIENTATION);
56 | int firstVisibleCardIndexInOldOrientation = (VISIBLE_POSITION_IN_OLD_ORIENTATION * maxCardsInOldOrientation);
57 | firstVisibleCardIndexInOldOrientation += 1; // 1 based index
58 | positionInNewOrientation = calculateBucket(
59 | firstVisibleCardIndexInOldOrientation,
60 | maxCardsInNewOrientation);
61 |
62 | }
63 | return positionInNewOrientation;
64 | }
65 |
66 | private int calculateBucket(int firstVisibleCardIndexInOldOrientation,
67 | int maxCardsInNewOrientation) {
68 | int quotient = (firstVisibleCardIndexInOldOrientation / maxCardsInNewOrientation);
69 | int remainder = (firstVisibleCardIndexInOldOrientation % maxCardsInNewOrientation);
70 | quotient -= 1; // 0 based rows
71 | if (remainder == 0) {
72 | // First visible card is already present in quotient row.
73 | } else {
74 | // First visible card is present in next row of quotient row.
75 | quotient += 1;
76 | }
77 | return quotient;
78 | }
79 |
80 | /**
81 | * Determine the max supported cards in current orientation.
82 | *
83 | * @param ORIENTATION . the current orientation
84 | * @param maxCardsInfo the max cards info which holds max supported cards info in
85 | * both orientation.
86 | * @return the max cards for given orientation mode.
87 | * @see MaxCardsInfo
88 | */
89 | public static int getMaxCardsFor(final int ORIENTATION,
90 | MaxCardsInfo maxCardsInfo) {
91 | int maxCards = 0;
92 | if (ORIENTATION == Configuration.ORIENTATION_PORTRAIT) {
93 | maxCards = maxCardsInfo.getPotraitMaxCards();
94 | } else if (ORIENTATION == Configuration.ORIENTATION_LANDSCAPE) {
95 | maxCards = maxCardsInfo.getLandscapeMaxCards();
96 | }
97 | return maxCards;
98 | }
99 |
100 | private int getMaxCardsFor(final int ORIENTATION) {
101 | return getMaxCardsFor(ORIENTATION, maxCardsInfo);
102 | }
103 |
104 | private void validateOrientation(final int ORIENTATION) {
105 | boolean isValid = (ORIENTATION == Configuration.ORIENTATION_LANDSCAPE || ORIENTATION == Configuration.ORIENTATION_PORTRAIT);
106 | if (isValid == false) {
107 | throw new UnsupportedOperationException(
108 | "Unsupported Orientation: "
109 | + ORIENTATION
110 | + ", Valid ones are Configuration.ORIENTATION_LANDSCAPE, Configuration.ORIENTATION_PORTRAIT");
111 | }
112 | }
113 |
114 | }
115 |
--------------------------------------------------------------------------------
/Studio/GridListViewAdapters/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdapters/src/com/birin/gridlistviewadapters/utils/GridDataStructure.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.gridlistviewadapters.utils;
17 |
18 | import java.util.List;
19 |
20 | import com.birin.gridlistviewadapters.dataholders.CardDataHolder;
21 | import com.birin.gridlistviewadapters.dataholders.CardPositionInfo;
22 | import com.birin.gridlistviewadapters.dataholders.RowDataHolder;
23 |
24 | /**
25 | * This is a data structure class,here user's linear data will be mapped to Grid
26 | * data with complexity of O(n)
27 | *
28 | *
29 | * Note: This class only bind the position info of cards , actual card data is
30 | * not bound into structure rather retrieved at runtime as per need.
31 | *
32 | *
33 | *
34 | *
35 | * It basically creates List of {@link RowDataHolder} which in turn holds list
36 | * of {@link CardDataHolder}, this list of {@link RowDataHolder} is directly fed
37 | * to GridAdapters for rendering.
38 | *
39 | */
40 |
41 | public class GridDataStructure {
42 |
43 | private int totalCardsInRow = 0;
44 |
45 | public GridDataStructure(int totalCardsInRow) {
46 | this.totalCardsInRow = totalCardsInRow;
47 | if (totalCardsInRow <= 0) {
48 | throw new IllegalArgumentException(
49 | "Invalid number of cards in a row should be greater than 0");
50 | }
51 | }
52 |
53 | /**
54 | * Update grid structure, this is main method that update/create the grid
55 | * data structure from total number data size, it takes list of grid-rows
56 | * which will be modified and cards will be added per row basis depending
57 | * upon {@link #totalCardsInRow}.
58 | *
59 | */
60 | public void updateStructure(List gridStructuredDataList,
61 | int dataListSize) {
62 | if (null == gridStructuredDataList) {
63 | throw new IllegalArgumentException("Data lists shouldn't be empty.");
64 | }
65 | int size = gridStructuredDataList.size();
66 | int absoluteCardposition = 0;
67 | int emptyCardIndex = -1;
68 | boolean isFirstChunk = (size == 0);
69 | if (isFirstChunk == false) {
70 | // this means gridStructuredData already is present in grid list let
71 | // us now check for any empty card in last row, to be able to
72 | // replace with new data.
73 | RowDataHolder lastRow = gridStructuredDataList.get(size - 1);
74 | if (null != lastRow) {
75 | absoluteCardposition = lastRow.getCardPositionInfos()
76 | .get(totalCardsInRow - 1).getAbsolutePositionValue() + 1;
77 | // start scanning from last to first card in row if empty card
78 | // found then assign the value to empty-index variable.
79 | // Scanning is stopped when a non-empty card is found.
80 | for (int i = totalCardsInRow - 1; i >= 0; i--) {
81 | CardPositionInfo positionInfo = lastRow
82 | .getCardPositionInfos().get(i);
83 | if (positionInfo.isEmptyCard() == false) {
84 | break;
85 | }
86 | absoluteCardposition = positionInfo
87 | .getAbsolutePositionValue();
88 | emptyCardIndex = i;
89 | lastRow.getCardPositionInfos().remove(i);
90 | }
91 | if (emptyCardIndex != -1) {
92 | // this means that we have found few empty cards,lets add
93 | // new card at index starting from position of empty
94 | // card in last row.
95 | for (int cardPositionInaRow = emptyCardIndex; cardPositionInaRow < totalCardsInRow; cardPositionInaRow++) {
96 | absoluteCardposition = addNewCard(lastRow,
97 | absoluteCardposition, dataListSize);
98 | }
99 | }
100 | }
101 | }
102 | // now we have checked probability of finding empty blocks in last row &
103 | // have already filled new gridStructuredData in place of them , now
104 | // let's add new row
105 | while (absoluteCardposition < dataListSize) {
106 | absoluteCardposition = addNewRow(gridStructuredDataList,
107 | dataListSize, absoluteCardposition);
108 | }
109 | }
110 |
111 | /**
112 | * Adds the new row from gridStructuredData list and add into grid list.
113 | *
114 | */
115 | private int addNewRow(List gridRowList, int dataListSize,
116 | int absoluteCardposition) {
117 | RowDataHolder newRow = new RowDataHolder();
118 | for (int i = 0; i < totalCardsInRow; i++) {
119 | absoluteCardposition = addNewCard(newRow, absoluteCardposition,
120 | dataListSize);
121 | }
122 | gridRowList.add(newRow);
123 | return absoluteCardposition;
124 | }
125 |
126 | /**
127 | * Adds the new card into given row.
128 | *
129 | */
130 | private int addNewCard(RowDataHolder gridRow, int absoluteCardposition,
131 | int dataListSize) {
132 | boolean isEmptyCard = false;
133 | if (absoluteCardposition >= dataListSize) {
134 | // Create new row and add new cards into it, but if we have
135 | // consumed till last card of the gridStructuredData-list but
136 | // still if row has cards to be filled then let's fill it with
137 | // empty block, which will be cleared out when next chunk of
138 | // gridStructuredData comes.
139 | isEmptyCard = true;
140 | }
141 | int type = -1;
142 | if (isEmptyCard == true) {
143 | type = CardPositionInfo.TYPE_EMPTY;
144 | } else {
145 | type = CardPositionInfo.TYPE_CONTENT;
146 | }
147 | // Lets tag our grid card with absoluteCardposition, which is its
148 | // position in the grid, this index helps us keep mapping between our
149 | // linear-data to grid structure.
150 | CardPositionInfo cardPositionInfo = new CardPositionInfo(type,
151 | absoluteCardposition);
152 | gridRow.getCardPositionInfos().add(cardPositionInfo);
153 | // return absoluteCardposition, is kept as to keep mapping for card of
154 | // grid structure to the data list of input.
155 | absoluteCardposition++;
156 | return absoluteCardposition;
157 | }
158 |
159 | }
160 |
--------------------------------------------------------------------------------
/Eclipse/GridListViewAdaptersDemo/src/com/birin/cursorgridadapter/datasetup/CursorRetainingFragment.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 | * not use this file except in compliance with the License. You may obtain a
6 | * 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, WITHOUT
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 | * License for the specific language governing permissions and limitations
14 | * under the License.
15 | */
16 | package com.birin.cursorgridadapter.datasetup;
17 |
18 | import android.app.Activity;
19 | import android.app.Fragment;
20 | import android.content.ContentResolver;
21 | import android.content.ContentValues;
22 | import android.database.Cursor;
23 | import android.os.AsyncTask;
24 | import android.os.Bundle;
25 |
26 | import com.birin.gridlistviewadaptersdemo.common.Constants;
27 | import com.birin.gridlistviewadaptersdemo.common.RandomInfoGenerator;
28 |
29 | /**
30 | * Helper fragment class which will be used to hold data across configuration
31 | * changes of activity life-cycle as mentioned in below link
32 | */
33 | // http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html
34 | public class CursorRetainingFragment extends Fragment {
35 |
36 | private DummyDataGeneratorCallback mCallbacks;
37 | private DummyTask mTask;
38 | private boolean isTaskRunning;
39 | public static final String KEY_MAX_ITEMS = "KEY_MAX_ITEMS";
40 |
41 | /**
42 | * Hold a reference to the parent Activity so we can report the task's
43 | * current progress and results. The Android framework will pass us a
44 | * reference to the newly created Activity after each configuration change.
45 | */
46 | @Override
47 | public void onAttach(Activity activity) {
48 | super.onAttach(activity);
49 | if (activity instanceof DummyDataGeneratorCallback) {
50 | mCallbacks = (DummyDataGeneratorCallback) activity;
51 | }
52 | }
53 |
54 | /**
55 | * This method will only be called once when the retained Fragment is first
56 | * created.
57 | */
58 | @Override
59 | public void onCreate(Bundle savedInstanceState) {
60 | super.onCreate(savedInstanceState);
61 |
62 | // Retain this fragment across configuration changes.
63 | setRetainInstance(true);
64 | runAddDataTaskToEnsureMinimumData();
65 | }
66 |
67 | /**
68 | * Set the callback to null so we don't accidentally leak the Activity
69 | * instance.
70 | */
71 | @Override
72 | public void onDetach() {
73 | super.onDetach();
74 | mCallbacks = null;
75 | }
76 |
77 | public boolean isTaskRunning() {
78 | return isTaskRunning;
79 | }
80 |
81 | private int getMaxAllowedItems() {
82 | int maxAllowed = Integer.MAX_VALUE;
83 | Bundle extras = getArguments();
84 | if (null != extras && extras.containsKey(KEY_MAX_ITEMS)) {
85 | maxAllowed = extras.getInt(KEY_MAX_ITEMS);
86 | }
87 | return maxAllowed;
88 | }
89 |
90 | private void runAddDataTaskToEnsureMinimumData() {
91 | runAddDataTaskIfNotRunning(true);
92 | }
93 |
94 | public void runAddDataTaskOnLoadMoreRequest() {
95 | runAddDataTaskIfNotRunning(false);
96 | }
97 |
98 | private void runAddDataTaskIfNotRunning(boolean isEnsureRun) {
99 | if (isTaskRunning() == false) {
100 | if (null != mTask) {
101 | mTask.cancel(true);
102 | mTask = null;
103 | }
104 | // Create and execute the background task.
105 | mTask = new DummyTask(isEnsureRun);
106 | mTask.execute();
107 | }
108 | }
109 |
110 | public boolean canLoadMoreData(int currentDataSize) {
111 | return currentDataSize < getMaxAllowedItems();
112 | }
113 |
114 | /**
115 | * A dummy task that performs some (dumb) background work and proxies
116 | * progress updates and results back to the Activity.
117 | *
118 | */
119 | private class DummyTask extends AsyncTask {
120 |
121 | private boolean isEnsureRun;
122 |
123 | public DummyTask(boolean isEnsureRun) {
124 | this.isEnsureRun = isEnsureRun;
125 | }
126 |
127 | @Override
128 | protected void onPreExecute() {
129 | isTaskRunning = true;
130 | if (mCallbacks != null) {
131 | mCallbacks.onDataGeneratorTaskExecuting();
132 | }
133 | }
134 |
135 | @Override
136 | protected Void doInBackground(Void... ignore) {
137 | Activity activity = getActivity();
138 | if (null != activity) {
139 | ContentResolver resolver = activity.getContentResolver();
140 | Cursor existingData = resolver.query(
141 | TestContentProvider.CONTENT_URI_EMPLOYEE, null, null,
142 | null, null);
143 | int currentSize = existingData == null ? 0 : existingData
144 | .getCount();
145 | if (isEnsureRun == false || currentSize <= 0) {
146 | generateNewDummyDataIfCanLoadMoreData(currentSize, resolver);
147 | }
148 | }
149 | return null;
150 | }
151 |
152 | private void generateNewDummyDataIfCanLoadMoreData(int currentSize,
153 | ContentResolver resolver) {
154 | if (canLoadMoreData(currentSize) == true) {
155 | generateNewDummyDataAndAddToDB(currentSize, resolver);
156 | }
157 | }
158 |
159 | @Override
160 | protected void onCancelled() {
161 | isTaskRunning = false;
162 | }
163 |
164 | @Override
165 | protected void onPostExecute(Void result) {
166 | isTaskRunning = false;
167 | }
168 |
169 | }
170 |
171 | private void generateNewDummyDataAndAddToDB(int currentSize,
172 | ContentResolver resolver) {
173 | try {
174 | Thread.sleep(Constants.DUMMY_DELAY_IN_MILLIS);
175 | } catch (InterruptedException e) {
176 | e.printStackTrace();
177 | }
178 | ContentValues valuesToInsert = new ContentValues();
179 | for (int i = currentSize; i < (currentSize + 20); i++) {
180 | valuesToInsert.clear();
181 | updateValuesToInsert(valuesToInsert, i);
182 | resolver.insert(TestContentProvider.CONTENT_URI_EMPLOYEE,
183 | valuesToInsert);
184 | }
185 | }
186 |
187 | private void updateValuesToInsert(ContentValues valuesToInsert, int i) {
188 | valuesToInsert.put(TestContentProviderSqlHelper.EMPLOYEE_NAME, "id:"
189 | + i);
190 | valuesToInsert.put(TestContentProviderSqlHelper.EMPLOYEE_COLUMN_CHAR,
191 | Character.toString(RandomInfoGenerator.getRandomChar()));
192 | valuesToInsert.put(
193 | TestContentProviderSqlHelper.EMPLOYEE_COLUMN_CHAR_COLOR,
194 | RandomInfoGenerator.getRandomColor());
195 |
196 | }
197 |
198 | public interface DummyDataGeneratorCallback {
199 |
200 | public void onDataGeneratorTaskExecuting();
201 | }
202 |
203 | }
204 |
--------------------------------------------------------------------------------
/Studio/GridListViewAdapters/gridlistviewadapters/src/main/java/com/birin/gridlistviewadapters/utils/GridDataStructure.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2014-present Biraj Patel
3 | * Licensed under the Apache License, Version 2.0 (the "License"); you may
4 | * not use this file except in compliance with the License. You may obtain a
5 | * copy of the License at
6 | * http://www.apache.org/licenses/LICENSE-2.0
7 | * Unless required by applicable law or agreed to in writing, software
8 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 | * License for the specific language governing permissions and limitations
11 | * under the License.
12 | */
13 | package com.birin.gridlistviewadapters.utils;
14 |
15 | import com.birin.gridlistviewadapters.dataholders.CardDataHolder;
16 | import com.birin.gridlistviewadapters.dataholders.CardPositionInfo;
17 | import com.birin.gridlistviewadapters.dataholders.RowDataHolder;
18 |
19 | import java.util.List;
20 |
21 | /**
22 | * This is a data structure class,here user's linear data will be mapped to Grid
23 | * data with complexity of O(n)
24 | * Note: This class only bind the position info of cards , actual card data is
25 | * not bound into structure rather retrieved at runtime as per need.
26 | * It basically creates List of {@link RowDataHolder} which in turn holds list
27 | * of {@link CardDataHolder}, this list of {@link RowDataHolder} is directly fed
28 | * to GridAdapters for rendering.
29 | */
30 |
31 | public class GridDataStructure {
32 |
33 | private int totalCardsInRow = 0;
34 |
35 | public GridDataStructure(int totalCardsInRow) {
36 | this.totalCardsInRow = totalCardsInRow;
37 | if (totalCardsInRow <= 0) {
38 | throw new IllegalArgumentException(
39 | "Invalid number of cards in a row should be greater than 0");
40 | }
41 | }
42 |
43 | /**
44 | * Update grid structure, this is main method that update/create the grid
45 | * data structure from total number data size, it takes list of grid-rows
46 | * which will be modified and cards will be added per row basis depending
47 | * upon {@link #totalCardsInRow}.
48 | */
49 | public void updateStructure(List gridStructuredDataList,
50 | int dataListSize) {
51 | if (null == gridStructuredDataList) {
52 | throw new IllegalArgumentException("Data lists shouldn't be empty.");
53 | }
54 | int size = gridStructuredDataList.size();
55 | int absoluteCardposition = 0;
56 | int emptyCardIndex = -1;
57 | boolean isFirstChunk = (size == 0);
58 | if (isFirstChunk == false) {
59 | // this means gridStructuredData already is present in grid list let
60 | // us now check for any empty card in last row, to be able to
61 | // replace with new data.
62 | RowDataHolder lastRow = gridStructuredDataList.get(size - 1);
63 | if (null != lastRow) {
64 | absoluteCardposition = lastRow.getCardPositionInfos()
65 | .get(totalCardsInRow - 1).getAbsolutePositionValue() + 1;
66 | // start scanning from last to first card in row if empty card
67 | // found then assign the value to empty-index variable.
68 | // Scanning is stopped when a non-empty card is found.
69 | for (int i = totalCardsInRow - 1; i >= 0; i--) {
70 | CardPositionInfo positionInfo = lastRow
71 | .getCardPositionInfos().get(i);
72 | if (positionInfo.isEmptyCard() == false) {
73 | break;
74 | }
75 | absoluteCardposition = positionInfo
76 | .getAbsolutePositionValue();
77 | emptyCardIndex = i;
78 | lastRow.getCardPositionInfos().remove(i);
79 | }
80 | if (emptyCardIndex != -1) {
81 | // this means that we have found few empty cards,lets add
82 | // new card at index starting from position of empty
83 | // card in last row.
84 | for (int cardPositionInaRow = emptyCardIndex; cardPositionInaRow < totalCardsInRow; cardPositionInaRow++) {
85 | absoluteCardposition = addNewCard(lastRow,
86 | absoluteCardposition, dataListSize);
87 | }
88 | }
89 | }
90 | }
91 | // now we have checked probability of finding empty blocks in last row &
92 | // have already filled new gridStructuredData in place of them , now
93 | // let's add new row
94 | while (absoluteCardposition < dataListSize) {
95 | absoluteCardposition = addNewRow(gridStructuredDataList,
96 | dataListSize, absoluteCardposition);
97 | }
98 | }
99 |
100 | /**
101 | * Adds the new row from gridStructuredData list and add into grid list.
102 | */
103 | private int addNewRow(List gridRowList, int dataListSize,
104 | int absoluteCardposition) {
105 | RowDataHolder newRow = new RowDataHolder();
106 | for (int i = 0; i < totalCardsInRow; i++) {
107 | absoluteCardposition = addNewCard(newRow, absoluteCardposition,
108 | dataListSize);
109 | }
110 | gridRowList.add(newRow);
111 | return absoluteCardposition;
112 | }
113 |
114 | /**
115 | * Adds the new card into given row.
116 | */
117 | private int addNewCard(RowDataHolder gridRow, int absoluteCardposition,
118 | int dataListSize) {
119 | boolean isEmptyCard = false;
120 | if (absoluteCardposition >= dataListSize) {
121 | // Create new row and add new cards into it, but if we have
122 | // consumed till last card of the gridStructuredData-list but
123 | // still if row has cards to be filled then let's fill it with
124 | // empty block, which will be cleared out when next chunk of
125 | // gridStructuredData comes.
126 | isEmptyCard = true;
127 | }
128 | int type = -1;
129 | if (isEmptyCard == true) {
130 | type = CardPositionInfo.TYPE_EMPTY;
131 | } else {
132 | type = CardPositionInfo.TYPE_CONTENT;
133 | }
134 | // Lets tag our grid card with absoluteCardposition, which is its
135 | // position in the grid, this index helps us keep mapping between our
136 | // linear-data to grid structure.
137 | CardPositionInfo cardPositionInfo = new CardPositionInfo(type,
138 | absoluteCardposition);
139 | gridRow.getCardPositionInfos().add(cardPositionInfo);
140 | // return absoluteCardposition, is kept as to keep mapping for card of
141 | // grid structure to the data list of input.
142 | absoluteCardposition++;
143 | return absoluteCardposition;
144 | }
145 |
146 | }
147 |
--------------------------------------------------------------------------------