Skip to content

Instantly share code, notes, and snippets.

View yrom's full-sized avatar
😹

Yrom Wang yrom

😹
View GitHub Profile
@yrom
yrom / CustomUARequest.java
Last active December 26, 2015 09:49
Volley: Custom UA Request
/*
* Copyright (C) 2013 YROM.NET
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@yrom
yrom / UsingCookiesRequest.java
Created October 24, 2013 06:28
Volley: 带cookies的 request
/*
* Copyright (C) 2013 YROM.NET
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@yrom
yrom / ForceCacheing.java
Created October 24, 2013 06:49
Volley: 强制缓存
...
protected Response<?> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
... // parseing json
return Response.success(obj, cache(response, 60));
} catch (Exception e) {
@yrom
yrom / gist:11167376
Created April 22, 2014 06:30
A hack to add the translation to the action bar (excerpt from https://github.com/umano/AndroidSlidingUpPanel/tree/master/demo)
public void setActionBarTranslation(float y) {
// Figure out the actionbar height
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
// A hack to add the translation to the action bar
ViewGroup content = ((ViewGroup) findViewById(android.R.id.content).getParent());
int children = content.getChildCount();
@yrom
yrom / gist:9918a8d5244a2f8ced11
Created May 21, 2014 09:19
Android: Check for Available Features at Runtime
// Check if android.hardware.telephony feature is available.
if (getPackageManager().hasSystemFeature("android.hardware.telephony")) {
Log.d("Mobile Test", "Running on phone");
// Check if android.hardware.touchscreen feature is available.
} else if (getPackageManager().hasSystemFeature("android.hardware.touchscreen")) {
Log.d("Tablet Test", "Running on devices that don't support telphony but have a touchscreen.");
} else {
Log.d("TV Test", "Running on a TV!");
}
@yrom
yrom / selector-snippet.java
Created October 19, 2014 15:12
code snippet for java.nio.channels.Selector. http://tutorials.jenkov.com/java-nio/selectors.html
// the remote server
SocketAddress remote = new InetSocketAddress(Inet4Address.getByName(hostName), port);
// open a selector
Selector selector = Selector.open();
// open a channel
SocketChannel channel = SocketChannel.open();
// non-blocking
channel.configureBlocking(false);
// pending connect
channel.connect(remote);
@yrom
yrom / ArrayAdapter.java
Last active March 25, 2016 05:52
wrap android.support.v7.widget.RecyclerView.Adapter like android.widget.ListAdapter
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import android.content.Context;
import android.support.v7.widget.RecyclerView.ViewHolder;
public abstract class ArrayAdapter<T, VH extends ViewHolder> extends BaseAdapter<T, VH> {
private List<T> mItems;
@yrom
yrom / SpacesItemDecoration.java
Last active September 2, 2018 16:57
SpacesItemDecoration for RecyclerView.
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int space;
private int spanCount;
private int lastItemInFirstLane = -1;
public SpacesItemDecoration(int space) {
@yrom
yrom / PagerActivity.java
Created May 30, 2015 08:12
sample of RecycledViewPool
public class PagerActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paper);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new PageAdapter(getSupportFragmentManager()));
}
@yrom
yrom / MyTestRunner.java
Created December 17, 2016 09:34
change repository's url of RobolectricTestRunner
import org.apache.maven.artifact.ant.DependenciesTask;
import org.apache.maven.artifact.ant.RemoteRepository;
import org.apache.maven.model.Dependency;
import org.apache.tools.ant.Project;
import org.junit.runners.model.InitializationError;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.internal.dependency.DependencyJar;
import org.robolectric.internal.dependency.DependencyResolver;
import org.robolectric.util.Util;