<Android>列表、網格、畫廊視圖及適配器的綁定

來源:互聯網
上載者:User

標籤:

列表視圖和適配器的綁定

列表視圖既可以使用ListView組件,也可以繼承ListActivity。顯示可以是ArrayAdapter,也可以是遊標SimpleCursorAdapter,還可以是繼承BaseAdapter展示其它視圖。

Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);//獲得通訊錄連絡人遊標對象CursorstartManagingCursor(c);//執行個體化列表適配器ListAdapter adapter = new SimpleCursorAdapter(this,        android.R.layout.simple_list_item_1,        c,        new String[] {People.NAME} ,        new int[] {android.R.id.text1});setListAdapter(adapter);

 

14.網格視圖(GridView)

網格視圖配合BaseAdapter樣本,圖片縮圖網格顯示

public class MainActivity extends Activity {   private GridView gv;      @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        gv = (GridView)findViewById(R.id.GridView01);        gv.setNumColumns(4);        // gv.setNumColumns(3);        // String[] strs = {"a","a1","a2","b","b1","b2","c","c1","c2"};        // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_gallery_item,strs);        gv.setAdapter(new MyAdapter(this));    }    class MyAdapter extends BaseAdapter{       private Integer[] imgs = {                     R.drawable.gallery_photo_1,                     R.drawable.gallery_photo_2,                     R.drawable.gallery_photo_3,                     R.drawable.gallery_photo_4,                     R.drawable.gallery_photo_5,                     R.drawable.gallery_photo_6,                     R.drawable.gallery_photo_7,                     R.drawable.gallery_photo_8,                                         R.drawable.gallery_photo_1,                     R.drawable.gallery_photo_2,                     R.drawable.gallery_photo_3,                     R.drawable.gallery_photo_4,                     R.drawable.gallery_photo_5,                     R.drawable.gallery_photo_6,                     R.drawable.gallery_photo_7,                     R.drawable.gallery_photo_8       };       Context context;       MyAdapter(Context context){              this.context = context;       }          public int getCount() {                 return imgs.length;          }          public Object getItem(int item) {                 return item;          }          public long getItemId(int id) {                 return id;          }          public View getView(int position, View convertView, ViewGroup parent) {                  ImageView imageView;               if (convertView == null) {                   imageView = new ImageView(context);                   imageView.setLayoutParams(new GridView.LayoutParams(45, 45));                   imageView.setAdjustViewBounds(false);                   imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);                   imageView.setPadding(8, 8, 8, 8);               } else {                   imageView = (ImageView) convertView;               }               imageView.setImageResource(imgs[position]);               return imageView;          }    }}

 

15.畫廊視圖

畫廊視圖和BaseAdapter配合,底部顯示圖片縮圖上面顯示放大圖片

public class MainActivity extends Activity implements OnItemSelectedListener,          ViewFactory {   private ImageSwitcher mSwitcher;   private Integer[] mThumbIds = { R.drawable.sample_thumb_0,                 R.drawable.sample_thumb_1, R.drawable.sample_thumb_2,                 R.drawable.sample_thumb_3, R.drawable.sample_thumb_4,                 R.drawable.sample_thumb_5, R.drawable.sample_thumb_6,                 R.drawable.sample_thumb_7 };   private Integer[] mImageIds = { R.drawable.sample_0, R.drawable.sample_1,                 R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,                 R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 };   @Override   public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          requestWindowFeature(Window.FEATURE_NO_TITLE);          setContentView(R.layout.main);          mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);          mSwitcher.setFactory(this);          mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,                        android.R.anim.fade_in));          mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,                        android.R.anim.fade_out));          Gallery g = (Gallery) findViewById(R.id.gallery);          g.setAdapter(new ImageAdapter(this));          g.setOnItemSelectedListener(this);   }   public class ImageAdapter extends BaseAdapter {          public ImageAdapter(Context c) {                 mContext = c;          }          public int getCount() {                 return mThumbIds.length;          }          public Object getItem(int position) {                 return position;          }          public long getItemId(int position) {                 return position;          }          public View getView(int position, View convertView, ViewGroup parent) {                 ImageView i = new ImageView(mContext);                 i.setImageResource(mThumbIds[position]);                 i.setAdjustViewBounds(true);                 i.setLayoutParams(new Gallery.LayoutParams(                               LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));                 i.setBackgroundResource(R.drawable.picture_frame);                 return i;          }          private Context mContext;   }   @Override   public void onItemSelected(AdapterView<?> adapter, View v, int position,                 long id) {          mSwitcher.setImageResource(mImageIds[position]);   }   @Override   public void onNothingSelected(AdapterView<?> arg0) {   }   @Override   public View makeView() {          ImageView i = new ImageView(this);          i.setBackgroundColor(0xFF000000);          i.setScaleType(ImageView.ScaleType.FIT_CENTER);          i.setLayoutParams(new ImageSwitcher.LayoutParams(                        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));          return i;   }}

 

<Android>列表、網格、畫廊視圖及適配器的綁定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.