畫廊幾乎完美。在我的項目之一,我確實有一個畫廊在它的LinearLayout:
<Gallery
android:id="@+id/gallery"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_width="fill_parent"
android:spacing="2dip" />
的活動實現OnItemClickListener:
public class MyActivity extends Activity implements OnItemClickListener {
的數據結構中包含的所有項目,併發送到適配器:
private void processGallery() {
adapter = new MyAdapter(this, containers, appName);
if (adapter != null) {
gallery.setAdapter(adapter);
}
}
@Override
public void onItemClick(final AdapterView<?> adapterView, final View view, final int position, final long id) {
if (containers != null) {
container = containers.get(position);
if (container != null) {
// Handle selected image
}
}
}
適配器是通常BaseAdapter - 沒有什麼魔法:
public class MyAdapter extends BaseAdapter {
private ArrayList<Container> containers;
private Context context;
public int getCount() {
return containers.size();
}
public Object getItem(final int position) {
return containers.get(position);
}
public long getItemId(final int position) {
return position;
}
public View getView(final int position, final View contentView, final ViewGroup viewGroup) {
ImageView imageView = new ImageView(context);
Container container = containers.get(position);
if (container != null) {
// Do your image thing here
}
return imageView;
}
public MyAdapter(final Context context, final ArrayList<Container> containers, final String appName) {
this.context = context;
this.containers = containers;
}
}
這個簡單的代碼給出了一個帶有可點擊項目的水平滾動圖庫。點擊被髮送到活動 - 無需在適配器中做一些特殊的事情。我從這裏顯示的代碼中刪除了我使用的DrawableCache,因爲我的項目確實來自網絡。
你能給一個完整的源代碼版本嗎? – Jaseem
您需要的所有內容都顯示在上面的代碼中。你有什麼具體的部分不清楚? –
我只是想知道如果一個API可以比Android更混亂。你需要50多行代碼和10個圖像庫導入嗎? Android API在某處出現錯誤。難怪它來自谷歌,在dart lang的背後,你需要15K +的線路才能成爲Hello World。這整件事對我來說似乎沒有好處。 – Jaseem