Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.

Commit 490b097

Browse files
authored
Merge pull request #19 from amardeshbd/feature/2_grid_view_for_layout_selection
Feature/2 grid view for layout selection
2 parents 9cd4484 + 2964916 commit 490b097

File tree

10 files changed

+193
-46
lines changed

10 files changed

+193
-46
lines changed

app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ dependencies {
4747
// Android Support libs and Google Android libs
4848
implementation "com.android.support:support-v13:$rootProject.supportLibraryVersion"
4949
implementation "com.android.support:design:$rootProject.supportLibraryVersion"
50+
implementation "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
5051

5152
implementation "com.android.support.constraint:constraint-layout:$rootProject.constraintLayoutVersion"
5253

@@ -55,7 +56,7 @@ dependencies {
5556
implementation "android.arch.lifecycle:extensions:$rootProject.archComponentVersion"
5657

5758
// https://developer.chrome.com/multidevice/android/customtabs
58-
compile "com.android.support:customtabs:$rootProject.supportLibraryVersion"
59+
implementation "com.android.support:customtabs:$rootProject.supportLibraryVersion"
5960

6061
// ========================================================
6162
// 3rd party libraries

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
android:roundIcon="@mipmap/ic_launcher_round"
1111
android:supportsRtl="true"
1212
android:theme="@style/AppTheme">
13-
<activity android:name=".MainActivity">
13+
<activity android:name=".browse.MainActivity">
1414
<intent-filter>
1515
<action android:name="android.intent.action.MAIN" />
1616

app/src/main/java/com/hossainkhan/android/demo/MainActivity.kt

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2018 Hossain Khan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hossainkhan.android.demo.browse
18+
19+
import android.content.Context
20+
import android.support.v7.widget.RecyclerView
21+
import android.view.LayoutInflater
22+
import android.view.View
23+
import android.view.ViewGroup
24+
import android.widget.TextView
25+
import com.hossainkhan.android.demo.R
26+
import com.hossainkhan.android.demo.data.LayoutInformation
27+
28+
class LayoutBrowseAdapter(
29+
private val data: List<LayoutInformation>,
30+
private val itemSelectedListener: (Int) -> Unit) : RecyclerView.Adapter<LayoutBrowseAdapter.ViewHolder>() {
31+
32+
// Provide a reference to the views for each data item
33+
// Complex data items may need more than one view per item, and
34+
// you provide access to all the views for a data item in a view holder.
35+
// Each data item is just a string in this case that is shown in a TextView.
36+
class ViewHolder(itemViewRoot: View,
37+
private val onClickListener: (Int) -> Unit) : RecyclerView.ViewHolder(itemViewRoot) {
38+
val itemName = itemViewRoot.findViewById<TextView>(R.id.layout_preview_name)!!
39+
40+
init {
41+
itemViewRoot.setOnClickListener {
42+
onClickListener(adapterPosition)
43+
}
44+
}
45+
}
46+
47+
48+
// Create new views (invoked by the layout manager)
49+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
50+
// create a new view
51+
val itemView = LayoutInflater.from(parent.context)
52+
.inflate(R.layout.list_item_layout_preview, parent, false)
53+
// set the view's size, margins, paddings and layout parameters
54+
55+
return ViewHolder(itemView, this::itemClickHandler)
56+
}
57+
58+
// Replace the contents of a view (invoked by the layout manager)
59+
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
60+
// - get element from your dataset at this position
61+
// - replace the contents of the view with that element
62+
holder.itemName.text = data[position].title
63+
}
64+
65+
// Return the size of your dataset (invoked by the layout manager)
66+
override fun getItemCount() = data.size
67+
68+
private fun itemClickHandler(position: Int) {
69+
itemSelectedListener(data[position].layoutResourceId)
70+
}
71+
72+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2018 Hossain Khan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hossainkhan.android.demo.browse
18+
19+
import android.support.v7.app.AppCompatActivity
20+
import android.os.Bundle
21+
import android.support.v7.widget.LinearLayoutManager
22+
import android.support.v7.widget.RecyclerView
23+
import android.view.View
24+
import com.hossainkhan.android.demo.R
25+
import com.hossainkhan.android.demo.data.AppDataStore
26+
import com.hossainkhan.android.demo.layoutpositioning.LayoutPositioningDemoActivity
27+
import dagger.android.AndroidInjection
28+
import timber.log.Timber
29+
import javax.inject.Inject
30+
31+
class MainActivity : AppCompatActivity() {
32+
@Inject
33+
lateinit var appDataStore: AppDataStore
34+
35+
private lateinit var recyclerView: RecyclerView
36+
private lateinit var viewAdapter: RecyclerView.Adapter<*>
37+
private lateinit var viewManager: RecyclerView.LayoutManager
38+
39+
override fun onCreate(savedInstanceState: Bundle?) {
40+
AndroidInjection.inject(this)
41+
super.onCreate(savedInstanceState)
42+
setContentView(R.layout.activity_main)
43+
44+
Timber.d("Got data: ${appDataStore.isFirstTime()}")
45+
appDataStore.updateFirstTimeUser(false)
46+
47+
viewManager = LinearLayoutManager(this)
48+
viewAdapter = LayoutBrowseAdapter(
49+
data = appDataStore.layoutStore.supportedLayoutInfos,
50+
itemSelectedListener = this::onLayoutItemSelected)
51+
52+
recyclerView = findViewById<RecyclerView>(R.id.recycler_view).apply {
53+
// use this setting to improve performance if you know that changes
54+
// in content do not change the layout size of the RecyclerView
55+
setHasFixedSize(true)
56+
57+
// use a linear layout manager
58+
layoutManager = viewManager
59+
60+
// specify an viewAdapter (see also next example)
61+
adapter = viewAdapter
62+
}
63+
}
64+
65+
fun onLayoutItemSelected(layoutResId: Int) {
66+
Timber.i("Selected layout id: %s", layoutResId)
67+
68+
startActivity(LayoutPositioningDemoActivity
69+
.createStartIntent(this, layoutResId))
70+
}
71+
}

app/src/main/java/com/hossainkhan/android/demo/dagger/MainActivityModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.hossainkhan.android.demo.dagger
1818

1919
import android.app.Activity
20-
import com.hossainkhan.android.demo.MainActivity
20+
import com.hossainkhan.android.demo.browse.MainActivity
2121
import dagger.Binds
2222
import dagger.Module
2323
import dagger.android.ActivityKey

app/src/main/java/com/hossainkhan/android/demo/dagger/MainActivitySubcomponent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.hossainkhan.android.demo.dagger
1818

19-
import com.hossainkhan.android.demo.MainActivity
19+
import com.hossainkhan.android.demo.browse.MainActivity
2020
import dagger.Subcomponent
2121
import dagger.android.AndroidInjector
2222

app/src/main/java/com/hossainkhan/android/demo/data/LayoutDataStore.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import javax.inject.Singleton
2626
@Singleton
2727
class LayoutDataStore @Inject constructor(
2828
private val resources: Resources) {
29-
private val supportedLayoutInfos = listOf(
29+
val supportedLayoutInfos = listOf(
3030
LayoutInformation(
3131
layoutResourceId = R.layout.activity_positioning_top_left,
3232
title = "Positioning: Top Left",

app/src/main/res/layout/activity_main.xml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
7-
tools:context="com.hossainkhan.android.demo.MainActivity">
7+
tools:context="com.hossainkhan.android.demo.browse.MainActivity">
88

9-
<TextView
10-
android:layout_width="wrap_content"
11-
android:layout_height="wrap_content"
12-
android:text="Hello World!"
13-
android:onClick="goSomewhere"
14-
app:layout_constraintBottom_toBottomOf="parent"
15-
app:layout_constraintLeft_toLeftOf="parent"
16-
app:layout_constraintRight_toRightOf="parent"
17-
app:layout_constraintTop_toTopOf="parent" />
9+
<android.support.v7.widget.RecyclerView
10+
android:id="@+id/recycler_view"
11+
android:layout_width="match_parent"
12+
android:layout_height="match_parent"
13+
android:scrollbars="vertical"
14+
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
1815

1916
</android.support.constraint.ConstraintLayout>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
~ Copyright (c) 2018 Hossain Khan
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
17+
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:card_view="http://schemas.android.com/apk/res-auto"
19+
android:layout_width="match_parent"
20+
android:layout_height="200dp"
21+
android:layout_margin="10dp"
22+
card_view:cardCornerRadius="0dp"
23+
card_view:cardElevation="5dp"
24+
card_view:cardMaxElevation="5dp"
25+
card_view:contentPadding="5dp">
26+
27+
<android.support.constraint.ConstraintLayout
28+
android:layout_width="match_parent"
29+
android:layout_height="match_parent">
30+
31+
<TextView
32+
android:id="@+id/layout_preview_name"
33+
android:layout_width="wrap_content"
34+
android:layout_height="wrap_content" />
35+
36+
</android.support.constraint.ConstraintLayout>
37+
</android.support.v7.widget.CardView>

0 commit comments

Comments
 (0)