我使用来自MySql的片段在列表视图中获取数据,当我以前单击按钮时它是一个上面的按钮,它不会替换它相互重叠的片段Before和After点击按钮。
以下是我以列表视图格式提取数据的代码,在单击按钮上我想打开一个新片段,但是它已经过度重复了。
public class PlayQuiz extends Fragment{
public String subject;
String myJSON;
private static final String TAG_RESULTS = "result";
private static final String TAG_NAME = "subname";
String selcsub;
Button b1;
JSONArray peoples = null;
ArrayList<HashMap<String, String>> personList;
ListView list;
TextView ss,name;
InputStream is = null;
String res = null;
String line = null;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.play_quiz, container, false);
list = (ListView) rootView.findViewById(R.id.listView);
ss = (TextView) rootView.findViewById(R.id.textView1);
b1 = (Button) rootView.findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
PlayQuizSubjectWise fragmentManager = new PlayQuizSubjectWise();//.beginTransaction();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.playsubjectwise, fragmentManager);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
主列表xml文件
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sarvashikshan.PlayQuiz"
android:background="#DCDCDC"
android:id="@+id/playsubjectwise">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Subject"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="16dp" >
</ListView>
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/listView"
android:layout_marginRight="44dp"
android:text="Button" />
</RelativeLayout>
这是列表xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#DCDCDC"
>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
PlayQuizSubjectWise XML
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="35dp"
android:layout_toRightOf="@+id/textView1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="45dp" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/radioGroup1"
android:layout_marginTop="78dp"
android:text="Next" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="Finish" />
</RelativeLayout>
答案 0 :(得分:3)
您的第二个片段的父级布局必须具有背景。
PlayQuizSubjectWise XML
<RelativeLayout 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"
android:background="#FFF" //Here
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
答案 1 :(得分:2)
而不是add
使用replace
替换
fragmentTransaction.add(R.id.playsubjectwise, fragmentManager);
与
fragmentTransaction.replace(R.id.playsubjectwise, fragmentManager);
试试这个
PlayQuizSubjectWise fragmentManager = new PlayQuizSubjectWise();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.playsubjectwise, fragmentManager);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
答案 2 :(得分:2)
它重叠是因为你正在调用fragmentTransaction.add
,它只是将片段添加到容器中,同时保持所有旧片段可见。
相反,使用fragmentTransaction.replace
将用新的片段替换所有现有片段。
答案 3 :(得分:0)
为片段添加背景颜色并使片段可点击
android:background="fff"
android:clickable="true"
答案 4 :(得分:0)
我也遇到了碎片重叠的问题。
对于解决方案:
只需将背景颜色赋予父版面 使用XML的片段。
例如。
这是onefragment.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
**android:background="#fff"**
android:orientation="vertical">
<!-- Fragments UI Components-->
</LinearLayout>
所以这里我给了背景色#fff(白色)。所以你应该在所有片段中给出背景颜色
我希望它会对你有所帮助:)。
答案 5 :(得分:0)
如果您想将replace
用于片段,则您的布局playsubjectwise
不应包含任何视图。
您应该将TextView, ListView and Button
提取到新的片段,然后您可以使用replace
来切换两个片段。
MainList xml,只是一个布局,不包含任何视图
<RelativeLayout
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sarvashikshan.PlayQuiz"
android:background="#DCDCDC"
android:id="@+id/playsubjectwise">
</RelativeLayout>
然后将这些视图作为TempFragment放入新片段。
首先,执行fragmentTransaction.add(R.id.playsubjectwise, TempFragment);
如果要替换为fragmentManager
,请执行
fragmentTransaction.replace(R.id.playsubjectwise, fragmentManager);
它的假代码,希望你能理解。