【发布时间】:2017-08-06 01:31:16
【问题描述】:
我有一个自定义适配器,因为我连续有两个 Textview。首先,我只希望左侧 TextViews 可点击并禁用右侧的点击事件。
layout_list_xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView style="@style/TextDesign"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/left" />
<TextView style="@style/TextDesign"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/right" />
</RelativeLayout>
</LinearLayout>
自定义适配器:
public class CustomAdapter extends BaseAdapter {
Context context;
List<Occurence> occurenceList;
TextView left, right;
public CustomAdapter(Context context, List<Occurence> occurenceList) {
this.context = context;
this.occurenceList = occurenceList;
}
@Override
public int getCount() {
return occurenceList.size();
}
@Override
public Object getItem(int position) {
return occurenceList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(context);
View v=inflater.inflate(R.layout.layout_list,parent,false);
left=(TextView) v.findViewById(R.id.left);
right=(TextView) v.findViewById(R.id.right);
left.setText(occurenceList.get(position).singleNumber);
right.setText(occurenceList.get(position).occurence+" occurence");
v.setTag(occurencekList.get(position).getId());
return v;
}
@Override
public boolean areAllItemsEnabled() {
return super.areAllItemsEnabled();
}
@Override
public boolean isEnabled(int position) {
return super.isEnabled(position);
}
}
因此,当用户单击 TextView 时,我想禁用 List 上该 TextView 的单击。单击的电视的值将作为 SpannableString 移动到另一个 TextView,我可以在其中执行另一个操作。这意味着如果用户单击该 SpannableString 将删除该 SpannableString 并重新启用该 ListItem 被禁用的单击操作。
我在下面附上一张图片来查看结果。
我已经读到我应该在适配器类中名为 isEnabled 的 Override 方法中添加一个 if 语句,但我需要具体的解决方案。
【问题讨论】:
-
禁用点击:listview.setOnItemclickListener(null);
-
但这会禁用整个列表的 onclick,不是吗?我只想禁用所选项目并在需要时启用它。
标签: android listview onitemclick