1. 程式人生 > >Android studio 定製 ListView 介面

Android studio 定製 ListView 介面

這篇文章是配套Android 第一行程式碼 第二版使用
3.5.2 定製listView的介面定製。第一要用到上一節中已經建立好的工程,activity_main.xml 中的內容不改變,至於MainActivity中的變化在稍後會說明。
建立一個實體類,在com.example.list 新建Java Class name填寫Fruit 其他不用理會然後點選OK,在新檔案中填入
public class Fruit {
private String name;

private  int imageId;

public Fruit (String name ,int imageId){
    this.name = name;
    this.imageId = imageId;
}

public String getName() {
    return name;
}

public int getImageId() {
    return imageId;
}

}

注意:這裡面都是public 類
下面新建fruit_item.xml 在layout中新建XML檔案填入

<ImageView
android:id="@+id/fruit_image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
    android:id="@+id/fruit_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="10dip"/>

兩個控制元件
然後新建一個是個介面卡,新建介面卡在com.example.list 新建Activity 選擇Empty Activity 並把Generate 不勾選 程式碼書上也有我就複製一下
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;

public class FruitAdapter extends ArrayAdapter {

private int resourceId;

public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {
    super(context, textViewResourceId, objects);
    resourceId = textViewResourceId;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Fruit fruit = getItem(position);
    View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
    ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
    TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
    fruitImage.setImageResource(fruit.getImageId());
    fruitName.setText(fruit.getName());
    return view;
}

}

最後就是改一下主活動了,主活動行程式碼
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private List<Fruit> fruitList = new ArrayList<Fruit>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initFruits();
    FruitAdapter adapter = new FruitAdapter(MainActivity.this,
            R.layout.fruit_item, fruitList);
    ListView listView = (ListView) findViewById(R.id.list_view);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Fruit fruit = fruitList.get(position);
            Toast.makeText(MainActivity.this, fruit.getName(),
                    Toast.LENGTH_SHORT).show();
        }
    });
}

private void initFruits() {
    Fruit apple = new Fruit("Apple", R.drawable.apple_pic);
    fruitList.add(apple);
    Fruit banana = new Fruit("Banana", R.drawable.banana_pic);
    fruitList.add(banana);
    Fruit orange = new Fruit("Orange", R.drawable.orange_pic);
    fruitList.add(orange);
    Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon_pic);
    fruitList.add(watermelon);
    Fruit pear = new Fruit("Pear", R.drawable.pear_pic);
    fruitList.add(pear);
    Fruit grape = new Fruit("Grape", R.drawable.grape_pic);
    fruitList.add(grape);
    Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple_pic);
    fruitList.add(pineapple);
    Fruit strawberry = new Fruit("Strawberry", R.drawable.strawberry_pic);
    fruitList.add(strawberry);
    Fruit cherry = new Fruit("Cherry", R.drawable.cherry_pic);
    fruitList.add(cherry);
    Fruit mango = new Fruit("Mango", R.drawable.mango_pic);
    fruitList.add(mango);
}

}
貼上這兩打段程式碼主要是想讓大家參考一下標頭檔案,很多時候的報錯就是因為標頭檔案沒有新增完整。每個函式的功能我就不贅述了,先要《第一程式碼 Android 》第二版 郭霖著 的留下郵箱免費發給你們,就不收什麼積分了。