Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProteusGridLayoutManager and ViewPager Example implemented #166

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ buildscript {
google()
mavenCentral()
jcenter()
maven { url "https://jitpack.io" }

}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
Expand Down
8 changes: 8 additions & 0 deletions data/layout.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@
"padding": "16dp",
"paddingBottom": "0dp"
},

{
"type": "include",
"layout": "ViewPagerExample",
"padding": "16dp",
"paddingBottom": "0dp"
},

{
"type": "include",
"layout": "SimpleDataBindingExample",
Expand Down
52 changes: 50 additions & 2 deletions data/layouts.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
"items": "@{data.achievements}"
},
"layout_manager": {
"type": "LinearLayoutManager"
"type": "GridLayoutManager"
},
"adapter": {
"@": {
Expand All @@ -270,13 +270,61 @@
"gravity": "center",
"text": "@{item}",
"textSize": "14sp",
"textColor": "#323232"
"textColor": "#323232",
"onClick":"@{item}"
}
}
}
}
]
},

"ViewPagerExample": {
"type": "FrameLayout",
"layout_width": "match_parent",
"layout_height": "wrap_content",
"padding": "16dp",
"paddingBottom": "0dp",
"children": [
{
"type": "RecyclerView",
"id" : "rc_view",
"layout_width": "match_parent",
"layout_height": "148dp",
"background": "#ffffff",
"data": {
"items": "@{data.achievements}"
},
"layout_manager": {
"type": "GridLayoutManager",
"orientation" : 1,
"numCols" : 2
},
"adapter": {
"@": {
"type": "SimpleListAdapter",
"item-count": "@{items.$length}",
"item-layout": {
"type": "TextView",
"data": {
"item": "@{items[$index]}"
},
"layout_width": "match_parent",
"layout_height": "match_parent",
"padding": "12dp",
"layout_marginBottom": "4dp",
"gravity": "center",
"text": "@{item}",
"textSize": "44sp",
"textColor": "#323232",
"onClick":"@{item}"
}
}
}
}
]
},

"AlertDialogLayout": {
"type": "LinearLayout",
"orientation": "vertical",
Expand Down
2 changes: 1 addition & 1 deletion data/user.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"0": {
"name": "John Doe",
"name": "Ravi Ranjan",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please revert this?

"location": {
"country": "India",
"city": "Bengaluru",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

public class ProteusActivity extends AppCompatActivity implements ProteusManager.Listener {

Expand Down Expand Up @@ -216,6 +219,44 @@ void render() {

// Add the inflated view to the container
container.addView(view.getAsView());



Comment on lines +223 to +224
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these excess new lines be removed?

LinearSnapHelper snapHelper = new LinearSnapHelper() {
@Override
public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
View centerView = findSnapView(layoutManager);
if (centerView == null)
return RecyclerView.NO_POSITION;

int position = layoutManager.getPosition(centerView);
int targetPosition = -1;
if (layoutManager.canScrollHorizontally()) {
if (velocityX < 0) {
targetPosition = position - 1;
} else {
targetPosition = position + 1;
}
}

if (layoutManager.canScrollVertically()) {
if (velocityY < 0) {
targetPosition = position - 1;
} else {
targetPosition = position + 1;
}
}

final int firstItem = 0;
final int lastItem = layoutManager.getItemCount() - 1;
targetPosition = Math.min(lastItem, Math.max(targetPosition, firstItem));
return targetPosition;
}
};

View recyclerView = view.getViewManager().findViewById("rc_view");
snapHelper.attachToRecyclerView((RecyclerView) recyclerView);

}

void reload() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.flipkart.android.proteus.support.v7.adapter.SimpleListAdapter;
import com.flipkart.android.proteus.support.v7.layoutmanager.LayoutManagerBuilder;
import com.flipkart.android.proteus.support.v7.layoutmanager.LayoutManagerFactory;
import com.flipkart.android.proteus.support.v7.layoutmanager.ProteusGridLayoutManager;
import com.flipkart.android.proteus.support.v7.layoutmanager.ProteusLinearLayoutManager;
import com.flipkart.android.proteus.support.v7.widget.RecyclerViewParser;

Expand All @@ -42,6 +43,8 @@ public class RecyclerViewModule implements ProteusBuilder.Module {
static final String ADAPTER_SIMPLE_LIST = "SimpleListAdapter";

static final String LAYOUT_MANAGER_LINEAR = "LinearLayoutManager";
static final String LAYOUT_MANAGER_GRID = "GridLayoutManager";


@NonNull
private final RecyclerViewAdapterFactory adapterFactory;
Expand Down Expand Up @@ -175,7 +178,7 @@ public RecyclerViewModule build() {
}

if (includeDefaultLayoutManagers) {
registerDefaultLayoutManagers();
registerGridLayoutManagers();
}

return new RecyclerViewModule(adapterFactory, layoutManagerFactory);
Expand All @@ -188,5 +191,9 @@ private void registerDefaultAdapters() {
private void registerDefaultLayoutManagers() {
register(LAYOUT_MANAGER_LINEAR, ProteusLinearLayoutManager.BUILDER);
}
Comment on lines 191 to 193
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be deleted now?


private void registerGridLayoutManagers() {
register(LAYOUT_MANAGER_GRID, ProteusGridLayoutManager.BUILDER);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.flipkart.android.proteus.support.v7.layoutmanager;

import android.content.Context;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.flipkart.android.proteus.support.v7.widget.ProteusRecyclerView;
import com.flipkart.android.proteus.value.ObjectValue;

public class ProteusGridLayoutManager extends GridLayoutManager {


private static final String ATTRIBUTE_COL = "numCols";
private static final String ATTRIBUTE_ORIENTATION = "orientation";
private static final String ATTRIBUTE_REVERSE_LAYOUT = "reverse";


public static final LayoutManagerBuilder<ProteusGridLayoutManager> BUILDER = new LayoutManagerBuilder<ProteusGridLayoutManager>() {

@NonNull
@Override
public ProteusGridLayoutManager create(@NonNull ProteusRecyclerView view, @NonNull ObjectValue config) {

int orientation = config.getAsInteger(ATTRIBUTE_ORIENTATION, GridLayoutManager.VERTICAL);
boolean reverseLayout = config.getAsBoolean(ATTRIBUTE_REVERSE_LAYOUT, false);

int col = config.getAsInteger(ATTRIBUTE_COL, 1);



return new ProteusGridLayoutManager(view.getContext(), col, orientation, reverseLayout);
}
};


public ProteusGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
}





Comment on lines +13 to +45
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the excess new lines be removed?

}