我正在尝试在我的Android应用中使用StickyGridHeaders,除非我尝试在标题视图中为可点击的ImageView添加clicklistener,否则效果很好。在我getHeaderView()
的{{1}}中,我正在尝试执行以下操作:
getHeaderView
BaseAdapter
gallery_item.xml
@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;
}
我无法出现祝酒词。我尝试在适配器中实现<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>
也无济于事。任何帮助将不胜感激。
谢谢,
-Zach
答案 0 :(得分:0)
您应该在托管StickyGrid的片段/活动中实现侦听器
我让它像这样工作:
@Override
public void onHeaderClick(AdapterView<?> adapterView, View view, long l) {
view.findViewById(R.id.folder_settings).performClick();
}
对于只有一个可点击元素的标题,没关系。如果标题包含多个可以单击的元素,则此解决方案将不起作用。
答案 1 :(得分:0)
好的,经过几个小时的麻烦麻烦搜索这个答案,我终于明白了。我正在查看示例here,并注意到在页眉布局中,他们没有将布局指定为
//remove the following from your header layout
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
我从标题视图中完全删除了这些属性并且中提琴!有用。之后我感到非常愚蠢,但希望它可以帮助别人。
<强>更新强>
以下是我如何实施它的更多细节。
片段类
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;
}
自定义基本适配器
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;
}
}
查看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>