我有一个类LanguagesListAdapter,我想使用行条件来显示特定行中的图像。 TextViews 值是正确的,但每行的 ImageView 可见。我的代码在这里:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = mcontex.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.language_list, null);
holder = new ViewHolder();
holder.langTextView = (TextView) convertView.findViewById(R.id.languageTextView);
holder.langTextView.setGravity(Gravity.LEFT);
holder.check = (ImageView) convertView.findViewById(R.id.check_lang);
holder.check.setVisibility(View.GONE);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.langTextView.setText(languages[position]);
if (session.getLanguage().equals("fa") && languages[position].equals("farsi")) {
holder.check.setVisibility(View.VISIBLE);
} else if (session.getLanguage().equals("en") && languages[position].equals("English")) {
holder.check.setVisibility(View.VISIBLE);
}
return convertView;
}
我的列表 XML 在这里:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:id="@+id/languageTextView"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
<ImageView
android:id="@+id/check_lang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
app:srcCompat="@drawable/ic_check_dark"/>
</RelativeLayout>
如何修复它以使 ImageView 仅在特定行中可见?
最佳答案
如果view被回收(即convertView不为null),ImageView不会消失。因此添加 else
语句使 ImageView
消失。
if (session.getLanguage().equals("fa") && languages[position].equals("farsi")) {
holder.check.setVisibility(View.VISIBLE);
} else if (session.getLanguage().equals("en") && languages[position].equals("English")) {
holder.check.setVisibility(View.VISIBLE);
} else {
holder.check.setVisibility(View.GONE);
}
关于java - 在android中的列表适配器中的列表的特定行中使用条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46824885/