是否可以将可滚动的 TextView 添加到 ListView? [英] Is it possible to add a scrollable TextView to a ListView?

查看:16
本文介绍了是否可以将可滚动的 TextView 添加到 ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListView,其中每一行都有一个固定的高度.
每行包含一些图像旁边的 TextView.
有时,我想显示的文本太大,因此我想让它可以滚动.
所以我添加了以下几行(基于 Making TextView 在 Android 上可滚动)到我的 TextView

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());

如果我单独使用 TextView,这可以正常工作,但当我将 TextView 放入 ListView 时则不行:
一旦我进行垂直滚动,这些事件似乎就被 ListView 消耗了.

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?

下面添加了完整的(测试)activity(没有layout.xml)

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+="	";
                s += "position "+position+"; line="+i+"
";
            }
            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);

  }
}

推荐答案

更好的方法可能是创建您自己的 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.

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