[置顶] ListView 中的TextView实现跑马灯效果

案例:怎么样在一个ListView中含有TextView的item中实现字母滚动呢。这个在一些特定的场合经常用得到。如下图,当焦点位于某个item的时候其内容就自动滚动显示

[置顶] ListView 中的TextView实现跑马灯效果_第1张图片要实现这样的效果,废话不多说直接上代码:

adapter对应的layout

对应的listView的layout

切记千万不要在TextView中加上android:focusable="true"

<LinearLayout android:orientation="vertical"
	android:layout_width="fill_parent" android:layout_height="wrap_content"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<RelativeLayout android:gravity="center_vertical"
		android:orientation="horizontal" android:layout_width="fill_parent"
		android:layout_height="32.0dip">
		<TextView android:textSize="16dip" android:gravity="center"
			android:id="@+id/list_live_content_time" android:layout_width="56dip"
			android:layout_height="fill_parent" />
		<TextView android:textSize="16dip" android:ellipsize="marquee"
			android:id="@+id/list_live_content_programm" android:layout_width="260dip"
			android:layout_height="fill_parent" android:singleLine="true"
			android:layout_toRightOf="@id/list_live_content_time" />
	</RelativeLayout>
</LinearLayout>


 

man.xml

如下红色字体 一定要加上 android:focusable="true"

<!--如下红色字体 一定要加上 android:focusable="true" -->

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello" />
	<ListView android:id="@+id/list" android:layout_height="wrap_content"
		android:layout_width="fill_parent"   android:focusable="true"
		android:background="@android:color/transparent"></ListView>
</LinearLayout>



主类代码:ListViewItemActivity。java

package com.jzh.testitem;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ListViewItemActivity extends Activity {
	/** Called when the activity is first created. */
	private ListItemAdapter adapter;
	private List<Channel> list;
	private ListView channelList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		channelList = (ListView) findViewById(R.id.list);
		list = new ArrayList<Channel>();
		list.add(new Channel(
				"9:12",
				"那些流逝的时光我想和电影里一样对你说--我爱你,我爱你,我爱你..光影世界的我爱你。一次一次,我不厌其烦地切回那个镜头,找到你最深情的一刻,复制这句不能再普通不能再庸俗的话语。而你,屡屡感动我在冰冷的屏幕前。"));
		list.add(new Channel("10:35", "A不喜欢吃鸡蛋,每次发了鸡蛋都给B吃。"));
		list.add(new Channel("12:55",
				"A不喜欢吃鸡蛋,每次发了鸡蛋都给B吃。刚开始B很感谢,久而久之便习惯了。习惯了,便理所当然了"));
		list.add(new Channel(
				"14:30",
				"于是,直到有一天,A将鸡蛋给了C,B就不爽了。她忘记了这个鸡蛋本来就是A的,A想给谁都可以。为此,她们大吵一架,从此绝交。其实,不是别人不好了,而是我们的要求变多了。习惯了得到,便忘记了感恩。"));
		adapter = new ListItemAdapter(this.getApplicationContext(), list);
		channelList.setAdapter(adapter);
		channelList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				adapter.setIndex(arg2);
				adapter.notifyDataSetChanged();

			}
		});
	}
} 


adapter类代码: ListItemAdapter.java

注意下面加注释的两行代码一定要设置你的textView的setSelected(true);

package com.jzh.testitem;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListItemAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<Channel> items;
	private int index = 0;

	public ListItemAdapter(Context context, List<Channel> items) {
		super();
		this.inflater = LayoutInflater.from(context);
		this.items = items;
	}

	public void setIndex(int selected) {
		index = selected;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.live_content_adapter, null);
			holder = new ViewHolder();
			holder.time_tv = (TextView) convertView
					.findViewById(R.id.list_live_content_time);
			holder.content_tv = (TextView) convertView
					.findViewById(R.id.list_live_content_programm);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (index == position) {
		
			convertView.setBackgroundColor(Color.GREEN);
                             //此处就是设置textview为选中状态,方可以实现效果		 
			 convertView.findViewById(R.id.list_live_content_programm)
			 .setSelected(true);

		} else {
			convertView.setBackgroundColor(Color.BLUE);
			//没选中的就不用设置了                           
                        convertView.findViewById(R.id.list_live_content_programm)
			 .setSelected(false);
		}
		convertView.setTag(holder);
		holder.time_tv.setText(items.get(position).getLiveTime());
		holder.content_tv.setText(items.get(position).getLiveContent());
		return convertView;
	}

	private class ViewHolder {
		private TextView time_tv;
		private TextView content_tv;
	}
}



实体类代码Channel.java

package com.jzh.testitem;

public class Channel {
	private String liveTime;
	private String liveContent;

	public Channel(String liveTime, String liveContent) {
		super();
		this.liveTime = liveTime;
		this.liveContent = liveContent;
	}

	public String getLiveTime() {
		return liveTime;
	}

	public String getLiveContent() {
		return liveContent;
	}

}


 

你可能感兴趣的:(android,ListView,String,layout,Class,encoding)