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

时间:2013-02-21 12:48:53

标签: android android-listview textview

如果textView在不适合的情况下在listview内部滚动,并且仅在选择了listview中的特定行时,最好的方法是什么。此外,我希望我的文本从左侧滚动到右侧,当它到达结束时从右侧向左滚动(不是默认为选框)

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中设置选择的那个),并且不知道如何覆盖选框行为。

谢谢

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:0)

你可以这样做。

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

        }
    }   
}