永久更改所选列表视图项目的背景颜色,直到选择其他项目

时间:2016-10-19 08:19:20

标签: android background-color

我想永久更改所选项目的背景颜色,直到选择了另一个项目。我已尝试过很多在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>

2 个答案:

答案 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
}