【问题标题】:Fail to add TextView marquee effect in ListView在 ListView 中添加 TextView 跑马灯效果失败
【发布时间】:2019-09-29 01:02:20
【问题描述】:

我有一个 ListView,其 TextView 如下。 ListView 中的一些文本太长,所以我想让它们水平滚动。

<ListView
        android:id="@+id/food_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="16dp"
        android:background="@drawable/border_background" />
<TextView
        android:id="@+id/food_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textColor="@color/black"
        android:textSize="14sp" />

在我的addTextChangedListener 中,我有以下代码(相应地在 EditText 中搜索时列表会发生变化)。然而 ListView 中的每个 TextView 项都在一行中,但不会水平滚动。使用System.out.println(foodName.getText())可以获取ListView中的所有项目名称,那么在for循环中使用foodNameText.setSelected(true);有什么问题吗?

myListAdapter = new SimpleAdapter(AddNewFoodActivity.this, foodListArray, R.layout.food_list_view, colHEAD, dataCell);
                                        foodList.setAdapter(myListAdapter);

int count = foodList.getAdapter().getCount();
for (int i = 0; i < count; i++) {
    TextView foodNameText = (TextView) foodList.getAdapter().getView(i, null, 
                         foodList).findViewById(R.id.food_item);
    foodNameText.setSelected(true);                                      
    //System.out.println(foodName.getText());
}

【问题讨论】:

  • -set foodNameText.setSelected(true);这条线到您的适配器中,这将正常工作。

标签: android android-layout listview


【解决方案1】:

试试这个代码:

<TextView
      android:id="@+id/toolbar_title"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:singleLine="true"
      android:text="Your text" />


foodNameText.setEllipsize(TextUtils.TruncateAt.MARQUEE);
foodNameText.setHorizontallyScrolling(true);
foodNameText.setMarqueeRepeatLimit(-1);
foodNameText.setFocusable(true);
foodNameText.setFocusableInTouchMode(true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-22
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    相关资源
    最近更新 更多