【问题标题】:Scroll (custom marquee) textview text inside listview when it doesn't fit在列表视图中滚动(自定义选取框)文本视图文本,当它不适合时
【发布时间】:2013-02-21 12:48:52
【问题描述】:

当它不适合并且仅当列表视图中的特定行被选中时,在列表视图中完成 textView 滚动的最佳方法是什么。另外,我希望我的文本从左侧滚动到右侧,并且当它到达末尾时从右侧向后滚动到左侧(不像选框那样默认)

ListView 行的 XML:

<TextView
            android:id="@+id/txtWouldLikeToScroll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:paddingBottom="1dp"
            android:textColor="#fff"
            android:textStyle="bold"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"  
            android:singleLine="true"
             />

我正在使用自定义 ListViewAdapter 并覆盖滚动侦听器:

myList.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {


                // TODO Auto-generated method stub

            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

                switch (scrollState) {
                case OnScrollListener.SCROLL_STATE_IDLE:

                  view.setSelection(positionWhereIwouldLikeToPointSelection);

                    break;
                case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                    break;
                case OnScrollListener.SCROLL_STATE_FLING:
                    break;
                }

            }

        });

    }

我想我必须设置要选择的 textview(我在 ListView 中设置选择的那个),并且不知道如何覆盖选取框行为。

谢谢

【问题讨论】:

    标签: android android-listview textview


    【解决方案1】:

    尝试动画。它易于使用,并且完全符合您的要求:

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1" />
    

    现在是 Java 代码:

    private TextView mTextView...
    private Animation mAnimation...
    
    private void animateTextView() {
        int textWidth = getTextViewWidth(mTextView);
        int maxWidth = ... // Do not know what width you want.
    
        /* Start animation only when text is longer than dislay width. */
        if(maxWidth<textWidth) {
            mAnimation = new TranslateAnimation(
                    0, displayWidth-textWidth,
                    0, 0);
            mAnimation.setDuration(3000);    // Set custom duration.
            mAnimation.setStartOffset(500);    // Set custom offset.
            mAnimation.setRepeatMode(Animation.REVERSE);    // This will animate text back ater it reaches end.
            mAnimation.setRepeatCount(Animation.INFINITE);    // Infinite animation.
    
            mTextView.startAnimation(mAnimation);
        }
    }
    
    private int getTextViewWidth(TextView textView) {
        textView.measure(0, 0);    // Need to set measure to (0, 0).
        return textView.getMeasuredWidth();
    }
    

    【讨论】:

      【解决方案2】:

      你可以这样做。

      TextView的xml

      <TextView
          android:id="@+id/label"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:ellipsize="marquee"
          android:marqueeRepeatLimit="marquee_forever"
          android:singleLine="true" />
      

      我假设您正在从ListActivity 扩展您的活动。在您的 onListItemClick 中执行以下操作

      public void onListItemClick(ListView parent, View row, int position, long id) {
      
          TextView textViewOne =   (TextView)row.findViewById(R.id.text_view);
          textViewOne.setSelected(true);
          for (int i = 0; i < parent.getChildCount(); i++) {
              if(i!=position){
                  View view = (View) parent.getChildAt(i).findViewById(R.id.view);
      
                  textViewOne = (TextView)view.findViewById(R.id.text_view);
                  textViewOne.setSelected(false);
      
              }
          }   
      }
      

      【讨论】:

      • 不幸的是我无法设置 ListItemClick 侦听器,因为当 ListView ScrollState 为空闲时必须选择 TextView(ListView 行)(用户正在滚动列表并且在 SCROLL_STATE_IDLE 我指示要由 listView 选择的行.setSelection(index).
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多