Android ListView Adaptive 구현 테이블

11171 단어 ListView테이블
ListView를 사용하여 사용자 적응 테이블을 구현하는 방법에 대해 설명합니다.GridView는ListView보다 스스로 적응하는 표를 쉽게 만들 수 있지만, GridView는 칸마다 크기가 고정되어 있고, ListView가 실현하는 표는 칸마다 크기를 사용자 정의할 수 있지만, 따라서 스스로 적응하는 표를 실현하는 것도 복잡할 수 있다. (칸마다 크기가 다르다.)또한 GridView가 구현한 표는 구체적인 격자에 위치할 수 있지만 ListView가 구현한 표는 표 줄에만 위치할 수 있다.그래서 구체적인 사용 환경에 따라 GridView나ListView 구현 표를 선택한다는 옛말이다.본고에서 실현한ListView표는 칸마다 크기가 다르고 텍스트(TextView)나 그림(ImageView)이 칸을 만드는 데이터로 XML 구현 양식(자체 적응의 근본 목표)을 미리 정의할 필요가 없다.ListView는 HorizontalScrollView에 설치되어 있기 때문에 열이 비교적 많거나 열이 비교적 긴 데이터 테이블에도 그 폭에 잘 적응할 수 있다.main.xml 소스는 다음과 같습니다.
<?xml version="1.0" encoding="utf-8"?>  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

    android:orientation="vertical" android:layout_width="fill_parent"  

    android:layout_height="fill_parent">  

    <HorizontalScrollView android:id="@+id/HorizontalScrollView01"  

        android:layout_height="fill_parent" android:layout_width="fill_parent">  

        <ListView android:id="@+id/ListView01" android:layout_height="wrap_content"  

            android:layout_width="wrap_content"></ListView>  

    </HorizontalScrollView>  

</LinearLayout> 
  testMyListView.java     : package com.testMyListView; 

import java.util.ArrayList; 

import com.testMyListView.TableAdapter.TableCell; 

import com.testMyListView.TableAdapter.TableRow; 

import android.app.Activity; 

import android.os.Bundle; 

import android.view.View; 

import android.widget.AdapterView; 

import android.widget.ListView; 

import android.widget.LinearLayout.LayoutParams; 

import android.widget.Toast; 

/** 

* @author hellogv 

*/ 

public class testMyListView extends Activity { 

        /** Called when the activity is first created. */ 

        ListView lv; 

        @Override 

        public void onCreate(Bundle savedInstanceState) { 

                super.onCreate(savedInstanceState); 

                setContentView(R.layout.main); 

                this.setTitle("ListView       ---hellogv"); 

                lv = (ListView) this.findViewById(R.id.ListView01); 

                ArrayList 

table = new ArrayList 

(); 

                TableCell[] titles = new TableCell[5];//   5    

                int width = this.getWindowManager().getDefaultDisplay().getWidth()/titles.length; 

                //      

                for (int i = 0; i < titles.length; i++) { 

                        titles[i] = new TableCell("  " + String.valueOf(i), 

                                                                        width + 8 * i, 

                                                                        LayoutParams.FILL_PARENT, 

                                                                        TableCell.STRING); 

                } 

                table.add(new TableRow(titles)); 

                //       

                TableCell[] cells = new TableCell[5];//   5    

                for (int i = 0; i < cells.length - 1; i++) { 

                        cells[i] = new TableCell("No." + String.valueOf(i), 

                                                                        titles[i].width, 

                                                                        LayoutParams.FILL_PARENT, 

                                                                        TableCell.STRING); 

                } 

                cells[cells.length - 1] = new TableCell(R.drawable.icon, 

                                                                                                titles[cells.length - 1].width, 

                                                                                                LayoutParams.WRAP_CONTENT, 

                                                                                                TableCell.IMAGE); 

                //            

                for (int i = 0; i < 12; i++) 

                        table.add(new TableRow(cells)); 

                TableAdapter tableAdapter = new TableAdapter(this, table); 

                lv.setAdapter(tableAdapter); 

                lv.setOnItemClickListener(new ItemClickEvent()); 

        } 

