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

Updated entire code library to Kotlin #131

Open
wants to merge 2 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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ local.properties

# Gradle
.gradle
build/
build/
.idea/instapk.xml
instapk.log*
59 changes: 30 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ AndroidTreeView
### Recent changes


Entire library code converted to Kotlin for better stability and reducing null pointer errors though could be used with java as well

2D scrolling mode added, keep in mind this comes with few limitations: you won't be able not place views on right side like alignParentRight. Everything should be align left. Is not enabled by default


Expand Down Expand Up @@ -43,61 +45,60 @@ Tree view implementation for android

**1)** Add library as a dependency to your project

```compile 'com.github.bmelnychuk:atv:1.2.+'```
```compile 'com.github.bmelnychuk:atv:1.3.+'```

**2)** Create your tree starting from root element. ```TreeNode.root()``` element will not be displayed so it doesn't require anything to be set.
```java
TreeNode root = TreeNode.root();
```Kotlin
val root = TreeNode.root()
```

Create and add your nodes (use your custom object as constructor param)
```java
TreeNode parent = new TreeNode("MyParentNode");
TreeNode child0 = new TreeNode("ChildNode0");
TreeNode child1 = new TreeNode("ChildNode1");
parent.addChildren(child0, child1);
root.addChild(parent);
```Kotlin
val parent = TreeNode("MyParentNode")
val child0 = TreeNode("ChildNode0")
val child1 = TreeNode("ChildNode1")
parent.addChildren(child0, child1)
root.addChild(parent)
```

**3)** Add tree view to layout
```java
AndroidTreeView tView = new AndroidTreeView(getActivity(), root);
containerView.addView(tView.getView());
```Kotlin
val tView = AndroidTreeView(getActivity(), root)
containerView.addView(tView.getView())
```
The simplest but not styled tree is ready. Now you can see ```parent``` node as root of your tree

**4)** Custom view for nodes

Extend ```TreeNode.BaseNodeViewHolder``` and overwrite ```createNodeView``` method to prepare custom view for node:
```java
public class MyHolder extends TreeNode.BaseNodeViewHolder<IconTreeItem> {
```Kotlin
class MyHolder : TreeNode.BaseNodeViewHolder<IconTreeItem> {
...
@Override
public View createNodeView(TreeNode node, IconTreeItem value) {
final LayoutInflater inflater = LayoutInflater.from(context);
final View view = inflater.inflate(R.layout.layout_profile_node, null, false);
TextView tvValue = (TextView) view.findViewById(R.id.node_value);
tvValue.setText(value.text);
override fun createNodeView(val node, val value) : View?{
val inflater = LayoutInflater?.from(context)
val view = inflater?.inflate(R.layout.layout_profile_node, container, false)
val tvValue = view?.findViewById(R.id.node_value) as? TextView
tvValue.setText(value.text)

return view;
return view
}
...
public static class IconTreeItem {
public int icon;
public String text;
class IconTreeItem {
val icon
val text
}
}
```

**5)** Connect view holder with node
```java
IconTreeItem nodeItem = new IconTreeItem();
TreeNode child1 = new TreeNode(nodeItem).setViewHolder(new MyHolder(mContext));
```Kotlin
val nodeItem = IconTreeItem()
val child1 = TreeNode(nodeItem).setViewHolder(new MyHolder(mContext))
```

**6)** Consider using
```java
TreeNode.setClickListener(TreeNodeClickListener listener);
```Kotlin
TreeNode.setClickListener(TreeNodeClickListener listener)
AndroidTreeView.setDefaultViewHolder
AndroidTreeView.setDefaultNodeClickListener
...
Expand Down
6 changes: 4 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
Expand All @@ -20,7 +21,8 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.github.johnkil.print:print:1.2.2'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.github.johnkil.print:print:1.3.1'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile project(':library')
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.unnamed.b.atv.sample.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
import com.unnamed.b.atv.sample.R
import com.unnamed.b.atv.sample.fragment.*

/**
* Converted to Kolin by Kumar Shivang on 16/07/17
*/

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)


val listItems = LinkedHashMap<String, Class<*>>()
listItems.put("Folder Structure Example", FolderStructureFragment::class.java)
listItems.put("Custom Holder Example", CustomViewHolderFragment::class.java)
listItems.put("Selectable Nodes", SelectableTreeFragment::class.java)
listItems.put("2d scrolling", TwoDScrollingFragment::class.java)
listItems.put("Expand with arrow only", TwoDScrollingArrowExpandFragment::class.java)


val list = ArrayList(listItems.keys)
val listview = findViewById(R.id.listview) as ListView
val adapter = SimpleArrayAdapter(this, list)
listview.adapter = adapter
listview.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
val clazz = listItems.values.toTypedArray()[position]
val i = Intent(this@MainActivity, SingleFragmentActivity::class.java)
i.putExtra(SingleFragmentActivity.FRAGMENT_PARAM, clazz)
this@MainActivity.startActivity(i)
}

}

private inner class SimpleArrayAdapter(context: Context, objects: List<String>) : ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, objects) {

override fun getItemId(position: Int): Long {
return position.toLong()
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.unnamed.b.atv.sample.activity

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import com.unnamed.b.atv.sample.R

/**
* Created by Bogdan Melnychuk on 2/12/15.
* Converted to Kolin by Kumar Shivang on 16/07/17
*/
class SingleFragmentActivity : AppCompatActivity() {

override fun onCreate(bundle: Bundle?) {
super.onCreate(bundle)
setContentView(R.layout.activity_single_fragment)

val b = intent.extras
val fragmentClass = b.get(FRAGMENT_PARAM) as Class<*>
if (bundle == null) {
val f = Fragment.instantiate(this, fragmentClass.name)
f.arguments = b
supportFragmentManager.beginTransaction().replace(R.id.fragment, f, fragmentClass.name).commit()
}
}

companion object {
val FRAGMENT_PARAM = "fragment"
}
}

This file was deleted.

Loading