ListView 中的TextView实现跑马灯效果
转载:http://blog.csdn.net/sky181772733/article/details/7003125#
案例:怎么样在一个ListView中含有TextView的item中实现字母滚动呢。这个在一些特定的场合经常用得到。如下图,当焦点位于某个item的时候其内容就自动滚动显示
要实现这样的效果,废话不多说直接上代码:
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"
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; } } 这篇文章是我转载的 这个人的 http://blog.csdn.net/jzh2012/article/details/6885204 大家可以看看,其实我最后找到了比较简单的相对来说你可以在 item的里面设置TextView的属性,至少我先在认为的几个不可或去的 [java] view plaincopyprint? <TextView android:id="@+id/app_loc" android:layout_width="120dip" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="12dp" android:layout_marginLeft="10dip" android:focusableInTouchMode="true" android:ellipsize="marquee" android:scrollHorizontally="true" android:marqueeRepeatLimit="marquee_forever"></TextView><TextView android:id="@+id/app_loc" android:layout_width="120dip" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="12dp" android:layout_marginLeft="10dip" android:focusableInTouchMode="true" android:ellipsize="marquee" android:scrollHorizontally="true" android:marqueeRepeatLimit="marquee_forever"></TextView>
可以参考我的一下....然后如果想不获取焦点就让TextView的跑马灯效果直接在自定义adapter中setView方法找到该TextView并设置成convertView.findViewById(R.id.app_loc)
.setSelected(true);
相关推荐
chenjinlong 2020-02-19
83580494 2013-07-19
langjiao 2013-07-16
kiduo0 2013-07-10
gongzhiyao0 2010-11-15
bigdatazx 2010-11-05
Urchindong 2011-08-01
peixiaopao 2011-08-21
MeOrdinary 2014-05-13
magic00 2019-10-21
guizhongyun 2011-09-27
csuhanshuai 2015-03-30
Sunanang 2015-03-30
snailbing 2015-04-23
huohu00 2015-04-22
toperfect 2015-07-01
nickey 2012-01-29
Rgenxiao 2012-01-26
满城风絮 2011-12-03