片段上的自定义列表视图更改屏幕旋转时的位置布局

时间:2015-02-13 09:31:29

标签: android listview android-fragments android-listview

我在一个包含2个自定义布局的片段中有一个列表视图,第一个带有搜索栏,第二个带有toggleButton

enter image description here

但每次我尝试旋转屏幕时,las位置都会显示第一个布局

enter image description here

第二个“亮度”搜索栏效果很好,因为它是第一个位置,如果我再次旋转屏幕,它仍然是这样。

这是我的自定义适配器getView方法:

@Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        View v = convertView;
        final int pos=position;

        if(convertView==null){
            LayoutInflater linf=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if(position==0) {
                v = linf.inflate(R.layout.item_single_option, null);

                soa=getItem(position);

                TextView textView=(TextView)v.findViewById(R.id.singleOptionTextView);
                textView.setText(soa.getTitle());

                SeekBar seekBar=(SeekBar)v.findViewById(R.id.singleOptionSeekBar);
                seekBar.setMax(soa.getSeekBarMax());
                seekBar.setProgress(soa.getValue());

                seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    int progress=0;
                    SystemManager systemManager=new SystemManager(activity);

                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        this.progress =progress;
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {

                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        FileManager fileManager=new FileManager(activity);

                        if(systemManager.isBrightnessAuto()==1){
                            Toast.makeText(activity,activity.getResources().getString(R.string.change_auto_bright),Toast.LENGTH_SHORT).show();
                        }else {
                            systemManager.setBrightness(progress);
                            fileManager.createFile();
                            fileManager.writeToFile(activity.getResources().getString(R.string.bright_modified)+" (" + progress + " "+activity.getResources().getString(R.string.out_of)+" 255)");
                        }
                    }
                });
            }else{
                v = linf.inflate(R.layout.item_single_option_toggle, null);

                soa=getItem(position);

                TextView textView=(TextView)v.findViewById(R.id.singleOptionTextView);
                textView.setText(soa.getTitle());

                final ToggleButton toggleButton=(ToggleButton)v.findViewById(R.id.toggleButton);
                toggleButton.setChecked(soa.isToggle());

                toggleButton.setOnClickListener(new View.OnClickListener() {
                    SingleOptionItem soa=getItem(pos);
                    SystemManager systemManager=new SystemManager(activity);

                    String fileContent;
                    @Override
                    public void onClick(View v) {
                        FileManager fileManager=new FileManager(activity);

                        if (soa.getTitle().equals(activity.getResources().getString(R.string.wifi))) {
                            fileContent = !soa.isToggle() ? activity.getResources().getString(R.string.activated) : activity.getResources().getString(R.string.deactivated);
                            systemManager.toggleWifi(!soa.isToggle());
                            soa.setToggle(!soa.isToggle());
                            fileManager.createFile();
                            fileManager.writeToFile(activity.getResources().getString(R.string.wifi)+": "+ fileContent);

                        } else if (soa.getTitle().equals(activity.getResources().getString(R.string.mobile_data))) {
                            fileContent = !soa.isToggle() ? activity.getResources().getString(R.string.activated) : activity.getResources().getString(R.string.deactivated);
                            systemManager.toggleMobileData(!soa.isToggle());
                            soa.setToggle(!soa.isToggle());
                            fileManager.createFile();
                            fileManager.writeToFile(activity.getResources().getString(R.string.mobile_data)+": "+ fileContent);
                            if (systemManager.isWifiEnabled() == 3)
                                Toast.makeText(activity,activity.getResources().getString(R.string.wont_use_mdata) , Toast.LENGTH_SHORT).show();

                        } else if (soa.getTitle().equals(activity.getResources().getString(R.string.bluetooth))) {
                            fileContent = !soa.isToggle() ? activity.getResources().getString(R.string.activated) : activity.getResources().getString(R.string.deactivated);
                            systemManager.toggleBluetooth(!soa.isToggle());
                            soa.setToggle(!soa.isToggle());
                            fileManager.createFile();
                            fileManager.writeToFile(activity.getResources().getString(R.string.bluetooth)+": "+ fileContent);

                        }else if(soa.getTitle().equals(activity.getResources().getString(R.string.auto_rotate))){
                            fileContent = !soa.isToggle() ? activity.getResources().getString(R.string.activated) : activity.getResources().getString(R.string.deactivated);
                            systemManager.toggleAutoRotate(!soa.isToggle());
                            soa.setToggle(!soa.isToggle());
                            fileManager.createFile();
                            fileManager.writeToFile(activity.getResources().getString(R.string.auto_rotate)+": "+fileContent);

                        }else if(soa.getTitle().equals(activity.getResources().getString(R.string.auto_sync))){
                            fileContent = !soa.isToggle() ? activity.getResources().getString(R.string.activated) : activity.getResources().getString(R.string.deactivated);
                            systemManager.toggleAutoSync(!soa.isToggle());
                            soa.setToggle(!soa.isToggle());
                            fileManager.createFile();
                            fileManager.writeToFile(activity.getResources().getString(R.string.auto_sync)+": "+fileContent);
                        }
                    }
                });
            }
        }
        return v;
    }

这是我的片段代码,列表视图:

public class CustomFragment extends Fragment {

    ListView list;

    public CustomFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        ViewGroup rootView=(ViewGroup)getActivity().getLayoutInflater().inflate(R.layout.fragment_custom, container, false);

        SystemManager systemManager=new SystemManager(getActivity().getApplicationContext());

        boolean wifiStatus=false,bluetoothStatus=false,mobileDataStatus,autoRotateState,autoSync;

        if(systemManager.isWifiEnabled()==1)
            wifiStatus=false;
        else if (systemManager.isWifiEnabled()==3)
            wifiStatus=true;

        if(systemManager.isBluetoothEnabled()==10)
            bluetoothStatus=false;
        else if (systemManager.isBluetoothEnabled()==12)
            bluetoothStatus=true;

        mobileDataStatus = systemManager.isMobileDataEnabled();

        autoRotateState = systemManager.isAutoRotateEnabled();

        autoSync=systemManager.isAutoSyncEnabled();

        ((MenuActivity)getActivity()).getmDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

        list=(ListView)rootView.findViewById(R.id.listViewCustom);

        final ArrayList<SingleOptionItem> itemArray=new ArrayList<SingleOptionItem>();

        SingleOptionItem soa;

        soa=new SingleOptionItem(getResources().getString(R.string.brightness),systemManager.getBrightness(),255);
        itemArray.add(soa);
        soa=new SingleOptionItem(getResources().getString(R.string.wifi),wifiStatus);
        itemArray.add(soa);
        soa=new SingleOptionItem(getResources().getString(R.string.mobile_data),mobileDataStatus);
        itemArray.add(soa);
        soa=new SingleOptionItem(getResources().getString(R.string.bluetooth),bluetoothStatus);
        itemArray.add(soa);
        soa=new SingleOptionItem(getResources().getString(R.string.auto_rotate),autoRotateState);
        itemArray.add(soa);
        soa=new SingleOptionItem(getResources().getString(R.string.auto_sync),autoSync);
        itemArray.add(soa);


        SingleOptionAdapter singleOptionAdapter=new SingleOptionAdapter(getActivity(),itemArray);


        list.setAdapter(singleOptionAdapter);


        return rootView;
    }

}

1 个答案:

答案 0 :(得分:1)

在您的适配器代码中,如果您没有,请覆盖以下方法。

@Override
public int getItemViewType(int position) {
    if(position==0)
        return 0;
    else
        return 1;
}

@Override
public int getViewTypeCount() {
    return 2;
}