问题描述
我有一个ListView,每一行都有固定的高度。
每一行包含,旁边的一些图像,一个TextView。
有时候,我想显示的文字是太大,所以我想使它滚动。
所以我加了(基于 Android中制作的TextView滚动)以下行到我的TextView
text.setMaxLines(5);
text.setVerticalScrollBarEnabled(真正的);
text.setMovementMethod(ScrollingMovementMethod.getInstance());
这工作正常,如果我用TextView的孤立而不是当我把TextView的一个ListView里面:
当我做一个垂直滚动,事件似乎是由ListView的消耗。
我如何使这项工作有什么建议?
完整的(测试)的活动中加入以下(无layout.xml)
公共类TestScrollableTextView扩展活动
{
保护无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的LinearLayout布局=新的LinearLayout(本);
//添加一个ListView
ListView控件列表=新的ListView(本);
layout.addView(名单);
list.setAdapter(新BaseAdapter()
{
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{
TextView的文本=新的TextView(TestScrollableTextView.this);
字符串s =;
//增加10行文字,除了第一类是缩进
的for(int i = 0;我小于10;我++)
{
如果(ⅰ大于0)S + =\ t的;
S + =位置+位置+,行=+ I +\ N的;
}
text.setText(多个);
text.setMaxLines(5);
text.setVerticalScrollBarEnabled(真正的);
text.setMovementMethod(ScrollingMovementMethod.getInstance());
返回文本;
}
@覆盖
众长getItemId(INT位置)
{
返回0;
}
@覆盖
公共对象的getItem(INT位置)
{
返回null;
}
@覆盖
公众诠释getCount将()
{
返回20;
}
});
的setContentView(布局);
}
}
一个更好的办法可能是如下创建自己的MyTextView,并呼吁requestDisallowInterceptTouchEvent。通过这样做,你指示它不应该拦截事件的列表视图。
类MyTextView扩展的TextView
{
公共MyTextView(上下文的背景下)
{
超(上下文);
// TODO自动生成构造函数存根
}
@覆盖
公共布尔dispatchTouchEvent(MotionEvent事件)
{
布尔RET;
RET = super.dispatchTouchEvent(事件);
如果(RET)
{
list.requestDisallowInterceptTouchEvent(真正的);
}
返回RET;
}
}
I have a ListView where each row has a fixed height.
Every row contains, next to some images, a TextView.
Sometimes, the text I want to display is too large and hence I would like to make it scrollable.
So I added (based on Making TextView Scrollable in Android) the following lines to my TextView
text.setMaxLines(5);
text.setVerticalScrollBarEnabled(true);
text.setMovementMethod(ScrollingMovementMethod.getInstance());
This works fine if I use the TextView in isolation but not when I put the TextView inside a ListView:
as soon as I do a vertical scroll, the events seem to be consumed by the ListView.
Any suggestions on how I can make this work?
The complete (test)activity is added below (no layout.xml)
public class TestScrollableTextView extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
//add a ListView
ListView list = new ListView(this);
layout.addView(list);
list.setAdapter(new BaseAdapter()
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
TextView text = new TextView(TestScrollableTextView.this);
String s = "";
//add 10 lines of text, all but first are indented
for (int i = 0; i < 10; i++)
{
if(i>0) s+="\t";
s += "position "+position+"; line="+i+"\n";
}
text.setText(s);
text.setMaxLines(5);
text.setVerticalScrollBarEnabled(true);
text.setMovementMethod(ScrollingMovementMethod.getInstance());
return text;
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public int getCount()
{
return 20;
}
});
setContentView(layout);
}
}
A better way is probably to create your own MyTextView as follows and to call requestDisallowInterceptTouchEvent. By doing so, you indicate to the listview that it' shouldn't intercept events.
class MyTextView extends TextView
{
public MyTextView(Context context)
{
super(context);
// TODO Auto-generated constructor stub
}
@Override
public boolean dispatchTouchEvent(MotionEvent event)
{
boolean ret;
ret = super.dispatchTouchEvent(event);
if(ret)
{
list.requestDisallowInterceptTouchEvent(true);
}
return ret;
}
}
这篇关于是否可以滚动TextView中添加到ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!