Android-list-fragment
Android-リストフラグメント
フレームワークのListFragmentの静的ライブラリサポートバージョン。 Android 3.0より前のプラットフォームで実行されるアプリを作成するために使用されます。 Android 3.0以降で実行する場合、この実装は引き続き使用されます。
'_リストフラグメントの基本的な実装は、フラグメント内のアイテムのリストを作成するためのものです_
フラグメントのリスト
例
この例では、arrayAdapterに基づいて独自のリストフラグメントを作成する方法を説明します。 それでは、Hello World Exampleの作成中に行った手順と同様に、次の手順に従ってみましょう-
Step | Description |
---|---|
1 | You will use Android Studio to create an Android application and name it as SimpleListFragment under a package com.example.finddevguides7.myapplication, with blank Activity. |
2 | Modify the string file, which has placed at res/values/string.xml to add new string constants |
3 | Create a layout called list_fragment.xml under the directory res/layout to define your list fragments. and add fragment tag(<fragment>) to your activity_main.xml |
4 | Create a myListFragment.java, which is placed at java/myListFragment.java and it contained onCreateView(),onActivityCreated() and OnItemClickListener() |
5 | Run the application to launch Android emulator and verify the result of the changes done in the application. |
コーディングを開始する前に、_res/values directory_の下の_string.xml_ファイル内の文字列定数を初期化します
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ListFragmentDemo</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="imgdesc">imgdesc</string>
<string-array name="Planets">
<item>Sun</item>
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>
以下は、 res/layout/activity_main.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"
android:orientation="vertical" >
<fragment
android:id="@+id/fragment1"
android:name="com.example.finddevguides7.myapplication.MyListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
以下は res/layout/list_fragment.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"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TextView>
</LinearLayout>
以下は、コードに書き込む前の src/main/java/myListFragment.java file.contentの内容です。以下に示すいくつかの手順に従う必要があります
- クラスMyListFragmentを作成し、ListFragmentに拡張します。
- * onCreateView()*メソッド内で、上記で定義したlist_fragment xmlレイアウトでビューを膨らませます。
- * onActivityCreated()*メソッド内で、string.xml内にあるString配列R.array.planetを使用してリソースからarrayadapterを作成し、このアダプターをlistviewに設定し、onItemクリックリスナーも設定します。
- * OnItemClickListener()*メソッド内で、クリックされているアイテム名とともにトーストメッセージを表示します。
package com.example.finddevguides7.myapplication;
import android.annotation.SuppressLint;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;
public class MyListFragment extends ListFragment implements OnItemClickListener {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_fragment, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.Planets, android.R.layout.simple_list_item_1);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
}
}
次のコードはMainActivity.javaのコンテンツになります
package com.example.finddevguides7.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
次のコードは、res/AndroidManifest.xmlに配置されたmanifest.xmlのコンテンツになります。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.finddevguides7.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
アプリケーションを実行する
作成した SimpleListFragment アプリケーションを実行してみましょう。 環境のセットアップ中に AVD を作成したと思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 AndroidはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-