2016-10-19 4 views
0

選択したアイテムの背景色を、別のアイテムが選択されるまで恒久的に変更します。私は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

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フォルダに新しいDrawable Resouce Fileを作成します。

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 
} 
+0

prevSelectedViewは何ですか? –

+0

@ ZohaibSiddique prevSelectedViewは、LeftFilterContentsのスコープで定義されたビューです。 – Abbas

+0

何がうまくいかないような有用な情報を追加してみませんか?あなたは選択ハイライトを取得しないか、ビューは状態などを保持しません。 – Abbas

0

あなたはList<Object>や位置、ユーザがクリックした中でストアを作成する必要があります。その後、ちょうどnotifydatasetchanged();メソッドを実行します。
保存された位置に従ってアダプタを表示する方法では、選択した行の背景色を変更します。

+0

は役に立ちません。詳細を編集してください –

関連する問題