Skip to content

Instantly share code, notes, and snippets.

View tcw165's full-sized avatar
💭
building AI

TC Wang tcw165

💭
building AI
View GitHub Profile
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.my.whatever"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="com.android.vending.BILLING"/>
<application>
<activity
android:name=".IapDelegateActivity"
android:theme="@style/AppTheme.Invisible">
// Example 1, from an android.app.Activity:
// Use the startActivityForResult(Intent, int) and subscribe
// the result in onActivityResult(int, int, Intent).
startActivityForResult(
new Intent(this, IapDelegateActivity.class)
.putExtra(IapDelegateActivity.PARAMS_SKU, "sku")
.putExtra(IapDelegateActivity.PARAMS_SKU_PRICE, 1.99f));
// Example 2, from a android.app.Service:
// Use the startActivity(Intent) and subscribe the result in
@tcw165
tcw165 / Example_of_ElasticDragLayout.java
Created March 20, 2017 01:31
Example of ElasticDragLayout.java
// Copyright (c) 2017-present boyw165
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
private void cancelChildrenTouchEvent(MotionEvent event) {
// MotionEvent.obtain creates a new MotionEvent`, copying from an existing one.
final MotionEvent canceledEvent = MotionEvent.obtain(event);
// The event was handled by the certain child view. In order to make the touch event
// lifecycle complete, we have to fake a ACTION_CANCEL for the child view.
// Otherwise, it would be chaos.
canceledEvent.setAction(MotionEvent.ACTION_CANCEL);
super.dispatchTouchEvent(canceledEvent);
canceledEvent.recycle();
private void cancelSelfTouchEvent(MotionEvent event) {
final MotionEvent canceledEvent = MotionEvent.obtain(event);
canceledEvent.setAction(MotionEvent.ACTION_CANCEL);
onTouchEvent(canceledEvent);
canceledEvent.recycle();
}
public interface NestedScrollingChild {
/**
* Begin a nestable scroll operation along the given axes.
*
* @param axes Flags consisting of a combination of {@link ViewCompat#SCROLL_AXIS_HORIZONTAL}
* and/or {@link ViewCompat#SCROLL_AXIS_VERTICAL}.
* @return true if a cooperative parent was found and nested scrolling has been enabled for
* the current gesture.
*/
public boolean startNestedScroll(int axes);
public class CoordinatorLayout extends ViewGroup
implements NestedScrollingParent {
@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
int dxUnconsumed, int dyUnconsumed) {
final int childCount = getChildCount();
boolean accepted = false;
for (int i = 0; i < childCount; i++) {
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder,
final int position,
List<Object> payloads) {
final ImageView imageView = (ImageView) viewHolder.itemView;
final IPhoto photo = getPhotoAt(position);
Glide.with(getContext())
.load(photo.thumbnailPath())
// Setting the placeholder will fix the "ghost images" problem.
public abstract class CursorRecyclerViewAdapter<VH extends RecyclerView.ViewHolder>
extends RecyclerView.Adapter<VH> {
final private WeakReference<Context> mContext;
final private WeakReference<LayoutInflater> mInflater;
private RecyclerView mRecyclerView;
private Cursor mCursor;
// State.
/**
* Change the underlying cursor to a new cursor. If there is an existing
* cursor it will be closed.
*/
public void setData(Cursor cursor) {
Cursor old = swapCursor(cursor);
if (old != null && !old.isClosed()) {
old.close();
}
}