简体   繁体   English

滚动(自定义选取框)textview文本时,listview不适合

[英]Scroll (custom marquee) textview text inside listview when it doesn't fit

What is the best way to accomplish textView scrolling inside listview when it doesn't fit and only when specific row inside listview is selected. 什么是最好的方法来完成textView在列表视图内滚动,当它不适合并且仅当选择listview内部的特定行时。 Also, I would like my text to scroll from left side to right and when it reaches end to scroll back from right side to left (not default as marquee does) 另外,我希望我的文本从左侧滚动到右侧,并在到达终点时从右侧滚动到左侧(不像字幕那样默认)

Xml for ListView row: 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"
             />

I'm using Custom ListViewAdapter and overrided scroll listener: 我正在使用Custom 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;
                }

            }

        });

    }

I guess that I have to set textview to be selected (the one I'm setting selection within ListView), and have no idea how to override marquee behavior. 我想我必须设置要选择的textview(我正在ListView中设置选择的那个),并且不知道如何覆盖选取框的行为。

Thank you 谢谢

Try Animation. 尝试动画。 It easy to use and will do exactly what u want: 它易于使用,并且可以完全满足您的要求:

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

And now Java code: 现在是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();
}

You can do this. 你可以这样做。

xml for TextView 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" />

I assume that you are extending your activity from ListActivity . 我假设您正在从ListActivity扩展您的活动。 Do the following In your onListItemClick 在您的onListItemClick执行以下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);

        }
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM