是否可以将可滚动的 TextView 添加到 ListView 中?
我有一个 ListView,其中每行都有固定的高度。
每行在一些图像旁边都包含一个 TextView。
有时,我想要显示的文本太大,因此我想使其可滚动。
因此,我将以下几行添加到我的 TextView 中(基于 使 TextView 在 Android 上可滚动)
text.setMaxLines(5);
text.setVerticalScrollBarEnabled(true);
text.setMovementMethod(ScrollingMovementMethod.getInstance());
如果我单独使用 TextView,则效果很好,但当我将 TextView 放入 ListView 时则效果不佳:
一旦我进行垂直滚动,事件似乎就会被 ListView 消耗。
关于我如何完成这项工作有什么建议吗?
下面添加了完整的(测试)活动(无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);
}
}
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 on 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
getLineCount 和 getLineHiegt 并检查 Text 是否大于 TextView。
如果您使用此代码,则可以通过使用 (boolean)isLarger = true 触摸 TextView 以外的任何位置来滚动 listView。
getLineCount and getLineHieght and check if the Text is larger than the TextView.
if you use this code, the listView can be scrolled by touching anywhere other than the TextView with (boolean)isLarger = true.
我可以通过创建 ListView 的子类并重写 onTouchEvent 来完成这项工作,如下所示:
- 我首先检查 MotionEvent 是否在 childView 内
- 如果是,我称孩子的 onTouchEvent
这看起来确实是相当基本的框架功能,所以我很惊讶这不是由 ListView 本身处理的。我错过了什么吗?
I can make this work by creating a subclass of ListView and override onTouchEvent as follows:
- I first check whether the MotionEvent is inside a childView
- if it is, I call the child's onTouchEvent
This does seem quite basic framework functionality so I'm surprised that this isn't handled by ListView itself. Am I missing something?
更好的方法可能是创建您自己的 MyTextView,如下所示并调用 requestDisallowInterceptTouchEvent。通过这样做,您向列表视图表明它不应该拦截事件。
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.
使用此自定义来解决您的问题。
Use this Custom to solve your issue.