Android:使用片段视图

时间:2015-05-18 19:24:48

标签: java android xml android-layout android-fragments

我一直在寻找理解碎片与活动之间关系的小时数小时,但我仍然无法用我的代码来理解它。

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

我在侧栏上有一个按钮,它应该启动一个带有复选框列表视图的侧边栏片段,这样我就可以选择要在我的图形上显示的数据等。但无论我是什么,该片段都没有显示做。它显示了" Bing〜!"吐司但没有启动其他视图。 也许我不应该使用片段?我不想开始一个新的活动,因为复选框应该与图形交互,另一个片段中包含动态表格,它将来回交互,依此类推。 我不太清楚在这做什么。我有大约3周的Android经验,所以我还不知道它的整个shebang。非常感谢我能得到的任何想法或帮助。我完全被难倒,因此不得不发表我自己的问题。 我非常感谢任何帮助!谢谢!

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) {

}
}

1 个答案:

答案 0 :(得分:0)

您只能替换从代码中添加的元素。 side_fragment是以XML格式添加的,因此是其中的一部分,我们会说#34;只读#34;结构体。您需要从XML中删除<fragment>元素,如果您想稍后替换它,请从代码中添加它。