0

インフレータを使用する次のリスト アダプターがあります。ここに色の変更を追加しようとしましたが、クリックされたアイテムだけでなく、すべてのアイテムが変更されます。

private class ListAdapter extends ArrayAdapter<jsonData>{

    private List<jsonData> jList;

    public ListAdapter(Context context, int resource,List<jsonData> jList) {
        super(context, resource, jList);
        this.jList = jList;
    }
         public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;

                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.listviewact_layout, null);
                            v.setBackgroundResource(R.layout.list_selector);
                }

                jsonData jd = jList.get(position);

                if (jd != null) {             
                    TextView name = (TextView) v.findViewById(R.id.memberName);
                    TextView dateJoined = (TextView) v.findViewById(R.id.dateJoined);
                    if (name != null) {
                        name.setText("Member Name: " + jd.name);
                    }
                    if (dateJoined != null) {
                        dateJoined.setText("Joined: " + getNewDate(jd.joined));
                    }
                }

                return v;
            }

アイテムの位置も取得できます。色以外のほとんどは正常に機能します。セレクター用のリソースファイルも追加してみましたが、同じ結果になります。

更新:これはうまくいくようです。アイテムをスクロールすると色がおかしくなりますが、グリッチがあります。

            public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {          

            if(selectedItemPosition != position){
                //Resets old item to original color
                parent.getChildAt(selectedItemPosition).setBackgroundColor(Color.BLACK);
                view.setBackgroundColor(Color.BLUE);                    
                selectedItemPosition = position;
            }               
        }
4

2 に答える 2

0
view.setbackground();

Changes the background of entire list .since your view points to that list not a field in that list.
try this

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/black" /> <!-- focused -->
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/black" /> <!-- focused and pressed-->
    <item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
    <item android:drawable="@color/black" /> <!-- default -->
</selector> 
于 2013-03-20T15:01:40.887 に答える
0

セレクターxmlファイルをリストビューに追加するために使用しandroid:listSelectorないでくださいandroid:background

         <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:listSelector="@drawable/list_selector">
        </ListView>

このセレクターxmlを試してください

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true" 
 android:state_pressed="true" android:drawable="@color/android:blue" />
<item android:state_enabled="true"
 android:state_focused="true" android:drawable="@color/android:transparent" />
<item
 android:drawable="@color/android:black" />

于 2013-03-20T14:08:26.560 に答える