하고재비

[Android] ListFragment 본문

Android

[Android] ListFragment

DeadDE 2018. 1. 10. 13:51

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/

Comments