我想永久更改所选项目的背景颜色,直到选择了另一个项目。我已尝试过很多在stackoverflow上的答案,但没有得到所需的解决方案。我正在使用列表片段。代码,
public class LeftFilterContents extends ListFragment implements AdapterView.OnItemClickListener {
Get get;
View view;
ListView listView;
AdapterLeftFilterContents adapter;
public interface Get {
void getData(int s);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
get = (Get) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString());
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.list_fragment, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayList<String> arrayList = new ArrayList<>();
listView = (ListView) view.findViewById(android.R.id.list);
arrayList.add("Type");
arrayList.add("Date");
arrayList.add("From to Date");
adapter = new AdapterLeftFilterContents(getActivity(), R.layout.single_list_item_view, arrayList);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
get.getData(position);
}
}
list_fragmnet.xml
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:listSelector="#666666">
</ListView>
single_list_item_view.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">
<TextView
android:id="@+id/list_item_left_filter_contents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
答案 0 :(得分:0)
为此,您必须创建List<Object>
并存储用户点击的位置。之后只做notifydatasetchanged();
方法
在根据保存位置获取适配器的查看方法时,更改所选行的背景颜色。
答案 1 :(得分:0)
您需要做的就是为TextView
或父LinearLayout
提供一个选择器。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/my_list_selector">
<TextView
android:id="@+id/list_item_left_filter_contents"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
在应用的Drawable Resouce File
文件夹中创建新的drawable
。
<强> my_list_selector.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/controlbar_gradient" />
<item android:drawable="@android:color/transparent" />
</selector>
<强> control_bar_gradient.xml 强>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:type="linear"
android:startColor="#7e0809"
android:endColor= "#fa0000" />
</shape>
最后只需选择点击的视图,取消选择之前的视图。
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
if (prevSelectedView != null
prevSelectedView.setSelected(false);
view.setSelected(true);
prevSelectedView = view;
get.getData(position);
// Here provide position to the adapter to update views in case of scrolling
}