我一直在寻找试图理解片段和活动之间关系的时间,但是我仍然无法用我的代码正确地理解它。

我有一个mainScreen类,它加载actvity_main.xml。该主屏幕的布局包含图形和侧边栏等。

我在侧栏上有一个按钮,该按钮应该启动侧栏片段,其中有一个复选框列表视图,因此我可以选择要在图形上显示的数据等。但是无论我做什么,该片段都不会显示。它显示“ Bing〜!”吐司,但没有其他视图被启动。
也许我不应该使用片段?我不想启动一个新活动,因为该复选框应该与图以及其中包含动态表的另一个片段进行交互,并且它将来回交互等等。
我不太确定该怎么做。我大约有3个星期的Android经验,所以我还不完全了解它的全部功能。非常感谢任何想法或我能得到的帮助。我完全被困住了,因此不得不发表自己的问题。
我真的很感谢任何帮助!谢谢!

mainScreen.java

public class mainScreen extends Activity {

private TextView text_display;
private Button button_list;
private Button button_table;
private String[] dataHolder;
public boolean listClicked = false;


public void loadData () {

    //loaddata stuff here

}

public void createGraph () {

    //graph create stuff here


}

public void buttonClick () {

    button_list.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    sideFragment sf = new sideFragment();
                    FragmentManager fm = getFragmentManager();
                    FragmentTransaction ft = fm.beginTransaction();
                    ft.replace(R.id.side_fragment, sf);
                    ft.commit();

                    Toast.makeText(mainScreen.this, "Bing~!", Toast.LENGTH_SHORT).show();
                }
            }
    );


}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent i = getIntent();
    dataHolder = i.getStringArrayExtra("dataHolder");
    button_list = (Button)findViewById(R.id.button_list);

    loadData();
    createGraph();
    buttonClick();

}

}


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".mainScreen"
android:id="@+id/mainlayout"
android:orientation="horizontal"
android:weightSum="1">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="85dp"
    android:layout_height="match_parent">

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="85dp"
        android:layout_height="85dp"
        android:text="Select"
        android:id="@+id/button_list" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="85dp"
        android:layout_height="85dp"
        android:text="Table"
        android:id="@+id/button_table" />
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text=" "
        android:id="@+id/text_display"
        android:textSize="26dp"
        android:layout_margin="5dp" />

    <com.jjoe64.graphview.GraphView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/graph" />

</LinearLayout>

<fragment
    android:name="xabre.mobileicip.sideFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/side_fragment" />

</LinearLayout>


sideFragment.java

public class sideFragment extends Fragment implements android.widget.CompoundButton.OnCheckedChangeListener {

ListView listviewFrag;
ArrayList<sideFrag> sideFragList;
sideFragAdapter sfAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.side_fragment, container, false);

    listviewFrag = (ListView) view.findViewById(R.id.side_listview);
    //displayList();

    return view;

}

private void displayList() {

    sideFragList = new ArrayList<sideFrag>();
    sideFragList.add(new sideFrag("SPO2"));
    sideFragList.add(new sideFrag("O2 Flow Rate"));
    sideFragList.add(new sideFrag("Resp."));
    sideFragList.add(new sideFrag("Cardiac Output"));
    sideFragList.add(new sideFrag("Cardiac Index"));
    sideFragList.add(new sideFrag("SVR"));
    sideFragList.add(new sideFrag("Wedge Pressure"));

    listviewFrag.setAdapter(sfAdapter);

}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    int pos = listviewFrag.getPositionForView(buttonView);
    if (pos != ListView.INVALID_POSITION) {
        sideFrag sf = sideFragList.get(pos);
        sf.setSelected(isChecked);

        Toast.makeText(getActivity(),"" + sf.getName(), Toast.LENGTH_SHORT).show();
        //Toast.makeText(sideFragment.this, "Clicked on sideFrag: " + sf.getName() + ". State: is " + isChecked, Toast.LENGTH_SHORT).show();
    }
}
}


side_fragment.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".sideFragment"
android:id="@id/side_fragment">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="HELLO WORLD"
    android:id="@+id/helloTester"/>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp">

    <ListView
        android:id="@+id/side_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>

</LinearLayout>

</LinearLayout>


side_list.xml

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

<CheckBox android:id="@+id/check_box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onCheckboxClicked"
    android:layout_marginBottom="15dp" />

<TextView
    android:id="@+id/check_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/check_box"
    android:textStyle="bold"/>


</RelativeLayout>


sideFragAdapter.java

    package xabre.mobileicip;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.List;

class sideFrag {
    String name;
    boolean selected = false;
    public sideFrag(String name) {
        super();
        this.name = name;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
public class sideFragAdapter extends ArrayAdapter<sideFrag> implements CompoundButton.OnCheckedChangeListener {

    private List<sideFrag> sideList;
    private Context context;

    public sideFragAdapter (List<sideFrag> sideList, Context context) {
        super(context, R.layout.side_list, sideList);
        this.sideList = sideList;
        this.context = context;
    }

    private static class sideHolder {
        public CheckBox check_box;
        public TextView check_name;
        /* public CheckBox check_O2FR;
        public CheckBox check_Resp;
        public CheckBox check_Carout;
        public CheckBox check_Carind;
        public CheckBox check_svr;
        public CheckBox check_resprate;
        public CheckBox check_Peep;
        public CheckBox check_O2AF;
        public CheckBox check_FIO2;
        public CheckBox check_PO2;
        public CheckBox check_HCO3;
        public CheckBox check_Urea;
        public CheckBox check_Potassium;
        public CheckBox check_Sodium;
        public CheckBox check_Creatinine;
        public CheckBox check_FluidIn;
        public CheckBox check_FluidOut;
        */
    }

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;

    sideHolder holder = new sideHolder();

    if(convertView == null) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.side_list, parent, false);

        holder.check_box = (CheckBox) v.findViewById(R.id.checkbox);
        holder.check_name = (TextView) v.findViewById(R.id.check_name);


        holder.check_box.setOnCheckedChangeListener(this);

    } else {
        holder = (sideHolder) v.getTag();
    }

    sideFrag p = sideList.get(position);
    holder.check_name.setText(p.getName());
    holder.check_box.setChecked(p.isSelected());
    holder.check_box.setTag(p);

    return v;
}

@Override
public void onCheckedChanged(CompoundButton buttonView,
                             boolean isChecked) {

}
}

最佳答案

您只能替换从代码添加的元素。 side_fragment是在XML中添加的,因此是“只读”结构的一部分。您需要从XML中删除<fragment>元素,并在以后从代码中添加它。

10-07 18:30