編輯:關於Android編程
android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" />
public class MainActivity extends AppCompatActivity { private String[] data = { "草莓", "梨", "芒果", "蘋果", "西瓜", "香蕉", "櫻桃", }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter adapter = new ArrayAdapter( MainActivity.this, android.R.layout.simple_list_item_1, data ); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); } }
定義一個Fruit類:
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 this.name; } public int getImageId() { return this.imageId; } }
新建布局文件:fruit_item.xml:
xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> android:id="@+id/fruit_image" android:layout_width="60dp" android:layout_height="60dp" /> android:id="@+id/fruit_name" android:textSize="30sp" android:gravity="center_vertical" android:layout_width="0dp" android:layout_weight="1" android:layout_marginLeft="10dp" android:layout_height="60dp" />
新建FruitAdapter類繼承ArrayAdapter
public class FruitAdapter extends ArrayAdapter { private int resourceId; public FruitAdapter(Context context, int textViewResourceId, List objects) { super(context, textViewResourceId, objects); resourceId = textViewResourceId; } @NonNull @Override public View getView(int position, View convertView, ViewGroup parent) { Fruit fruit = getItem(position); // 獲取當前項的Fruit實例 View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false); 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; } }
初始化數據,創建FruitAdapter,設置adapter:
public class MainActivity extends AppCompatActivity { private List fruitList = new ArrayList<>(); @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); } private void initFruits() { Fruit strawberry = new Fruit("草莓", R.drawable.strawberry); fruitList.add(strawberry); Fruit pear = new Fruit("梨", R.drawable.pear); fruitList.add(pear); Fruit mango = new Fruit("芒果", R.drawable.mango); fruitList.add(mango); Fruit apple = new Fruit("蘋果", R.drawable.apple); fruitList.add(apple); Fruit watermelon = new Fruit("西瓜", R.drawable.watermelon); fruitList.add(watermelon); Fruit banana = new Fruit("香蕉", R.drawable.banana); fruitList.add(banana); Fruit cherry = new Fruit("櫻桃", R.drawable.cherry); fruitList.add(cherry); } }
@Override public View getView(int position, View convertView, ViewGroup parent) { Fruit fruit = getItem(position); // 獲取當前項的Fruit實例 View view; if (convertView == null) { view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false); } else { view = convertView; } 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; }
convertView參數,用於將之前加載好的布局進行緩存,這樣不會再重復去加載布局。
新增一個內部類ViewHolder,用於對控件的實例進行緩存。就不用每次都通過findViewById()方法來獲取實例了。
最終實現代碼如下:
public class FruitAdapter extends ArrayAdapter { private int resourceId; public FruitAdapter(Context context, int textViewResourceId, List objects) { super(context, textViewResourceId, objects); resourceId = textViewResourceId; } @NonNull @Override public View getView(int position, View convertView, ViewGroup parent) { Fruit fruit = getItem(position); // 獲取當前項的Fruit實例 View view; ViewHolder viewHolder; if (convertView == null) { view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false); viewHolder = new ViewHolder(); viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image); viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name); view.setTag(viewHolder); // 將viewHolder存儲在View中 } else { view = convertView; viewHolder = (ViewHolder) view.getTag(); // 重新獲取viewHolder } viewHolder.fruitImage.setImageResource(fruit.getImageId()); viewHolder.fruitName.setText(fruit.getName()); return view; } class ViewHolder { ImageView fruitImage; TextView fruitName; } }
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterViewparent, View view, int position, long id) { Fruit fruit = fruitList.get(position); Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show(); } });
附錄文章1介紹了Android平台上的懸浮窗WindowManager,WindowManager懸浮窗可以懸浮在Android設備上的桌面窗口之上,但是WindowMa
前些天有個大神告訴我,你寫這麼多TetxtView實際上就實現了一個TextView的功能。我說我要設置background,textsize,textcolor阿,還有
本菜開源的一個自己寫的Demo,希望能給Androider們有所幫助,水平有限,見諒見諒… https://github.com/zhiaixinyang
最近忙著項目,很久沒有總結提交博客和提交github了。接下來我打算整理下項目中用到的比較有用的發表到博客上。也打算總結一些關於設計模式和源碼分析的博客。今天的話就先來講