gpt4 book ai didi

java - Android:用 fragment View 替换 Activity View

转载 作者:太空宇宙 更新时间:2023-11-04 13:50:41 25 4
gpt4 key购买 nike

我花了几个小时试图理解 fragment 和 Activity 之间的关系,但我仍然无法正确使用我的代码。

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

我的侧边栏上有一个按钮,应该启动一个侧边栏 fragment ,其中有一个复选框 ListView ,这样我就可以选择要在我的图表等上显示的数据。但是无论我做什么,该 fragment 都不会显示。它显示“Bing~!” Toast 但没有启动其他 View 。也许我不应该使用 fragment ?我不想开始一个新的 Activity ,因为该复选框应该与图形和另一个带有动态表的 fragment 交互,并且它将来回交互等等。我不太确定在这里做什么。我有大约 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 中,因此是“只读”结构的一部分。您需要删除<fragment>从 XML 中元素它,如果您想稍后替换它,可以从代码中添加它。

关于java - Android:用 fragment View 替换 Activity View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30311312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com