2016-10-19 3 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 당신이 위치를 사용자가 클릭 한있는 List<Object> 및 저장소를 만들어야합니다이를 위해

<?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

. 그 후에는 notifydatasetchanged(); 메소드를 수행하십시오.
저장된 위치에 따라 어댑터보기 방법에서 선택한 행의 배경색을 변경하십시오.

+0

도움이되지 않습니다. 자세한 내용을 편집하십시오 –

관련 문제