하고재비
[Android] ListFragment 본문
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import android.support.v4.app.ListFragment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; public class MainActivity extends AppCompatActivity { static final String[] LIST_MENU = {"LIST1", "LIST2", "LIST3"} ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, LIST_MENU); ListFragment listFragment = (ListFragment)getSupportFragmentManager().findFragmentById(R.id.menulistfragment); listFragment.setListAdapter(adapter) ; } } | cs |
MenuListFragment.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.example.user.myapplication_1; import android.support.v4.app.ListFragment; import android.view.View; import android.widget.ListView; public class MenuListFragment extends ListFragment { @Override public void onListItemClick(ListView l, View v, int position, long id) { String strText = (String) l.getItemAtPosition(position); } } | cs |
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.myapplication_1.MainActivity">
<fragment
android:id="@+id/menulistfragment"
android:name="com.example.user.myapplication_1.MenuListFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
출처 :: http://recipes4dev.tistory.com/
'Android' 카테고리의 다른 글
| [Android]탭레이아웃 Tab Layout (0) | 2018.02.11 |
|---|---|
| [Android] 클래스 이름 불러와 인텐트로 바로써먹어보자 (0) | 2018.01.22 |
| [Android] 현재 실행중인 액티비티 가져오기 (0) | 2018.01.19 |
| [Android] Recycler View, StaggeredGrid (0) | 2018.01.17 |
| [Android] android.content.res.Resources$NotFoundException: String resource ID #0x1 (0) | 2018.01.03 |
Comments