Skip to content
This repository was archived by the owner on Mar 24, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.android.tools.build:gradle:2.2.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Dec 12 10:27:39 CST 2014
#Thu Dec 29 14:37:07 CST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,9 @@ public void invalidateHeaders() {
mHeaderProvider.invalidate();
mHeaderRects.clear();
}

public Rect getHeaderRect(int position) {
return mHeaderRects.get(position);
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.timehop.stickyheadersrecyclerview;

import android.graphics.Rect;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.ViewGroup;

public class StickyRecyclerHeadersTouchListener implements RecyclerView.OnItemTouchListener {
private final GestureDetector mTapDetector;
Expand Down Expand Up @@ -68,6 +71,12 @@ public boolean onSingleTapUp(MotionEvent e) {
if (position != -1) {
View headerView = mDecor.getHeaderView(mRecyclerView, position);
long headerId = getAdapter().getHeaderId(position);
Rect rect = mDecor.getHeaderRect(position);
int hOffset = rect.left - headerView.getLeft();
int vOffset = rect.top - headerView.getTop();
if (isChildClicked(headerView, hOffset, vOffset, e)) {
return false;
}
mOnHeaderClickListener.onHeaderClick(headerView, position, headerId);
mRecyclerView.playSoundEffect(SoundEffectConstants.CLICK);
headerView.onTouchEvent(e);
Expand All @@ -76,6 +85,31 @@ public boolean onSingleTapUp(MotionEvent e) {
return false;
}

private boolean isChildClicked(View headerView, int hOffset, int vOffset, MotionEvent e) {
if (headerView instanceof ViewGroup == false) {
return false;
}
ViewGroup vg = (ViewGroup) headerView;
int childCount = vg.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = vg.getChildAt(i);
if (isViewTouched(child, hOffset, vOffset, e) && child.isClickable()) {
child.performClick();
return true;
}
}
return false;
}

private boolean isViewTouched(View view, int hOffset, int vOffset, MotionEvent e) {
Rect rect = new Rect();
rect.left = view.getLeft() + hOffset;
rect.right = view.getRight() + hOffset;
rect.top = view.getTop() + vOffset;
rect.bottom = view.getBottom() + vOffset;
return rect.contains((int) e.getX(), (int) e.getY());
}

@Override
public boolean onDoubleTap(MotionEvent e) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,22 @@ public long getHeaderId(int position) {

@Override
public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
View view = LayoutInflater.from(parent.getContext())
final View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.view_header, parent, false);
view.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "button"+v.getTag(), Toast.LENGTH_SHORT).show();
}
});
return new RecyclerView.ViewHolder(view) {
};
}

@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
TextView textView = (TextView) holder.itemView;
holder.itemView.findViewById(R.id.btn).setTag(position);
TextView textView = (TextView) holder.itemView.findViewById(R.id.tv);
textView.setText(String.valueOf(getItem(position).charAt(0)));
holder.itemView.setBackgroundColor(getRandomColor());
}
Expand Down
34 changes: 24 additions & 10 deletions sample/src/main/res/layout/view_header.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>

<TextView
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="#001F3F"
android:textSize="28sp"
android:textStyle="bold"
android:textColor="@android:color/white"
tools:text="Animals starting with A"
tools:context=".MainActivity"/>
android:background="#001F3F">

<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp"
android:textColor="@android:color/white"
android:textSize="28sp"
android:textStyle="bold"
tools:context=".MainActivity"
tools:text="Animals starting with A"/>

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="button"/>

</RelativeLayout>