        class ItemClickEvent implements AdapterView.OnItemClickListener { 

                @Override 

                public void onItemClick(AdapterView arg0, View arg1, int arg2, 

                                long arg3) { 

                        Toast.makeText(testMyListView.this, "   "+String.valueOf(arg2)+" ", 500).show(); 

                } 

        } 

} 
ListView     Table  TableAdapter.java    :PS:TableCell      ,TableRow      ,TableRowView         。    :TableCell --> TableRow(TableRowView)-->ListView package com.testMyListView; 

import java.util.List; 

import android.content.Context; 

import android.graphics.Color; 

import android.view.Gravity; 

import android.view.View; 

import android.view.ViewGroup; 

import android.widget.BaseAdapter; 

import android.widget.ImageView; 

import android.widget.LinearLayout; 

import android.widget.TextView; 

public class TableAdapter extends BaseAdapter { 

        private Context context; 

        private List 

table; 

        public TableAdapter(Context context, List 

table) { 

                this.context = context; 

                this.table = table; 

        } 

        @Override 

        public int getCount() { 

                return table.size(); 

        } 

        @Override 

        public long getItemId(int position) { 

                return position; 

        } 

        public TableRow getItem(int position) { 

                return table.get(position); 

        } 

        public View getView(int position, View convertView, ViewGroup parent) { 

                TableRow tableRow = table.get(position); 

                return new TableRowView(this.context, tableRow); 

        } 

        /** 

         * TableRowView          

         * @author hellogv 

         */ 

        class TableRowView extends LinearLayout { 

                public TableRowView(Context context, TableRow tableRow) { 

                        super(context); 

                        

                        this.setOrientation(LinearLayout.HORIZONTAL); 

                        for (int i = 0; i < tableRow.getSize(); i++) {//          

                                TableCell tableCell = tableRow.getCellValue(i); 

                                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( 

                                                tableCell.width, tableCell.height);//               

                                layoutParams.setMargins(0, 0, 1, 1);//         

                                if (tableCell.type == TableCell.STRING) {//           

                                        TextView textCell = new TextView(context); 

                                        textCell.setLines(1); 

                                        textCell.setGravity(Gravity.CENTER); 

                                        textCell.setBackgroundColor(Color.BLACK);//     

                                        textCell.setText(String.valueOf(tableCell.value)); 

                                        addView(textCell, layoutParams); 

                                } else if (tableCell.type == TableCell.IMAGE) {//           

                                        ImageView imgCell = new ImageView(context); 

                                        imgCell.setBackgroundColor(Color.BLACK);//     

                                        imgCell.setImageResource((Integer) tableCell.value); 

                                        addView(imgCell, layoutParams); 

                                } 

                        } 

                        this.setBackgroundColor(Color.WHITE);//    ,          

                } 

        } 

        /** 

         * TableRow        

         * @author hellogv 

         */ 

        static public class TableRow { 

                private TableCell[] cell; 

                public TableRow(TableCell[] cell) { 

                        this.cell = cell; 

                } 

                public int getSize() { 

                        return cell.length; 

                } 

                public TableCell getCellValue(int index) { 

                        if (index >= cell.length) 

                                return null; 

                        return cell[index]; 

                } 

        } 

        /** 

         * TableCell          

         * @author hellogv 

         */ 

        static public class TableCell { 

                static public final int STRING = 0; 

                static public final int IMAGE = 1; 

                public Object value; 

                public int width; 

                public int height; 

                private int type; 

                public TableCell(Object value, int width, int height, int type) { 

                        this.value = value; 

                        this.width = width; 

                        this.height = height; 

                        this.type = type; 

                } 

        } 

}

 

효과도를 정상적으로 볼 수 없으면 그림을 클릭하세요.

좋은 웹페이지 즐겨찾기