Skip to content

Commit

Permalink
Merge pull request #4747 from hieupham007/timob-13686-v1
Browse files Browse the repository at this point in the history
Timob 13686 v1: Listview headerView, footerView, canScroll, separatorColor
  • Loading branch information
pingwang2011 committed Oct 4, 2013
2 parents eb9db1b + d0403da commit 1bff05e
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public class ListSectionProxy extends ViewProxy{
private String headerTitle;
private String footerTitle;

private TiViewProxy headerView;
private TiViewProxy footerView;

private WeakReference<TiListView> listView;
public TiDefaultListViewTemplate builtInTemplate;

Expand Down Expand Up @@ -96,6 +99,18 @@ public void handleCreationDict(KrollDict dict) {
if (dict.containsKey(TiC.PROPERTY_FOOTER_TITLE)) {
footerTitle = TiConvert.toString(dict, TiC.PROPERTY_FOOTER_TITLE);
}
if (dict.containsKey(TiC.PROPERTY_HEADER_VIEW)) {
Object obj = dict.get(TiC.PROPERTY_HEADER_VIEW);
if (obj instanceof TiViewProxy) {
headerView = (TiViewProxy) obj;
}
}
if (dict.containsKey(TiC.PROPERTY_FOOTER_VIEW)) {
Object obj = dict.get(TiC.PROPERTY_FOOTER_VIEW);
if (obj instanceof TiViewProxy) {
footerView = (TiViewProxy) obj;
}
}
if (dict.containsKey(TiC.PROPERTY_ITEMS)) {
handleSetItems(dict.get(TiC.PROPERTY_ITEMS));
}
Expand All @@ -105,11 +120,37 @@ public void setAdapter(TiBaseAdapter a) {
adapter = a;
}

@Kroll.method @Kroll.setProperty
public void setHeaderView(TiViewProxy headerView) {
this.headerView = headerView;
if (adapter != null) {
notifyDataChange();
}
}

@Kroll.method @Kroll.getProperty
public TiViewProxy getHeaderView() {
return headerView;
}

@Kroll.method @Kroll.setProperty
public void setFooterView(TiViewProxy footerView) {
this.footerView = footerView;
if (adapter != null) {
notifyDataChange();
}
}

@Kroll.method @Kroll.getProperty
public TiViewProxy getFooterView() {
return footerView;
}

@Kroll.method @Kroll.setProperty
public void setHeaderTitle(String headerTitle) {
this.headerTitle = headerTitle;
if (adapter != null) {
adapter.notifyDataSetChanged();
notifyDataChange();
}
}

Expand All @@ -122,7 +163,7 @@ public String getHeaderTitle() {
public void setFooterTitle(String headerTitle) {
this.footerTitle = headerTitle;
if (adapter != null) {
adapter.notifyDataSetChanged();
notifyDataChange();
}
}

Expand All @@ -131,15 +172,34 @@ public String getFooterTitle() {
return footerTitle;
}

public void notifyDataChange() {
getMainHandler().post(new Runnable() {
@Override
public void run()
{
adapter.notifyDataSetChanged();
}
});
}

public String getHeaderOrFooterTitle(int index) {
if (isHeaderView(index)) {
if (isHeaderTitle(index)) {
return headerTitle;
} else if (isFooterView(index)) {
} else if (isFooterTitle(index)) {
return footerTitle;
}
return "";
}

public View getHeaderOrFooterView(int index) {
if (isHeaderView(index)) {
return getListView().layoutHeaderOrFooterView(headerView);
} else if (isFooterView(index)) {
return getListView().layoutHeaderOrFooterView(footerView);
}
return null;
}

@Override
public boolean handleMessage(Message msg)
{
Expand Down Expand Up @@ -597,7 +657,7 @@ public void appendExtraEventData(TiUIView view, int itemIndex, int sectionIndex,
}

//itemIndex = realItemIndex + header (if exists). We want the real item index.
if (headerTitle != null) {
if (headerTitle != null || headerView != null) {
itemIndex -= 1;
}

Expand Down Expand Up @@ -684,7 +744,7 @@ public void populateViews(KrollDict data, TiBaseListViewItem cellContent, TiList
}

public TiListViewTemplate getTemplateByIndex(int index) {
if (headerTitle != null) {
if (headerTitle != null || headerView != null) {
index -= 1;
}
return listItemData.get(index).getTemplate();
Expand All @@ -693,25 +753,34 @@ public TiListViewTemplate getTemplateByIndex(int index) {
public int getContentCount() {
return itemCount;
}

/**
* @return number of entries within section
*/
public int getItemCount() {
int totalCount = itemCount;
if (headerTitle != null) {
if (headerTitle != null || headerView != null) {
totalCount += 1;
}
if (footerTitle != null) {
if (footerTitle != null || footerView != null) {
totalCount +=1;
}
return totalCount;
}

public boolean isHeaderView(int pos) {
return (headerTitle != null && pos == 0) ;
return (headerView != null && pos == 0);
}

public boolean isFooterView(int pos) {
return (footerView != null && pos == getItemCount() - 1);
}

public boolean isHeaderTitle(int pos) {
return (headerTitle != null && pos == 0) ;
}

public boolean isFooterTitle(int pos) {
return (footerTitle != null && pos == getItemCount() - 1);
}

Expand Down Expand Up @@ -741,7 +810,7 @@ public void setTemplateType() {
}

public KrollDict getListItemData(int position) {
if (headerTitle != null) {
if (headerTitle != null || headerView != null) {
position -= 1;
}
if (position >= 0 && position < listItemData.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
TiC.PROPERTY_FOOTER_TITLE,
TiC.PROPERTY_DEFAULT_ITEM_TEMPLATE,
TiC.PROPERTY_SHOW_VERTICAL_SCROLL_INDICATOR,
TiC.PROPERTY_SECTIONS
TiC.PROPERTY_SECTIONS,
TiC.PROPERTY_SEPARATOR_COLOR
})
public class ListViewProxy extends TiViewProxy {

Expand All @@ -54,6 +55,10 @@ public class ListViewProxy extends TiViewProxy {
private ArrayList<ListSectionProxy> preloadSections;
private HashMap<String, Integer> preloadMarker;

public ListViewProxy() {
super();
}

public TiUIView createView(Activity activity) {
return new TiListView(this, activity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiColorHelper;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiRHelper;
import org.appcelerator.titanium.util.TiRHelper.ResourceNotFoundException;
import org.appcelerator.titanium.view.TiCompositeLayout;
import org.appcelerator.titanium.view.TiCompositeLayout.LayoutArrangement;
import org.appcelerator.titanium.view.TiCompositeLayout.LayoutParams;
import org.appcelerator.titanium.view.TiUIView;

import ti.modules.titanium.ui.UIModule;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
Expand Down Expand Up @@ -69,9 +74,11 @@ public class TiListView extends TiUIView {
public static List<String> MUST_SET_PROPERTIES = Arrays.asList(TiC.PROPERTY_VALUE);

public static final String MIN_ROW_HEIGHT = "30dp";
public static final int HEADER_FOOTER_ITEM_TYPE = 0;
public static final int BUILT_IN_TEMPLATE_ITEM_TYPE = 1;

public static final int HEADER_FOOTER_WRAP_ID = 12345;
public static final int HEADER_FOOTER_VIEW_TYPE = 0;
public static final int HEADER_FOOTER_TITLE_TYPE = 1;
public static final int BUILT_IN_TEMPLATE_ITEM_TYPE = 2;

class ListViewWrapper extends FrameLayout {

public ListViewWrapper(Context context) {
Expand Down Expand Up @@ -157,19 +164,22 @@ public long getItemId(int position) {
return position;
}

//One type for header/footer, One type for built-in template, and one type per custom template.
//One type for header/footer title, one for header/footer view, one for built-in template, and one type per custom template.
@Override
public int getViewTypeCount() {
return 2 + templatesByBinding.size();
return 3 + templatesByBinding.size();

}
@Override
public int getItemViewType(int position) {
Pair<ListSectionProxy, Pair<Integer, Integer>> info = getSectionInfoByEntryIndex(position);
ListSectionProxy section = info.first;
int sectionItemIndex = info.second.second;
if (section.isHeaderView(sectionItemIndex) || section.isFooterView(sectionItemIndex))
return HEADER_FOOTER_ITEM_TYPE;
if (section.isHeaderTitle(sectionItemIndex) || section.isFooterTitle(sectionItemIndex))
return HEADER_FOOTER_TITLE_TYPE;
if (section.isHeaderView(sectionItemIndex) || section.isFooterView(sectionItemIndex)) {
return HEADER_FOOTER_VIEW_TYPE;
}
return section.getTemplateByIndex(sectionItemIndex).getType();
}

Expand All @@ -188,8 +198,11 @@ public View getView(int position, View convertView, ViewGroup parent) {

View content = convertView;

//Handling section header/footer titles
//Handles header/footer views and titles.
if (section.isHeaderView(sectionItemIndex) || section.isFooterView(sectionItemIndex)) {
return section.getHeaderOrFooterView(sectionItemIndex);
} else if (section.isHeaderTitle(sectionItemIndex) || section.isFooterTitle(sectionItemIndex)) {
//No content to reuse, so we create a new view
if (content == null) {
content = inflater.inflate(headerFooterId, null);
}
Expand Down Expand Up @@ -318,6 +331,11 @@ public void processProperties(KrollDict d) {
}
}

if (d.containsKey(TiC.PROPERTY_SEPARATOR_COLOR)) {
String color = TiConvert.toString(d, TiC.PROPERTY_SEPARATOR_COLOR);
setSeparatorColor(color);
}

if (d.containsKey(TiC.PROPERTY_SHOW_VERTICAL_SCROLL_INDICATOR)) {
listView.setVerticalScrollBarEnabled(TiConvert.toBoolean(d, TiC.PROPERTY_SHOW_VERTICAL_SCROLL_INDICATOR, true));
}
Expand All @@ -344,17 +362,23 @@ public void processProperties(KrollDict d) {

listProxy.clearPreloadSections();

if (d.containsKey(TiC.PROPERTY_HEADER_TITLE)) {
if (d.containsKey(TiC.PROPERTY_HEADER_VIEW)) {
Object viewObj = d.get(TiC.PROPERTY_HEADER_VIEW);
setHeaderOrFooterView(viewObj, true);
} else if (d.containsKey(TiC.PROPERTY_HEADER_TITLE)) {
headerView = inflater.inflate(headerFooterId, null);
setHeaderTitle(TiConvert.toString(d, TiC.PROPERTY_HEADER_TITLE));
}

if (d.containsKey(TiC.PROPERTY_FOOTER_TITLE)) {
if (d.containsKey(TiC.PROPERTY_FOOTER_VIEW)) {
Object viewObj = d.get(TiC.PROPERTY_FOOTER_VIEW);
setHeaderOrFooterView(viewObj, false);
} else if (d.containsKey(TiC.PROPERTY_FOOTER_TITLE)) {
footerView = inflater.inflate(headerFooterId, null);
setFooterTitle(TiConvert.toString(d, TiC.PROPERTY_FOOTER_TITLE));
}

//Check to see if headerTitle and footerTitle are specified. If not, we hide the views
//Check to see if headerView and footerView are specified. If not, we hide the views
if (headerView == null) {
headerView = inflater.inflate(headerFooterId, null);
headerView.findViewById(titleId).setVisibility(View.GONE);
Expand All @@ -375,6 +399,20 @@ public void processProperties(KrollDict d) {

}

private void setHeaderOrFooterView (Object viewObj, boolean isHeader) {
if (viewObj instanceof TiViewProxy) {
TiViewProxy viewProxy = (TiViewProxy)viewObj;
View view = layoutHeaderOrFooterView(viewProxy);
if (view != null) {
if (isHeader) {
headerView = view;
} else {
footerView = view;
}
}
}
}

public void propertyChanged(String key, Object oldValue, Object newValue, KrollProxy proxy) {

if (key.equals(TiC.PROPERTY_HEADER_TITLE)) {
Expand All @@ -391,11 +429,21 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
} else if (key.equals(TiC.PROPERTY_DEFAULT_ITEM_TEMPLATE) && newValue != null) {
defaultTemplateBinding = TiConvert.toString(newValue);
refreshItems();
} else if (key.equals(TiC.PROPERTY_SEPARATOR_COLOR)) {
String color = TiConvert.toString(newValue);
setSeparatorColor(color);
} else {
super.propertyChanged(key, oldValue, newValue, proxy);
}
}

private void setSeparatorColor(String color) {
int sepColor = TiColorHelper.parseColor(color);
int dividerHeight = listView.getDividerHeight();
listView.setDivider(new ColorDrawable(sepColor));
listView.setDividerHeight(dividerHeight);
}

private void refreshItems() {
for (int i = 0; i < sections.size(); i++) {
ListSectionProxy section = sections.get(i);
Expand All @@ -415,6 +463,36 @@ protected void processTemplates(KrollDict templates) {
template.setRootParent(proxy);
}
}

public View layoutHeaderOrFooterView (TiViewProxy viewProxy) {
TiUIView tiView = viewProxy.peekView();
if (tiView != null) {
TiViewProxy parentProxy = viewProxy.getParent();
//Remove parent view if possible
if (parentProxy != null) {
TiUIView parentView = parentProxy.peekView();
if (parentView != null) {
parentView.remove(tiView);
}
}
} else {
tiView = viewProxy.forceCreateView();
}
View outerView = tiView.getOuterView();
ViewGroup parentView = (ViewGroup) outerView.getParent();
if (parentView != null && parentView.getId() == HEADER_FOOTER_WRAP_ID) {
return parentView;
} else {
//add a wrapper so layout params such as height, width takes in effect.
TiCompositeLayout wrapper = new TiCompositeLayout(viewProxy.getActivity(), LayoutArrangement.DEFAULT, null);
AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
wrapper.setLayoutParams(params);
outerView = tiView.getOuterView();
wrapper.addView(outerView, tiView.getLayoutParams());
wrapper.setId(HEADER_FOOTER_WRAP_ID);
return wrapper;
}
}

protected void processSections(Object[] sections) {

Expand Down

0 comments on commit 1bff05e

Please sign in to comment.