我有一個自定義適配器,因爲我有兩個Textviews在一行。首先,我只想要左側的TextViews可點擊並禁用右側的點擊事件。禁用ListView項目單擊然後啓用它
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的用戶點擊我要禁用點擊在列表中的TextView。被點擊的TV的值將作爲SpannableString移動到另一個TextView,我可以在哪裏執行另一個操作。這意味着如果用戶單擊該SpannableString將刪除該SpannableString並重新啓用該ListItem被禁用的點擊操作。
我在下面附上了一張圖片以查看結果。
我讀過有關,我應該把一個if語句在適配器類叫的IsEnabled覆蓋方法,但我需要具體的解決方案。
禁用點擊:listview.setOnItemclickListener(NULL); –
但這會禁用整個列表的onclick,不會嗎? 我只想禁用選定的項目並在需要時啓用它。 –