实现Android StickyGridHeaders时标题中的按钮 [英] Button in header when implementing StickyGridHeaders Android

查看:52
本文介绍了实现Android StickyGridHeaders时标题中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android应用中使用 StickyGridHeaders ,它的运行效果非常好,除非我尝试将clicklistener添加到headerview中的可点击ImageView中.在我的BaseAdapter中的getHeaderView()中,我尝试执行以下操作:

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>

我无法吐司.我已经尝试在适配器中实现onHeaderClick(),但无济于事.任何帮助将不胜感激. 谢谢, -扎克

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

推荐答案

好吧,经过数小时的思考之后,我终于找到了答案.我正在查看示例此处,发现在标题布局中,他们没有将布局指定为

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.

更新

在这里,我将详细介绍如何实现它.

Here is a little more detail in how I have implemented it.

片段类

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>

这篇关于实现Android StickyGridHeaders时标题中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