Skip to content

Commit

Permalink
Debug issue #22
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarr21 committed Feb 14, 2015
1 parent 37b2558 commit 8d7eff4
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sample/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
android:label="@string/gridview_example" />
<activity
android:name=".RecyclerViewActivity" />
<activity
android:name=".SimpleListWithHeadersAndFootersActivity"
android:label="Issue" />
</application>

</manifest>
10 changes: 10 additions & 0 deletions sample/res/layout/footer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="@dimen/footer_height"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:minHeight="@dimen/footer_height"
android:textColor="@android:color/black"
android:background="@android:color/holo_red_light"/>
1 change: 1 addition & 0 deletions sample/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<resources>
<dimen name="item_height">48dp</dimen>
<dimen name="footer_height">96dp</dimen>
<dimen name="grid_item_height">150dp</dimen>
<dimen name="grid_item_taller_height">243dp</dimen>
<dimen name="grid_item_spacing">16dp</dimen>
Expand Down
2 changes: 2 additions & 0 deletions sample/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

<string name="standard">Standard</string>
<string name="listview_example">ListView Example</string>
<string name="listview_footer_example">ListView Example</string>
<string name="listview_dob_example">ListView Example</string>
<string name="gridview_example">GridView Example</string>
<string name="recyclerview_list_example">RecyclerView List Example</string>
<string name="recyclerview_grid_example">RecyclerView Grid Example</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
Expand All @@ -25,6 +26,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
mList = (JazzyListView) findViewById(android.R.id.list);
mList.addFooterView(LayoutInflater.from(this).inflate(R.layout.footer, null, false));
mList.setAdapter(new ListAdapter(this, R.layout.item));

if (savedInstanceState != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class MainActivity extends ListActivity {

private final List<ActivityInfo> activityInfos = Arrays.asList(
new ActivityInfo(ListViewActivity.class, R.string.listview_example, R.layout.item, false),
new ActivityInfo(SimpleListWithHeadersAndFootersActivity.class, R.string.listview_footer_example, R.layout.item, false),
new ActivityInfo(SimpleListWithHeadersAndFootersActivity.class, R.string.listview_dob_example, R.layout.item, false),
new ActivityInfo(GridViewActivity.class, R.string.gridview_example, R.layout.grid_item, false),
new ActivityInfo(RecyclerViewActivity.class, R.string.recyclerview_list_example, R.layout.item, false),
new ActivityInfo(RecyclerViewActivity.class, R.string.recyclerview_grid_example, R.layout.grid_item, false),
Expand Down
122 changes: 122 additions & 0 deletions sample/src/com/twotoasters/jazzylistview/sample/PagerListAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.twotoasters.jazzylistview.sample;

import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class PagerListAdapter extends BaseAdapter implements AbsListView.OnScrollListener {

public static final int COUNT_OF_PER_PAGE = 15;
private final LayoutInflater inflater;
private final Resources res;
private final int itemLayout;
private final List<String> pagerResults = new ArrayList<String>();
private LoadingState state = LoadingState.PENDING;
private final ListView listView;

private int currentPage = 1;
private final int totalPages;

private final Handler handler = new Handler(Looper.getMainLooper());

public PagerListAdapter(Context context, int itemLayout, ListView listView) {
inflater = LayoutInflater.from(context);
res = context.getResources();
this.itemLayout = itemLayout;
final int length = ListModel.getModel().length;
totalPages = length % COUNT_OF_PER_PAGE == 0 ? length / COUNT_OF_PER_PAGE : (length / COUNT_OF_PER_PAGE) + 1;
this.listView = listView;
this.listView.setOnScrollListener(this);
}

@Override
public int getCount() {
return pagerResults.size();
}

@Override
public String getItem(int position) {
return pagerResults.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;

if (convertView == null) {
convertView = inflater.inflate(itemLayout, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

int colorResId = position % 2 == 0 ? R.color.even : R.color.odd;
holder.text.setBackgroundColor(res.getColor(colorResId));
holder.text.setText(ListModel.getModelItem(position));

return convertView;
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (state != LoadingState.PENDING) {
return;
}

if (firstVisibleItem + visibleItemCount >= totalItemCount && currentPage < totalPages) {
loadPage(currentPage++);
}
}

private void loadPage(final int page) {
state = LoadingState.LOADING;
handler.postDelayed(new Runnable() {
@Override
public void run() {
final int length = ListModel.getModel().length;
for (int i = 0; i < COUNT_OF_PER_PAGE; i++) {
int index = page * COUNT_OF_PER_PAGE + i;
if (index >= length) {
break;
}
pagerResults.add(ListModel.getModelItem(index));
}
notifyDataSetChanged();
state = page == totalPages ? LoadingState.END : LoadingState.PENDING;
}
}, 2000);
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}

static class ViewHolder {
final TextView text;

ViewHolder(View view) {
text = (TextView) view.findViewById(R.id.text);
}
}

static enum LoadingState {
PENDING, LOADING, FAILURE, END
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.twotoasters.jazzylistview.sample;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import com.twotoasters.jazzylistview.JazzyHelper;
import com.twotoasters.jazzylistview.JazzyListView;

import java.util.*;

public class SimpleListWithHeadersAndFootersActivity extends Activity {

private static final String KEY_TRANSITION_EFFECT = "transition_effect";

private JazzyListView mList;
private HashMap<String, Integer> mEffectMap;
private int mCurrentTransitionEffect = JazzyHelper.HELIX;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);

// add header to listview
mList = (JazzyListView) findViewById(android.R.id.list);
TextView headerView = (TextView) getLayoutInflater().inflate(R.layout.item, null);
headerView.setBackgroundColor(0xFFEA4C89);
headerView.setText("ListView Header");
mList.addHeaderView(headerView);

// add footer to listview
mList = (JazzyListView) findViewById(android.R.id.list);
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.item, null);
footerView.setBackgroundColor(0xFFEA4C89);
footerView.setText("ListView Footer");
mList.addFooterView(footerView);

mList.setAdapter(new PagerListAdapter(this, R.layout.item, mList));
// mList.setShouldOnlyAnimateNewItems(true);

if (savedInstanceState != null) {
mCurrentTransitionEffect = savedInstanceState.getInt(KEY_TRANSITION_EFFECT, JazzyHelper.HELIX);
setupJazziness(mCurrentTransitionEffect);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
mEffectMap = new HashMap<String, Integer>();
int i = 0;
String[] effects = this.getResources().getStringArray(R.array.jazzy_effects);
for (String effect : effects) {
mEffectMap.put(effect, i++);
}

List<String> effectList = new ArrayList<String>(Arrays.asList(effects));
Collections.sort(effectList);
effectList.remove(getString(R.string.standard));
effectList.add(0, getString(R.string.standard));
for (String effect : effectList) {
menu.add(effect);
}

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
String strEffect = item.getTitle().toString();
Toast.makeText(this, strEffect, Toast.LENGTH_SHORT).show();
setupJazziness(mEffectMap.get(strEffect));
return true;
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_TRANSITION_EFFECT, mCurrentTransitionEffect);
}

private void setupJazziness(int effect) {
mCurrentTransitionEffect = effect;
mList.setTransitionEffect(mCurrentTransitionEffect);
}
}

0 comments on commit 8d7eff4

Please sign in to comment.