view-ListView学习

LiastView网上有很多,推荐如下:

1、android ListView详解

http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html

2、Android中ListView的性能问题

http://android.tgbus.com/Android/tutorial/201105/354459.shtml

3、设置背景色为透明,防止滑动时,背景变黑

android:cacheColorHint="#00000000"

4、设置ListView控件条目被按下时背景颜色在文字背后,设置成True时背景色会覆盖文字

android:drawSelectorOnTop="false" 

5、android:divider属性:本属性设置listView中的选项分隔符,可设置图片,颜色等。

android:divider="#00000000" 设置为无色,这样显示时,就没有分隔线了。

6、android:minHeight="?android:attr/listPreferredItemHeight" 设置最小高度由item决定

7、Listview更新:

    在BaseAdapter的getView()方法中根据条件改变View的字体颜色、其中的图片等,是不能立即在UI上显示的,必须调用notifyDataSetChanged()方法,实现更新。

    ListView不能局部更新(现在为止我没找到实现局部更新的方法,如有同学有方法实现局部更新,盼赐教。),更新实现如下:

    BaseAdapter adapter = new BaseAdapter();

    adapter.notifyDataSetChanged();

8、更改ListView中选中框的TextView的字体颜色:

    要改变TextView的颜色,需调用TextView的setTextColor()方法设置,在使用中发现,在BaseAdapter中的getView()中使用“name.setTextColor(R.color.background);”方式字体会是黑色,可用”name.setTextColor(Color.rgb(0, 234, 255));“方式改变字体颜色,“Color.rgb(0, 234, 255)”为要设置的颜色的RGB值;最后在OnItemSelectedListener的onItemSelected()方法中调用”adapter.notifyDataSetChanged();“方法即可。

    虽然这种方式可以改变字体颜色,但不知道为什么?盼有知道的同学解惑。

9、可扩展的ListView:ExpandableListView

    ExpandableListView为ListView子类,

10、代码:

    banner中的ListView定义

<ListView
            android:id="@+id/bannerList"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:listSelector="@drawable/banneritemfocus"
            android:cacheColorHint="#00000000"
            android:drawSelectorOnTop="false"
            android:divider="#00000000"
			android:minHeight="?android:attr/listPreferredItemHeight"
            android:focusable="true"
             />

ListItem定义

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:paddingLeft="10dip"
    android:paddingRight="8dip"
    android:paddingTop="1dip"
    android:paddingBottom="1dip"
     >

    <ImageView
        android:id="@+id/bannerPlay"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/banner_play"
        />

    <TextView
        android:id="@+id/bannertext"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:singleLine="true"
        android:textSize="14dip"
        android:gravity="center_vertical"
        android:textColor="@color/white"
        android:paddingTop="1dip"
        android:paddingBottom="1dip"
        />

    <LinearLayout
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:gravity="right"
        >
        <ImageView
        android:id="@+id/bannerok"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="right"
        android:visibility="invisible"
        android:src="@drawable/banner_ok" />
        
    </LinearLayout>


</LinearLayout>

java代码:
private void showBannerListView() {

		if(bannerPop == null) {
			View v = getLayoutInflater().inflate(R.layout.banner, null);//加载包含ListView的定义
			bannerPop = new PopupWindow(v);//新建PopupWiondow对象
			bannerPop.setFocusable(true);//可获得焦点
			bannerPop.setOutsideTouchable(true);//可点击PopupWindow区域外事PopupWindow消失,会调用popupWIndow的dismiss()方法
			bannerPop.setBackgroundDrawable(new BitmapDrawable());//使PopuPWIndow响应退出按键
			
			bannerList = (ListView)v.findViewById(R.id.bannerList);
			bannerAdapter = new BaseAdapter() {
				@Override
				public View getView(int position, View convertView, ViewGroup parent) {
					ImageView bannerPlay = null;
					TextView name = null;
					if(convertView == null) {//优化ListView性能
						convertView = getLayoutInflater().inflate(R.layout.banner_item, null);
						bannerPlay = (ImageView)convertView.findViewById(R.id.bannerPlay);
						name = (TextView)convertView.findViewById(R.id.bannertext);
						
						convertView.setTag(R.id.bannerPlay, bannerPlay);
						convertView.setTag(R.id.bannertext, name);
					}else {
						bannerPlay = (ImageView)convertView.getTag(R.id.bannerPlay);
						name = (TextView)convertView.getTag(R.id.bannertext);
					}
					
					bannerPlay.setVisibility(View.GONE);//设置不可见
					name.setText(bannerData.get(position));//设置文字内容
					
					int select = bannerList.getSelectedItemPosition();
					if(position == curBannerIndex) {
						bannerPlay.setVisibility(View.VISIBLE);
						name.setTextColor(Color.rgb(0, 234, 255));//设置字体颜色 					}else {
						name.setTextColor(Color.rgb(255, 255, 255));
					}
					
					return convertView;
				}
				
				@Override
				public long getItemId(int position) {
					return position;
				}
				
				@Override
				public Object getItem(int position) {
					
					return position;
				}
				
				@Override
				public int getCount() {
					return bannerData.size();
				}
			}; 
			bannerList.setAdapter(bannerAdapter);
			bannerList.setOnItemSelectedListener(new OnItemSelectedListener() {
				@Override
				public void onItemSelected(AdapterView<?> parent, View view,
						int position, long id) {
					Log.i(TAG, "00000in onItemSelected:position="+position+" id="+id+" view="+view+" parent="+parent);
					canceBannerDelayHide();
					hideBannerDelay();
				}

				@Override
				public void onNothingSelected(AdapterView<?> parent) {
					Log.i(TAG, "11111111in onNothingSelected: parent="+parent);
				}
			});
			bannerList.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					canceBannerDelayHide();
					String name = bannerData.get(position);
					int index = name.indexOf(tvgap);//-1 为电影
					if(index == -1) {//电影,
						childPositionIndex = 0;
					}else {//电视剧,获得电视剧集数的childPositionIndex索引
						String aa = name.substring(index + tvgap.length());
						childPositionIndex = Integer.parseInt(aa) - 1;
						name = name.substring(0, index);
					}
					
					String item[] = null;
					for (int i = 0; i < favoriteData.size(); i++) {
						item = favoriteData.get(i);
						if(item[0].startsWith(name)){//可直接比较名称,获得groupPositionIndex 索引
							groupPositionIndex = i;
							break;
						}
					}
					hideBanner();
					changeMove();
				}
			});
			bannerList.setOnTouchListener(new OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					if(event.getAction() == MotionEvent.ACTION_DOWN) {
						canceBannerDelayHide();
					}else if(event.getAction() == MotionEvent.ACTION_UP) {
						hideBannerDelay();
					}
					return false;
				}
			});
			
		}else {
			bannerAdapter.notifyDataSetChanged();
		}
		bannerList.setSelection(curBannerIndex);		
		bannerPop.showAtLocation(vv, Gravity.TOP, controlx, controly);
		bannerPop.update(controlx, controly, controlWidth, controlHeight);
		hideBannerDelay();
	}

转载于:https://my.oschina.net/xwy/blog/38034

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值