Skip to content

Commit

Permalink
recycler view support part 1.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashqal committed Sep 3, 2017
1 parent 91189dc commit 81c4f6a
Show file tree
Hide file tree
Showing 14 changed files with 644 additions and 255 deletions.
8 changes: 5 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion '25.0.0'
compileSdkVersion 25
buildToolsVersion '25.0.2'

defaultConfig {
applicationId "com.asha.md360player4android"
Expand All @@ -23,9 +23,11 @@ android {
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.squareup.picasso:picasso:2.5.2'
//required, enough for most devices.
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'tv.danmaku.ijk.media:ijkplayer-java:0.6.0'
compile 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.6.0'
compile project(path: ':vrlib')
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".DemoActivity" android:configChanges="screenSize|orientation" android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.MAIN" />-->
<!--<category android:name="android.intent.category.LAUNCHER" />-->
<!--</intent-filter>-->
</activity>
<activity android:label="IjkPlayerDemoActivity" android:name=".IjkPlayerDemoActivity" android:screenOrientation="landscape" android:configChanges="screenSize|orientation" />
<activity android:label="VideoPlayerActivity" android:name=".VideoPlayerActivity" android:screenOrientation="landscape" android:configChanges="screenSize|orientation" />
<activity android:label="BitmapPlayerActivity" android:name=".BitmapPlayerActivity" android:screenOrientation="landscape" android:configChanges="screenSize|orientation" />
<activity android:label="CubemapPlayerActivity" android:name=".CubemapPlayerActivity" android:screenOrientation="landscape" android:configChanges="screenSize|orientation" />
<activity android:name=".RecyclerViewActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
package com.asha.md360player4android;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.asha.vrlib.MDVRLibrary;
import com.asha.vrlib.texture.MD360BitmapTexture;
import com.google.android.apps.muzei.render.GLTextureView;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import static com.asha.vrlib.MDVRLibrary.INTERACTIVE_MODE_CARDBORAD_MOTION_WITH_TOUCH;

public class RecyclerViewActivity extends AppCompatActivity {

private Uri[] sMockData;

private static final String TAG = "MainActivity";

private VRLibManager manager;

public RecyclerViewActivity() {
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sMockData = new Uri[] {
getDrawableUri(R.drawable.bitmap360)
,getDrawableUri(R.drawable.texture)
};

setContentView(R.layout.activity_main);
manager = new VRLibManager(this);

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
final FeedAdapter adapter = new FeedAdapter();
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(null);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState != RecyclerView.SCROLL_STATE_DRAGGING) {
int pos = layoutManager.findFirstCompletelyVisibleItemPosition();
adapter.onPosition(pos);
}
}
});
recyclerView.setAdapter(adapter);
}

@Override
protected void onResume() {
super.onResume();
manager.fireResumed();
}

@Override
protected void onPause() {
super.onPause();
manager.firePaused();
}

@Override
protected void onDestroy() {
super.onDestroy();
manager.fireDestroy();
}

private static class FeedModel {

private final Uri uri;
private final int index;
private int pos;

public FeedModel(int index, Uri uri) {
this.index = index;
this.uri = uri;
}

public void setPos(int pos) {
this.pos = pos;
}
}

private abstract class FeedVH extends RecyclerView.ViewHolder {

public FeedVH(ViewGroup vp, int layoutId) {
super(create(vp, layoutId));
}

public abstract void bind(FeedModel feedModel);
}

private class FeedSimpleVH extends FeedVH {

public FeedSimpleVH(ViewGroup vp) {
super(vp, R.layout.feed_simple_layout);
}

@Override
public void bind(FeedModel feedModel) {
}
}

