幾乎都看的到列表的存在
這就來一個簡單地舉例
1 . 一定有一大筆整理好的資料
2 . 再來選擇我們要置放的大容器內
3 . 容器內每個抽屜內要如何擺放
public class MainActivity extends Activity {
    private ListView listView;
    private List<String> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //我的容器
        listView = (ListView) findViewById(R.id.listView1);
        list = new ArrayList<String>();
        
        //所有整理好資料
        for(int i=0; i<10; i++){
            list.add("王 "+i);
        }
        
        //將容器調適為我們可用格式
        Adapter adapter = new Adapter(this);
        listView.setAdapter(adapter);
        
    }
    private class Adapter extends BaseAdapter{
        private LayoutInflater mInflater = null;
        private Adapter(Context context) {  
            this.mInflater = LayoutInflater.from(context);  
        }
        public void updateResults("如果有獨立Class,請添加來源") {
            notifyDataSetChanged(); //更新列表用
        }
        
        @Override
        public int getCount() {
            return list.size();
        }
        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Elements elements = null;  
            
            if (convertView == null) {  
                elements = new Elements();  
                //我抽屜裡的設計圖
                convertView = mInflater.inflate(R.layout.listview_con, null);  
                //裡面有XX元件( 如果有而外需求,必須在 Elements 內添加才可以使用 )
                elements.str1 = (TextView) convertView.findViewById(R.id.textView1);   
                convertView.setTag(elements);  
            } else {  
                elements = (Elements) convertView.getTag();  
            }  
            
            //將資料依序放入我擺放的位置
            elements.str1.setText((String) list.get(position).toString());  
  
            return convertView; 
        }
    }
    //每格要擺放的元件( 如有而外需求可再添加 )
    private class Elements{
        TextView str1;
    }
}
容器.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>
</LinearLayout>
抽屜.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" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp" />
</LinearLayout>
成果圖 :

注意 :
listView.setOnItemClickListener(this);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/mainBackgroundColor"
    android:descendantFocusability="blocksDescendants">
    
    .
    .
    .
</LinearLayout>
參考資訊 : ListView裡的OnItemClickListener失去作用(失效)
歡迎轉載,請註明出處。
 
沒有留言:
張貼留言