问题
I'm trying to get scrolling text (marquee) happening inside a listview, but from my previous reading it seems I need to use setSelected(true) on my textview.
Because the textview is inside listitem instead of listview, I can't seem to use getView on that textView, and thus can't use setSelected.
I'll try and explain with some code:
Here is my oncreate:
setContentView(R.layout.albumsview);
Bundle extras = getIntent().getExtras();
String where = extras.getString("where");
String[] whereVal = extras.getStringArray("whereVal");
String[] columns = { BaseColumns._ID, AudioColumns.ALBUM, };
String orderBy = BaseColumns._ID;
albumCursor = managedQuery(
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, columns, where,
whereVal, orderBy);
ListView listView = (ListView) findViewById(R.id.listViewAlbums);
listView.setOnItemClickListener(this);
String[] displayFields = new String[] { AudioColumns.ALBUM };
int[] displayViews = new int[] { R.id.albumTitle };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.albumitem, albumCursor, displayFields, displayViews);
listView.setAdapter(adapter);
The "albumTitle" is the view containing the textview (called subTitle) which I want to setSelected(true).
Could someone please show me how to do this?
Thanks for your help.
回答1:
If you are using Simple Adapter than u have to make custome TextView and than add it in your xml as given below
public class MyTextView extends TextView{
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
rotate();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
rotate();
}
public MyTextView(Context context) {
super(context);
init();
rotate();
}
private void rotate() {
// TODO Auto-generated method stub
setSelected(true);
}
private void init() {
if (!isInEditMode()) {
}
}
}
and than add custome textview in Your albumitem.xml as shown below
<Your Package Name.MyTextView
android:layout_marginTop="10dip" android:id="@+id/tv_parse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="22px"
android:textColor="#34A4c5"
android:ellipsize="marquee"
android:maxWidth="220dp"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginRight="10dp"></Your Package Name.MyTextView>
回答2:
I also had a similar need and came up with the following approach. I implemented SimpleCursorAdapter.ViewBinder
in my activity. It is a single method that looks like this public boolean setViewValue(View view, Cursor cursor, int columnIndex)
. Inside I get the id from the View. If it is the correct one, I cast it to a TextView
and setText();
on it followed by setSelected(true)
and return true. On views I'm not interested in, I return false and let TextView
handle it. No need to create a custom TextView
.
回答3:
You can get each view record in your list, using getChildCount()
and getChildAt(i)
Here are my example
View getViewPattern;
TextView getTextByPattern;
String contentAnswerInPattern;
for (int i = 0; i < listView.getChildCount(); i++) {
getViewPattern = listView.getChildAt(i);
getTextByPattern = (TextView) getViewPattern.findViewById(R.id.value_a);
contentAnswerInPattern = getTextByPattern.getText().toString().trim();
}
来源:https://stackoverflow.com/questions/10861257/using-setselected-on-a-textview-inside-a-listitem-inside-a-listview