忍者ブログ

Azukish

消えゆく世界と流れる未来に最後の灯を since 2006/4/3

2024/04/24

[PR]

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

コメント

ただいまコメントを受けつけておりません。

2014/10/30

【Android】ListFragmentとかいうのを使ってみる

だいぶ寒くなってきました。
そんな寒さにも負けず、Androidの開発の練習ちっくなことをやっとります。
なんか動的に扱う場合はListViewよりもListFragmentの方が使いやすいようなので、ちょっと使ってみることに。
参考というか、ほとんど丸パクリだけれど、ListFragment の基本的な使い方 - Android 開発入門を見ながら 実行してみました。


src/com.example.listfragment/MainActivity.java
package com.example.listFragmentTest;

import com.example.listFragmentTest.R;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
}

res/layout/activity_main.xml
<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout
  android:layout_height="match_parent"
  android:layout_width="match_parent"
  android:orientation="horizontal"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools">
<fragment
  android:id="@+id/list_parent"
  android:layout_height="match_parent/"
  android:layout_weight="1"
  android:layout_width="0dp"
  class="com.example.listFragmentTest.ItemListFragment">
</fragment>
</linearlayout>

src/com.example.listfragment/ItemListFragment.java
package com.example.listFragmentTest;

import com.example.listFragmentTest.R;
import android.app.ListFragment;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class ItemListFragment extends ListFragment {
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  ArrayAdapter adapter =
    ArrayAdapter.createFromResource(
    getActivity(),
    R.array.list,
    android.R.layout.simple_list_item_1);
  setListAdapter(adapter);
  }
}

res/layout/layout_list.xml
<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal">
  <fragment
    android:id="@+id/list_child"
    class="com.example.listFragmentTest.ItemListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/> 
  </fragment>
</linearlayout>

res/values/styles.xml
<!--?xml version="1.0" encoding="utf-8"?-->
<resources>
  <string name="app_name">
    listFragmentTest 
  </string>
    
  <string name="action_settings">
    Settings 
  </string>
    
  <string-array name="list">
    <item>a</item>
    <item>b</item>
    <item>c</item>
    <item>d</item>
  </string-array>
</resources>

で、様子は下の感じ。

拍手

コメント













カレンダー

03 2024/04 05
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

アーカイブ

AD

Azkishはamazon.co.jpを宣伝しリンクすることによって サイトが紹介料を獲得できる手段を提供することを目的に設定されたアフィリエイト宣伝プログラムである、 Amazonアソシエイト・プログラムの参加者です。