我在我的应用程序中使用带有自定义适配器的ListView
,它包含两个TextView
个ImageButton
和一个ImageView
作为背景图片。现在默认情况下,ImageButton
的可见性设置为GONE
。每当点击列表中的某个项目时,该项目的ImageButton
应该变为VISIBLE
,如果另一个项目中ImageButton
为VISIBLE
,则应将其设置为GONE
}。
我已对CustomAdapter进行了更改,以便在点击某个项目(阅读背景ImageButton
)时显示ImageView
,但这会导致我的onItemClick
方法Fragment.java不被调用。此外,当点击项目(阅读ImageView
)时,它不会将之前点击的项目ImageButton
的可见性设置为GONE
,我需要再次点击该项目以设置该项目的{ {1}} ImageButton
的可见度。
这是itemlayout.xml文件
GONE
CustomAdapter.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/backgroundImage"
android:layout_alignBottom="@+id/linearLayout"
android:layout_alignLeft="@id/linearLayout"
android:layout_alignRight="@id/linearLayout"
android:layout_alignTop="@id/linearLayout"
android:scaleType="fitXY"
android:src="@drawable/bg1" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/bg_key">
<TextView
android:id="@+id/topText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="24sp"
android:textColor="#000000"/>
<TextView
android:id="@+id/bottomText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="18sp"
android:textColor="#000000"/>
</LinearLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_repeat"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="@drawable/ic_replay_black_24dp"
android:visibility="gone"
android:background="@null"/>
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:0)
尝试将此添加到您的片段中:
mListView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
mListView.clearChoices();
mListView.setItemChecked(position, true);
mListView.invalidateViews();
}
});
并修改适配器的getView():
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
AbsListView listView = (AbsListView) parent;
if (convertView == null){
convertView = layoutInflater.inflate(R.layout.layout_list_row, null);
holder = new ViewHolder();
holder.backgroundImage = (ImageView) convertView.findViewById(R.id.backgroundImage);
holder.topText = (TextView) convertView.findViewById(R.id.topText);
holder.bottomText = (TextView) convertView.findViewById(R.id.bottomText);
holder.button_repeat = (ImageButton) convertView.findViewById(R.id.button_repeat);
} else {
holder = (ViewHolder) convertView.getTag();
}
SongItem songItem = (SongItem) songList.get(position);
holder.topText.setText(songItem.getTopText());
holder.bottomText.setText(songItem.getBottomText());
if(listView.isItemChecked(position)){
holder.button_repeat.setVisibility(View.VISIBLE);
holder.button_repeat.setTag(position);
}else{
holder.button_repeat.setVisibility(View.GONE);
}
holder.button_repeat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// If you need the Image button click
int pos = v.getTag();
}
});
convertView.setTag(holder);
return convertView;
}