Last active
December 22, 2016 14:52
-
-
Save neonankiti/6aa5b6266603fc399403eaf804cc80c0 to your computer and use it in GitHub Desktop.
SNSタイムライン動画の自動再生 ref: http://qiita.com/neonankiti/items/cfa8cc0d3c48b9edc2fb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ネットワーク状態の取得 | |
private static NetworkInfo getNetworkInfo(@NonNull Context context) { | |
ConnectivityManager connectivityManager | |
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
return connectivityManager.getActiveNetworkInfo(); | |
} | |
// Wifiステータス | |
public static boolean isWifi(@NonNull Context context) { | |
return getNetworkType(context) == ConnectivityManager.TYPE_WIFI; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int getMovieStartPosition(RecyclerView recyclerView) { | |
// 最初のコンテンツの取得 | |
int firstVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition(); | |
// 最後のコンテンツの取得 | |
int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition(); | |
// コンテンツの大きさにより、画面上に表示されるコンテンツの数は異なる可能性がある。 | |
if (lastVisibleItemPosition - firstVisibleItemPosition == 0) { | |
return firstVisibleItemPosition; | |
} | |
// 画面上に表示される最初と最後のアイテムのpositionの差分が1以上のとき、表示するべきコンテンツ一つを特定する必要がある。 | |
float ratio = 0.0f; | |
int startPosition = firstVisibleItemPosition; | |
// コンテンツ分for文を回す。 | |
for (int i = 0; i < lastVisibleItemPosition - firstVisibleItemPosition + 1; i++) { | |
// positionのadapterを取得する | |
RecyclerView.ViewHolder holder = recyclerView.findViewHolderForAdapterPosition(firstVisibleItemPosition + i); | |
// findViewHolderForAdapterPosition could return null. | |
if (holder == null) { | |
continue; | |
} | |
// 各コンテンツのTop, Bottomの座標を取得する | |
int topPosition = holder.itemView.getTop() < 0 ? 0 : holder.itemView.getTop(); | |
int bottomPosition = holder.itemView.getBottom() > displaymetrics.heightPixels | |
? displaymetrics.heightPixels | |
: holder.itemView.getBottom(); | |
// 画面縦の比率を取得して、コンテンツごとに占有面積を比較。 | |
float pointedRatio = (bottomPosition - topPosition) / (float) displaymetrics.heightPixels; | |
if (pointedRatio > ratio) { | |
ratio = pointedRatio; | |
startPosition = firstVisibleItemPosition + i; | |
} | |
} | |
return startPosition; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface OnPositionListener { | |
void insideScreen(int startPosition); | |
} | |
// positionのlisterはMapで持ちます。 | |
private HashMap<Integer, OnCompletelyVisiblePositionListener> positionListeners = new HashMap<>(); | |
public void setOnPositionListener( | |
int position, OnPositionListener positionListener) { | |
this.positionListeners.put(position, positionListener); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private int getMovieStartPosition(RecyclerView recyclerView) { | |
// 最初のコンテンツの取得 | |
int firstVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition(); | |
// 最後のコンテンツの取得 | |
int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition(); | |
// コンテンツの大きさにより、画面上に表示されるコンテンツの数は異なる可能性がある。 | |
if (lastVisibleItemPosition - firstVisibleItemPosition == 0) { | |
return firstVisibleItemPosition; | |
} | |
// 画面上に表示される最初と最後のアイテムのpositionの差分が1以上のとき、表示するべきコンテンツ一つを特定する必要がある。 | |
float ratio = 0.0f; | |
int startPosition = firstVisibleItemPosition; | |
// コンテンツ分for文を回す。 | |
for (int i = 0; i < lastVisibleItemPosition - firstVisibleItemPosition + 1; i++) { | |
// positionのadapterを取得する | |
RecyclerView.ViewHolder holder = recyclerView.findViewHolderForAdapterPosition(firstVisibleItemPosition + i); | |
// findViewHolderForAdapterPosition could return null. | |
if (holder == null) { | |
continue; | |
} | |
// 各コンテンツのTop, Bottomの座標を取得する | |
int topPosition = holder.itemView.getTop() < 0 ? 0 : holder.itemView.getTop(); | |
int bottomPosition = holder.itemView.getBottom() > displaymetrics.heightPixels | |
? displaymetrics.heightPixels | |
: holder.itemView.getBottom(); | |
// 画面縦の比率を取得して、コンテンツごとに占有面積を比較。 | |
float pointedRatio = (bottomPosition - topPosition) / (float) displaymetrics.heightPixels; | |
if (pointedRatio > ratio) { | |
ratio = pointedRatio; | |
startPosition = firstVisibleItemPosition + i; | |
} | |
} | |
return startPosition; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private final RecyclerView.OnScrollListener listener = new RecyclerView.OnScrollListener() { | |
@Override | |
public void onScrollStateChanged(RecyclerView recyclerView, int newState) { | |
insideScreen(getMovieStartPosition(recyclerView)); | |
} | |
}; | |
@Override | |
public void insideScreen(int startPosition) { | |
for (Map.Entry<Integer, OnCompletelyVisiblePositionListener> e : onCompletelyVisiblePositionListeners.entrySet()) { | |
e.getValue().insideScreen(startPosition); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment