I am trying to use StickyGridHeaders in my Android app and it is working great except when I try to add a clicklistener to a clickable ImageView in the headerview. In getHeaderView()
in my BaseAdapter
I am trying to do the following:
getHeaderView
@Override
public View getHeaderView(final int pos, View view, ViewGroup viewGroup) {
view = inflater.inflate(R.layout.gallery_item,viewGroup, false);
TextView title = (TextView) view.findViewById(R.id.title);
TextView date = (TextView) view.findViewById(R.id.date);
ImageView settings = (ImageView) view.findViewById(R.id.folder_settings);
settings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext, "The Click Worked.", Toast.LENGTH_SHORT).show();
}
});
GalleryItem galleryItem = galleryItems.get(pos);
icon.setImageResource(setIcon(galleryItem.getMode()));
title.setText(galleryItem.getTitle());
Date da = galleryItem.record.getDate("FILE_DATE");
SimpleDateFormat dateFormat = new SimpleDateFormat("LLLL-dd-yyyy");
String mDate = dateFormat.format(da);
date.setText(mDate);
return view;
}
gallery_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="55dp"
android:background="@color/lightgraymain"
android:id="@+id/linearLayout">
<ImageView
android:layout_width="55dp"
android:layout_height="match_parent"
android:id="@+id/icon"
android:src="@drawable/ic_gallery_mode_tag" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="521 North 7th Street, Lincoln, NE"
android:id="@+id/title"
android:textColor="@color/black"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:padding="5dp"
android:fontFamily="sans-serif-condensed"
android:enabled="true"
android:ellipsize="marquee"
android:textIsSelectable="false"
android:singleLine="true"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="August-25-2014"
android:id="@+id/date"
android:layout_gravity="center_vertical"
android:fontFamily="sans-serif-light"
android:textColor="@color/gray" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/folder_settings"
android:src="@drawable/ic_gallery_options"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:padding="10dp"
android:layout_gravity="center"
/>
</LinearLayout>
I cannot get the toast to appear. I have tried implementing onHeaderClick()
in the adapter as well to no avail. Any help would be greatly appreciated.
Thank you,
-Zach
You should implement the listener in the fragment/activity who hosts the StickyGrid
I made it to work like this:
@Override
public void onHeaderClick(AdapterView<?> adapterView, View view, long l) {
view.findViewById(R.id.folder_settings).performClick();
}
For a header with just one clickable element it's ok. If the header has more than one element which can be clickable this solution won't work.
Ok so after hours and hours of mind numbing searching for this answer, I finally figured it out. I was looking through the example here and noticed that in the header layout, they didn't specify the layout as
//remove the following from your header layout
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
I removed these properties from my header view completely and viola! it works. I feel pretty dumb after that, but hope it helps someone else.
UPDATE
Here is a little more detail in how I have implemented it.
Fragment Class
public static class PlaceholderFragment extends Fragment implements
StickyGridHeadersGridView.OnHeaderClickListener,
StickyGridHeadersGridView.OnItemClickListener,
StickyGridHeadersGridView.OnItemLongClickListener {
@Override
public void onHeaderClick(AdapterView<?> adapterView, final View view, long l) {
Log.i("asd","THE HEADER IS CLICKED");
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.i("asd","ITEM "+i+" IS CLICKED");
}
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, final View view, int i, long l) {
Log.i("asd","ITEM "+i+" IS LONG CLICKED");
return true;
}
Custom Base Adapter
public class MediaAdapter extends BaseAdapter implements StickyGridHeadersBaseAdapter{
private Context mContext;
ArrayList<MediaItem> mediaitems;
ArrayList<GalleryItem> galleryItems;
LayoutInflater inflater;
HashMap<String,ImageView> imageHolder;
public MediaAdapter(Context c,ArrayList<GalleryItem> l) {
mediaitems = new ArrayList<MediaItem>();
galleryRef = new HashMap<String, Integer>();
mContext = c;
galleryItems = l;
imageHolder = new HashMap<String, ImageView>();
inflater = LayoutInflater.from(c);
}
public GalleryItem getSelectedGallery(String id){
return ((Tidy)mContext.getApplicationContext()).startOrGetFileStore().getGalleryItem(id);
}
public int getCount() {
return mediaitems.size();
}
public Object getItem(int position) {
return mediaitems.get(position);
}
public long getItemId(int position) {
return position;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public View getView(int position, View convertView, ViewGroup parent) {
GridView gv =(GridView)parent;
ViewHolder holder;
if (convertView == null) {// if it's not recycled, initialize some attributes
convertView = inflater.inflate(R.layout.media_item, parent, false);
holder = new ViewHolder();
holder.img = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
imageManager.displayImage(holder.img);
return convertView;
}
@Override
public int getCountForHeader(int i) {
return galleryItems.get(i).getMediaLength();
}
@Override
public int getNumHeaders() {
return galleryItems.size();
}
@Override
public View getHeaderView(final int pos, View view, ViewGroup viewGroup) {
HeaderViewHolder holder = new HeaderViewHolder();
if(view == null) {
view = inflater.inflate(R.layout.gallery_item, viewGroup, false);
holder.title = (TextView) view.findViewById(R.id.title);
holder.date = (TextView) view.findViewById(R.id.date);
holder.icon = (ImageView) view.findViewById(R.id.icon);
holder.settings = (ImageView) view.findViewById(R.id.folder_settings);
view.setTag(holder);
}
else{
holder = (HeaderViewHolder) view.getTag();
}
GalleryItem galleryItem = galleryItems.get(pos);
holder.icon.setImageResource(setIcon(galleryItem.getMode()));
holder.title.setText(galleryItem.getTitle());
Date da = galleryItem.record.getDate("FILE_DATE");
SimpleDateFormat dateFormat = new SimpleDateFormat("LLLL-dd-yyyy");
String mDate = dateFormat.format(da);
holder.date.setText(mDate);
holder.gallery = galleryItem.record;
return view;
}
class HeaderViewHolder {
DbxRecord gallery;
RelativeLayout layout;
TextView title;
TextView date;
ImageView icon;
ImageView settings;
}
class ViewHolder {
ImageView img;
}
}
View XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.tonicartos.widget.stickygridheaders.StickyGridHeadersGridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imagegrid"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
android:stackFromBottom="false"
android:numColumns="auto_fit"
android:columnWidth="80dp"
/>
</RelativeLayout>