Android中demo的编写

  • Post author:
  • Post category:其他




Android中demo的编写

在Android开放中,难免需要学习或验证某个问题,编写一个demo运行一下可以很好的满足这个需求。这样的demo需要重点突出,且只针对某个问题,编写快速,添加或者删除方便。

将某个功能点放到一个单独的model中,需要验证某个功能时就编写关于这个功能的一个单独的model。这个功能点中的每一个子功能都放到一个单独的Activity中。

这样进入这个demo就是一个列表,点击列表的某一项就跳转到某一个model中。

进入这个model又是一个列表,点击列表的某一项跳转到某一个具体的功能里面。

通过这两个列表页一个详情页就能很好的满足上面的需求。



继承 ListActivity

主Activity代码

import android.app.ListActivity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ListView
import android.widget.SimpleAdapter

class MainActivity : ListActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        listAdapter = SimpleAdapter(this, getData(),
            android.R.layout.simple_list_item_1, arrayOf("title"),
            intArrayOf(android.R.id.text1))
    }
    private fun getData(): List<Map<String, Any>> {
        val myData = mutableListOf<Map<String, Any>>()
        myData.add(mapOf("title" to "RecyclerView",
            "intent" to activityToIntent(RecyclerViewActivity::class.java.name)))
        myData.add(mapOf("title" to "ViewPager2",
            "intent" to activityToIntent(ViewPager2Activity::class.java.name)))
        myData.add(mapOf("title" to "9Png",
            "intent" to activityToIntent(PngMainActivity::class.java.name)))
        return myData
    }

    private fun activityToIntent(activity: String): Intent =
        Intent(Intent.ACTION_VIEW).setClassName(this.packageName, activity)

    override fun onListItemClick(listView: ListView, view: View, position: Int, id: Long) {
        val map = listView.getItemAtPosition(position) as Map<*, *>
        val intent = Intent(map["intent"] as Intent)
        intent.addCategory(Intent.CATEGORY_SAMPLE_CODE)
        startActivity(intent)
    }
}

model中主Activity的代码

import android.app.ListActivity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ListView
import android.widget.SimpleAdapter

class RecyclerViewActivity : ListActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        listAdapter = SimpleAdapter(this, getData(),
            android.R.layout.simple_list_item_1, arrayOf("title"),
            intArrayOf(android.R.id.text1))
    }
    private fun getData(): List<Map<String, Any>> {
        val myData = mutableListOf<Map<String, Any>>()
        myData.add(mapOf("title" to "RecyclerView1",
            "intent" to activityToIntent(RecyclerView1Activity::class.java.name)))
        myData.add(mapOf("title" to "RecyclerView2",
            "intent" to activityToIntent(ViewPager2Activity::class.java.name)))
        myData.add(mapOf("title" to "RecyclerView3",
            "intent" to activityToIntent(RecyclerView3Activity::class.java.name)))
        return myData
    }

    private fun activityToIntent(activity: String): Intent =
        Intent(Intent.ACTION_VIEW).setClassName(this.packageName, activity)

    override fun onListItemClick(listView: ListView, view: View, position: Int, id: Long) {
        val map = listView.getItemAtPosition(position) as Map<*, *>
        val intent = Intent(map["intent"] as Intent)
        intent.addCategory(Intent.CATEGORY_SAMPLE_CODE)
        startActivity(intent)
    }
}

子model中的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hsz.viewpager2.integration.testapp">
    <application>
    <activity android:name=".RecyclerViewActivity" android:exported="true">
        <intent-filter>
             <action android:name="android.intent.action.MAIN"/>
             <category android:name="android.intent.category.SAMPLE_CODE"/>
            </intent-filter>
    </activity>
    <activity android:name=".RecyclerView1Activity" android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.SAMPLE_CODE"/>
        </intent-filter>
    </activity>

    <activity android:name=".RecyclerView2Activity" android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.SAMPLE_CODE"/>
        </intent-filter>
    </activity>

    <activity android:name=".RecyclerView3Activity" android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.SAMPLE_CODE"/>
        </intent-filter>
    </activity>
        
    </application>
</manifest>



版权声明:本文为cdc_csdn原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。