2014/08/07

Android 利用LayoutInflater建立ListView

當助教寫筆記

ListView Layout XML:
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.exlistview04.MainActivity" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>



MyLayout XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textViewName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textViewEngName"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />

    <TextView
        android:id="@+id/textViewEngName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/imageView"
        android:text="TextView" />

</RelativeLayout>




Code:
package com.cy.exlistview04;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
 private int ID[] = { R.drawable.baseball, R.drawable.basketball,
   R.drawable.football, R.drawable.other };
 private String[] NAME = { "棒球", "籃球", "足球", "其他" };
 private String[] ENG_NAME = { "Baseball", "Basketball", "Football", "Other" };
 private ListView listView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  listView = (ListView) findViewById(R.id.listView);
  MyAdapter adapter = new MyAdapter(this);
  listView.setAdapter(adapter);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }

 class MyAdapter extends BaseAdapter {
  private LayoutInflater inflater;

  protected MyAdapter(Context context) {
   this.inflater = LayoutInflater.from(context);
  }

  @Override
  public int getCount() {
   return ID.length;
  }

  @Override
  public Object getItem(int arg0) {
   // TODO Auto-generated method stub
   return arg0;
  }

  @Override
  public long getItemId(int arg0) {
   return arg0;
  }

  @Override
  public View getView(int index, View view, ViewGroup viewGroup) {
   view = inflater.inflate(R.layout.mylayout, null);
   ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
   TextView textViewName = (TextView) view
     .findViewById(R.id.textViewName);
   TextView textViewEngName = (TextView) view
     .findViewById(R.id.textViewEngName);
   imageView.setImageResource(ID[index]);
   textViewName.setText(NAME[index]);
   textViewEngName.setText(ENG_NAME[index]);
   return view;
  }
 }
}

執行結果:





關於LayoutInflater的觀念之後補上

沒有留言:

張貼留言