2017-07-28 119 views
0

我使用MySql中的片段获取列表视图中的数据,这里是一个按钮,当我用它来点击按钮时,它不会替代它重叠的片段,另一个BeforeAfter点击按钮。片段在android中重叠另一个

这里是代码,我以列表视图格式获取数据,并点击按钮我想打开一个新的片段,但它的重叠。

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

请发表您的片段 –

+0

@jigarsavaliya请 – Badman

+0

哪里是PlayQuizSubjectWise' –

回答

0

您的片段的父级布局必须具有背景。

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

代替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(); 
+0

的'仍然没有工作 – Badman

+0

u能张贴其中u添加 – Anil

2

它重复,因为你调用fragmentTransaction.add这只是增加了该片段到容器,同时保持所有旧碎片可见。

取而代之的是,使用fragmentTransaction.replace将替换所有现有的片段与新的片段。

+0

仍然没有工作YOUT主要片段代码 – Badman

+0

根据您提出的第一个(未经编辑的)问题,答案是正确的。如果它没有帮助别的事情。您应该提供可重现的代码示例。 https://stackoverflow.com/help/mcve – Kuffs

0

添加背景颜色为您的片段,使片段点击

android:background="fff" 
android:clickable="true" 
+0

仍然不工作..same结果 – Badman

+0

'R.id.playsubjectwise'检查什么是playsubjectwise它应该是framelayout –

+0

请检查它上面我已经添加了所有的xml文件 – Badman

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(白色)。所以像这样的,你应该给的背景颜色中的所有片段

我希望它会帮助你:)

+0

这是一个乐队援助,它只会隐藏现有的片段,并没有解释他们为什么首先出现在那里。解决一个问题比隐藏它 – Kuffs

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

这是假的代码,希望你能理解。

+0

没有正确理解..因为如果我在另一个使用相同的东西那么什么是我想要一些另一个数据的另一种观点 – Badman

+0

anwser更新 –