我在片段中有一个自定义列表视图,并且在项目单击文本颜色更改和选框运行时,但是在第一次项目单击时没有任何工作,没有选框或没有文本颜色更改,第二次单击两者都工作正常。请帮忙!!
mainListViews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
textView = (TextView) view.findViewById(R.id.textView);
String text = textView.getText().toString();
song_namef.setText(text);
setItemNormal();
View rowView = view;
views=rowView;
setItemSelected(rowView);
}});
public void setItemSelected(View view){
View rowView = view;
TextView tv = (TextView)rowView.findViewById(R.id.textView);
tv.setTextColor(Color.WHITE);
tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
tv.setSingleLine(true);
tv.setHorizontallyScrolling(true);
tv.setSelected(true);
tv.requestFocus();
}
public void setItemNormal()
{
for (int i=0; i< mainListViews.getChildCount(); i++)
{
View v = mainListViews.getChildAt(i);
TextView txtview = ((TextView)v.findViewById(R.id.textView));
txtview.setSelected(false);
txtview.setSingleLine(true);
txtview.setTextColor(getResources().getColor(R.color.tabsScrollColor));
}}
布局:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_height="80dp"
android:layout_width="match_parent"
android:layout_marginBottom="16dp"
app:cardCornerRadius="16dp"
app:cardElevation="16dp"
card_view:cardCornerRadius="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
card_view:cardBackgroundColor="@android:color/black"
android:divider="#B49238"
>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/cancel"
android:layout_marginLeft="1200dp"
android:layout_marginTop="18dp"
android:id="@+id/imageView19" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_toStartOf="@+id/imageView16">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView"
android:textSize="30dp"
android:singleLine="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_marginStart="100dp"
android:layout_marginTop="18dip"
android:textColor="@color/tabsScrollColor"
android:layout_alignParentTop="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="@+id/textView11" />
</LinearLayout>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginEnd="10dp"
android:id="@+id/button"
android:src="@drawable/musicalbum"
android:padding="5dp"
android:focusable="false"
android:layout_marginStart="10dp"
android:layout_marginTop="7dip"
android:layout_alignParentRight="true"
/>
将 android:descendantFocusability="blocksDescendants" 添加到您的行项中它应该工作正常如果您仍然遇到问题,请告诉我