android 图片列表,Android 列表使用(ListView GridView Gallery图片计时滚动)

ListView

作用: 1.将数据填充到布局。 2.处理用户的选择点击等操作。

根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter

实例:listview.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:descendantFocusability="afterDescendants">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:cacheColorHint="#00000000"

android:dividerHeight="1dp"

android:divider="#FF0000"

/>item.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center_vertical"

android:orientation="horizontal">

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_marginLeft="15dp"

android:class="lazyload" src="https://img-blog.csdnimg.cn/2022010700564781686.png" data-original="@drawable/a2"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="25dp"/>

public class ActivityListView extends Activity {   private ListView mListView;   protected void onCreate(android.os.Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //加载listview控件所在的布局文件        setContentView(R.layout.listview_layout);        //获取listview控件的对象

mListView = (ListView)findViewById(R.id.listview);        /**

*定义SimpleAdapter(并加载对应item布局文件,获得对应的keY,获得对应item布局文件中的组件的id

*第二个参数data:数据源第三个参数resource:listView每个item对应的布局文件第四个参数 from:

*第五个参数to: new String[]{对应的key}v         */

SimpleAdapter simpleAdapter =new SimpleAdapter(this, get_data(),

R.layout.item_listview,new String[]{"name","img" },

newint[] { R.id.txtname, R.id.img });             //通过setAdapter将适配器绑定到listView控件              mListView.setAdapter(simpleAdapter);

};        /**

*数据源          */

String[] name = { "张三","李四","小星星","静静","明明","小翠" };         private ArrayList> get_data() {         //定义一个ArrayList的集合(集合中又封装一个map类型的集合)

ArrayList> data_list =new ArrayList>();               for (int i = 0; i

Map data_map =new HashMap();

data_map.put("name",name[i]);

data_map.put("img",R.drawable.a1);                  //将map集合添加(封装)到ArrayList集合中                 data_list.add(data_map);

}             //返回ArrayList集合

return data_list;

}

}

AAffA0nNPuCLAAAAAElFTkSuQmCC

Gallery

作用:实现图片计时滚动显示

AAffA0nNPuCLAAAAAElFTkSuQmCCgallery.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center_vertical"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:spacing="0dip"/>

public class ActivityGrelly extends Activity {   /** Called when the activity isfirst created. */

private int index;   private Gallery g;   private Handler handler;   public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);

setContentView(R.layout.gallery_layout);

initView();

}

private void initView() {      // TODOAuto-generated method stub      // 获得Gallery对象

g = (Gallery)findViewById(R.id.gallery);

// 添加ImageAdapter给Gallery对象

g.setAdapter(newImageAdapter(this));

// 设置Gallery的背景      g.setBackgroundResource(R.drawable.bg);

TimerTask task = new TimerTask() {

@Override         public void run() {

Message message = new Message();

message.what = 2;

index = g.getSelectedItemPosition();

index++;

handler.sendMessage(message);

}

};

Timer timer = new Timer();

timer.schedule(task, 3000, 3000);

handler = new Handler(){

@Override         public void handleMessage(Message msg) {            super.handleMessage(msg);            switch (msg.what) {            case 2:

g.setSelection(index);                break;

default:                break;

}

}

};

// 设置Gallery的事件监听

g.setOnItemClickListener(new OnItemClickListener() {         public void onItemClick(AdapterView> parent, View v,                int position,long id) {

Toast.makeText(ActivityGrelly.this,                   "你选择了" + (position + 1) +"号图片", Toast.LENGTH_SHORT)

.show();

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值