private class FeedVRVH extends FeedVH implements MDVRLibrary.IBitmapProvider {

private ImageView cover;

private TextView text;

private GLTextureView glTextureView;

private ViewGroup parent;

private MDVRLibrary vrlib;

private FeedModel model;

public FeedVRVH(ViewGroup vp) {
super(vp, R.layout.feed_panorama_layout);
cover = (ImageView) itemView.findViewById(R.id.feed_img_cover);
text = (TextView) itemView.findViewById(R.id.feed_text);
glTextureView = (GLTextureView) itemView.findViewById(R.id.feed_texture_view);
parent = (ViewGroup) glTextureView.getParent();
}

@Override
public void bind(FeedModel model) {
Log.d(TAG, "FeedVRVH bind.");
this.model = model;
// Picasso.with(itemView.getContext()).load(model.url).into(cover);
ensureVRLib();
vrlib.notifyPlayerChanged();
/*
if (model.pos == model.index) {
text.setText("on");
if (glTextureView.getParent() == null) {
// parent.addView(glTextureView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
} else {
text.setText("off");
// parent.removeView(glTextureView);
}
*/
}

private void ensureVRLib() {
if (vrlib == null) {
vrlib = manager.create(this, glTextureView);
}
}

@Override
public void onProvideBitmap(final MD360BitmapTexture.Callback callback) {
if (model == null) {
return;
}
Log.d(TAG, "onProvideBitmap");
Picasso.with(itemView.getContext()).load(model.uri).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.d(TAG, "onProvideBitmap onBitmapLoaded");

vrlib.onTextureResize(bitmap.getWidth(), bitmap.getHeight());
callback.texture(bitmap);
}

@Override
public void onBitmapFailed(Drawable errorDrawable) {

}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {

}
});
}
}

private Uri getDrawableUri(@DrawableRes int resId){
Resources resources = getResources();
return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + resources.getResourcePackageName(resId) + '/' + resources.getResourceTypeName(resId) + '/' + resources.getResourceEntryName(resId) );
}

private class FeedAdapter extends RecyclerView.Adapter<FeedVH> {

private List<FeedModel> feeds = new ArrayList<>();

public FeedAdapter() {
int i = -1;
while (i++ < 50) {
Uri url = sMockData[(int) (Math.random() * sMockData.length)];
feeds.add(new FeedModel(i, url));
}
}

@Override
public FeedVH onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 0) {
return new FeedVRVH(parent);
} else {
return new FeedSimpleVH(parent);
}

}

@Override
public void onBindViewHolder(FeedVH holder, int position) {
holder.bind(feeds.get(position));
}

@Override
public void onViewRecycled(FeedVH holder) {
super.onViewRecycled(holder);
}

@Override
public int getItemViewType(int position) {
return position == 0 ? 0 : 1;
}

@Override
public int getItemCount() {
return feeds.size();
}

private int prevPos = -1;
public void onPosition(int pos) {
if (prevPos == pos) {
return;
}

for (FeedModel feedModel : feeds) {
feedModel.setPos(pos);
}

notifyItemChanged(prevPos);
notifyItemChanged(pos);
prevPos = pos;
}
}

private static View create(ViewGroup vp, int layout) {
return LayoutInflater.from(vp.getContext()).inflate(layout, vp, false);
}

private static class VRLibManager {
private Activity activity;

private boolean isResumed;

private List<MDVRLibrary> libs = new LinkedList<>();

public VRLibManager(Activity activity) {
this.activity = activity;
}

public MDVRLibrary create(MDVRLibrary.IBitmapProvider provider, GLTextureView textureView) {
MDVRLibrary lib = MDVRLibrary.with(activity).
asBitmap(provider).
interactiveMode(INTERACTIVE_MODE_CARDBORAD_MOTION_WITH_TOUCH).
build(textureView);
add(lib);
return lib;
}

private void add(MDVRLibrary lib) {
if (isResumed) {
lib.onResume(activity);
}

libs.add(lib);
}

public void fireResumed() {
isResumed = true;
for (MDVRLibrary library : libs) {
library.onResume(activity);
}
}

public void firePaused() {
isResumed = false;
for (MDVRLibrary library : libs) {
library.onPause(activity);
}
}

public void fireDestroy() {
for (MDVRLibrary library : libs) {
library.onDestroy();
}
}
}
}
13 changes: 13 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:background="#EEE"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</FrameLayout>
Loading

0 comments on commit 81c4f6a

Please sign in to comment.