listview两种数据适配器以及监听处理

//MainActivity

public class MainActivity extends Activity implements OnItemClickListener,

OnScrollListener{
/**
* OnItemClickListener:监听单个点击事件
* OnScrollListener:监听滚动状态变化
*/
private ListView listview;
private ArrayAdapter<String> arr_adapter;
private SimpleAdapter sim_adapter;
private List<Map<String,Object>>list;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview=(ListView) findViewById(R.id.listView1);
        
        //数据源arr
        String[]arr={"数据1","数据2","数据3"};
        /**
         * 新建一个适配器
         * this:上下文参数
         * android.R.layout.simple_list_item_1:系统自带的列表样式
         * arr:数据源
         */
        arr_adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr);
        //加载数据适配器
        //listview.setAdapter(arr_adapter);
        
        //准备好数据源list
        getData();
        /**
         * 新建一个数据适配器
         * context(this):上下文参数
         * data(list):数据源,由Map组成的集合
         * 每个Map对应listview的一个列表项
         * resource(R.layout.item):列表项样式ID,这里为自定义样式
         * from(new String[]{"price","name","pic"}):Map中键值
         * to(new int[]{R.id.price,R.id.name,R.id.imgview}):数据视图对应的Id,与from对应
         */
      
        sim_adapter=new SimpleAdapter(this, list,R.layout.item , new String[]{"price","name","pic"},
        new int[]{R.id.price,R.id.name,R.id.imgview});
        
        //加载数据适配器
        listview.setAdapter(sim_adapter);
        
        //加载监听器
        listview.setOnItemClickListener(this);
        listview.setOnScrollListener(this);
    }
        
       protected  void getData(){
      list=new ArrayList<Map<String,Object>>();
     
      for(int i=0;i<10;i++){
      Map<String,Object> map=new HashMap<String,Object>();
          map.put("price", "$5"+i);
          map.put("name", "t恤"+i);
          map.put("pic", R.drawable.ic_launcher);
          list.add(map);
      
      }
      /*
      Map<String,Object> map=new HashMap<String,Object>();
      map.put("price", "$50");
      map.put("name", "t恤1");
      map.put("pic", R.drawable.ic_launcher);
      
      Map<String,Object> map2=new HashMap<String,Object>();
      map2.put("price", "$40");
      map2.put("name", "t恤2");
      map2.put("pic", R.drawable.ic_launcher);
      
      Map<String,Object> map3=new HashMap<String,Object>();
      map3.put("price", "$50.5");
      map3.put("name", "t恤3");
      map3.put("pic", R.drawable.ic_launcher);
      
      Map<String,Object> map4=new HashMap<String,Object>();
      map4.put("price", "$600");
      map4.put("name", "t恤4");
      map4.put("pic", R.drawable.ic_launcher);
      //list中加入map
      list.add(map);
      list.add(map2);
      list.add(map3);
      list.add(map4);*/
     // return list;
       
    }


@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub

}


@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
// 手指离开屏幕前,用力滑了一下
if (scrollState == SCROLL_STATE_FLING) {

} else
// 停止滚动
if (scrollState == SCROLL_STATE_IDLE) {
Toast.makeText(MainActivity.this, "用力滑一下",0).show();
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "t恤7 ");
map.put("price", "$56 ");
map.put("pic", R.drawable.ic_launcher);
list.add(map);
listview.setAdapter(sim_adapter);
sim_adapter.notifyDataSetChanged();


} else
// 正在滚动
if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {


}

}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
/**
* 点击事件处理
* int:点击位置,从0开始

*/
String str=listview.getItemAtPosition(arg2)+"";
Toast.makeText(this, "点击位置"+arg2+str, Toast.LENGTH_SHORT).show();
}

}


//item.xml 自定义列表项样式

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <ImageView 
        android:id="@+id/imgview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        
        />
    <LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_alignBottom="@+id/imgview"
    android:layout_toRightOf="@+id/imgview"
        >
        <TextView 
            android:id="@+id/name"
            android:layout_width="wrap_content"
    android:layout_height="wrap_content"
            android:text="商品名"
            />
        <TextView 
            android:id="@+id/price"
            android:layout_width="wrap_content"
    android:layout_height="wrap_content"
            android:text="价格"
            />
    </LinearLayout>
</RelativeLayout>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值