Study/Android

Simple ListView

Kostrian 2015. 1. 29. 10:32

이벤트 처리 외의 자바를 사용하지 않고 xml만 가지고 어플을 만든다는건 php나 js를 사용하지 않고 웹페이지를 만든다는거와 비슷하다.


리스트를 구현할때 자바의 도움을 받지 않고서는 제한된 리스트만을 만들수밖에 없다. 버튼이나 이미지 버튼을 제한된 수를 계속해서 넣는방법밖에는 없을텐데 이를 위해 ListView를 사용한다.


먼저 xml에서 <ListView>를 만든다.


 <ListView

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:id="@+id/listview">

 </ListView>


다음 자바에서 다음과 같은 코드를 작성한다.


private ListView                m_ListView;

private ArrayAdapter<String>    m_Adapter;


protected void onCreate(Bundle savedIntanceState) {

...

m_Adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1);

          m_ListView = (ListView) findViewById(R.id.listview);

          m_ListView.setAdapter(m_Adapter);

          m_ListView.setOnItemClickListener(new OnItemClickListener() {

    @Override

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Toast.makeText(Main.this,m_Adapter.getItem(position),Toast.LENGTH_SHORT);

    }

};);

          for (int i = 0; i < 20; ++i) {

              m_Adapter.add("Item "+(i+1));

          }

...

}


또한 GridLayout을 통해 하단 메뉴바를 만드는법은


<RelativeLayout 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">


        <RelativeLayout

            android:id="@+id/menu_bottom"

            android:layout_width="match_parent"

            android:layout_height="60dp"

            android:layout_alignParentBottom="true"

            android:background="@drawable/menu_bottom"

            >

            <GridLayout

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:rowCount="1"

                android:columnCount="5">


                <ImageButton

                    android:layout_height="match_parent"

                    android:layout_width="wrap_content"

                    android:src="@drawable/tab1"

                    />

                <ImageButton

                    android:layout_height="match_parent"

                    android:layout_width="wrap_content"

                    android:src="@drawable/tab2"

                    />

                <ImageButton

                    android:layout_height="match_parent"

                    android:layout_width="wrap_content"

                    android:src="@drawable/tab3"

                    />

                <ImageButton

                    android:layout_height="match_parent"

                    android:layout_width="wrap_content"

                    android:src="@drawable/tab4"

                    />

                <ImageButton

                    android:layout_height="match_parent"

                    android:layout_width="wrap_content"

                    android:src="@drawable/tab5"

                    />


            </GridLayout>

        </RelativeLayout>

        <TableLayout

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:layout_above="@id/menu_bottom"

            android:stretchColumns="1">


        </TableLayout>

 

    </RelativeLayout>


를 통해 만든다. RelativeLayout을 통해 하단으로 보내고 GridLayout을 이용해 5등분을 하도록 하였다.

'Study > Android' 카테고리의 다른 글

MYSQL에서 안드로이드로 데이터 가져오기  (0) 2015.01.29
CheckBox & 갤러리 사진 선택  (0) 2015.01.29
Intent & SQLite  (0) 2015.01.29
Custom ListView  (0) 2015.01.29
onCreate & Event  (0) 2015.01.29