1. 기본 환경 세팅


- 두 액티비티 모두 Google Map 적용

- 사용자지정 액태비티 전환을 위한 코드 삽입

https://developer.android.com/training/material/animations.html?hl=ko#Transitions


2. Google Map에 적용

두 액티비티의 소스 코드 중 초기 Google Map Fragment를 가져오는 부분에 추가

val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.view!!.transitionName = resources.getString(R.string.shared_element_map)

Fragment의 View를 가져와 TransitionName을 넣는다. 

넣은 후 액티비티 전환을 위한 Intent의 Options을 정의할 때 다음과 같이 추가한다.

var options = ActivityOptionsCompat.makeSceneTransitionAnimation(this,
Pair<View, String>(map.view, ViewCompat.getTransitionName(map.view)))


by JamesY 2018. 1. 8. 17:54

Kotlin으로 작성된 커스텀 리스트뷰 예제



MainActivity.kt

import android.content.Context
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

val list: ArrayList<CustomItem> = ArrayList()
list.add(CustomItem("James", 23, "Male"))
list.add(CustomItem("Jamie", 20, "Female"))
list.add(CustomItem("John", 30, "Male"))

listview1.adapter = CustomAdapter(this, list)
}

private class CustomAdapter(context: Context, array: ArrayList<CustomItem>): BaseAdapter() {

private var list: ArrayList<CustomItem> = array
private var inflater: LayoutInflater = LayoutInflater.from(context)

override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {

val view: View?
val viewHolder: ViewHolder

if(p1 == null) {
view = this.inflater.inflate(R.layout.listview_item, p2,false)
viewHolder = ViewHolder(view)
view.tag = viewHolder
}
else {
view = p1
viewHolder = view.tag as ViewHolder
}

viewHolder.name.text = list[p0].name
viewHolder.age.text = list[p0].age.toString()
viewHolder.gender.text = list[p0].gender

return view!!
}

override fun getItem(p0: Int): Any {
return list[p0]
}

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

override fun getCount(): Int {
return list.size
}

}

private class ViewHolder(view: View?) {

val name: TextView = view?.findViewById(R.id.nameTextView) as TextView
val age: TextView = view?.findViewById(R.id.ageTextView) as TextView
val gender: TextView = view?.findViewById(R.id.genderTextView) as TextView
}

data class CustomItem(var name: String, var age: Int, var gender: String)
}


listview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />

<TextView
android:id="@+id/ageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />

<TextView
android:id="@+id/genderTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>


GitHub : https://github.com/Hot6ix/SimpleCustomListView

by JamesY 2017. 12. 21. 17:52

Volley를 이용하여 간단한 HTTP 통신을 하는 예제이다.


1. AndroidManifest.xml 에서 권한 명시 

<uses-permission android:name="android.permission.INTERNET" />


2. build.gradle 내  dependencies 에 Volley 사용을 위한 명시

compile 'com.android.volley:volley:1.1.0'


3. Build - Make Project 를 하여 프로젝트에서 Volley 사용 가능


4. HTTP 통신 예제

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log.i
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

lateinit var request: RequestQueue
lateinit var stringRequest: StringRequest

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

// Setup RequestQueue
request = Volley.newRequestQueue(this)
val url = "http://www.google.com"

// Setup StringRequest
stringRequest = StringRequest(Request.Method.GET, url, Response.Listener<String> {
response -> textView1.text = response // Print http source using textview
}, Response.ErrorListener {
error -> i(applicationContext.packageName, error.toString()) // Print log if error occurred
})

// Set tag for cancel
stringRequest.tag = applicationContext.packageName
// Request
request.add(stringRequest)
}

override fun onStop() {
super.onStop()

// Cancel all request that have packageName tag
if(request != null) {
request.cancelAll(applicationContext.packageName)
}
}
}


GitHub : https://github.com/Hot6ix/SimpleHttpRequest


출처 : https://developer.android.com/training/volley/simple.html

by JamesY 2017. 12. 21. 16:30
| 1 2 3 4 5 6 7 8 |