更改ListFragment中列表项组件的状态

时间:2014-12-17 17:39:57

标签: android fragment selector

我目前正在根据一些在线教程创建自定义ListView片段 我成功地创建了一个自定义ArrayAdapter以及整个ListFragment事物。

[代码]

我创建的列表项看起来像这样;

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/img"
        android:src="@drawable/img"/>
    <TextView
        android:layout_toRightOf="@+id/img"
        android:layout_margin="4dp"
        android:textSize="36dp"
        android:layout_toEndOf="@+id/img"
        android:layout_height="42dp"
        android:layout_width="fill_parent"
        android:id="@+id/dev_name"
        android:text="TEST"/>
</RelativeLayout>

可绘制的img;

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/orange" /> 
    <item android:state_activated="true"
        android:drawable="@drawable/green" />
    <item android:state_activated="false"
        android:drawable="@drawable/red" /> 
</selector>

自定义ArrayAdapter类;

public class CustomAdapter extends ArrayAdapter<DeviceTest> {
    private final Context context;
    private final DeviceTest[]deviceList;
    public CustomAdapter(Context context, DeviceTest[]deviceList) {
        super(context, R.layout.listitem,deviceList);
        this.context = context;
        this.deviceList =deviceList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View listItem = inflater.inflate(R.layout.listitem, parent, false);
        ImageView img = (ImageView) listItem.findViewById(R.id.img);
        img.setActivated(deviceList[position].isActivated());
        TextView device = (TextView) listItem.findViewById(R.id.dev_name);
        device.setText(deviceList[position].getName());
        return listItem;
    }

}

自定义片段类;

public class MyListFragment extends ListFragment{
private DeviceTest[] deviceList;
private  CustomAdapter customAdapter;
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    deviceList = new DeviceTest[] {new DeviceTest("First"),new DeviceTest("Second")};
    customAdapter = new CustomAdapter(getActivity(),deviceList);
    setListAdapter(customAdapter);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    customAdapter.getItem(position).setActivated(!customAdapter.getItem(position).isActivated());
    customAdapter.notifyDataSetChanged();
}

}

DeviceTest对象;

public class DeviceTest {
    private String name;
    private boolean activated;
    public DeviceTest(String name){
        this.name = name;
        this.activated = true;
    }

    public void setActivated(boolean activated) {
        this.activated = activated;
    }

    public boolean isActivated() {
        return activated;
    }

    public String getName() {
        return name;
    }
}

[问题]

你如何通过onListItemClick方法更改ImageView的状态?
我的目的是让ImageView初始设置为红色,当按下设置为橙色时,然后依次为绿色或红色在以前的状态。

提前致谢!

[解决]

调整代码,为建议加油!

1 个答案:

答案 0 :(得分:0)

试试这个。

 <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">

            <item android:drawable="@color/orange" android:state_pressed="true"></item>
            <!-- pressed -->
            <item android:drawable="@color/green" android:state_focused="true"></item>
            <!-- focused -->
            <item android:drawable="@color/green"></item>
            <!-- default -->
        </selector>