Ich habe einige Probleme für Fragment in Android.Android Fragment Layout ersetzt nicht eine Ansicht mit linearen Layout
Ich habe eine Implementierung von ViewPager mit zwei Fragment-Klasse mit seinem XML-Layout, einer MainActivity und einem Adapter abgeschlossen.
Erstes Fragment: Lineares Layout ist Parent im zweiten Fragment, Linear Layout ist Parent. Ich habe eine Schaltfläche in meinem ersten Fragment, ich möchte das erste Fragment ersetzen bedeutet, das Fragment von der ersten auf die zweite zu wechseln.
Ich verwende ViewPager. Bitte schauen Sie auf meinen Quellcode.
PagerActivity.java
public class PagerActivity extends FragmentActivity {
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pager);
mViewPager=(ViewPager)findViewById(R.id.viewpager);
/** set the adapter for ViewPager */
mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
// mViewPager.notify();
}
}
PagerAdapter.java
public class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
/** Show a Fragment based on the position of the current screen */
if (position == 0) {
return new Welcome();
}else{
return new Locate();
}
}
@Override
public int getCount() {
return 2;
}
}
Welcome.java
Fragment 1
public class Welcome extends Fragment implements GlobalInterFace, View.OnClickListener{
private Button welcomeBtn;
private View v;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_welcome, container, false);
welcomeBtn=(Button)v.findViewById(R.id.welcomeBtn);
findViewById();
setOnClickListener();
return v;
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.welcomeBtn:{
Log.e("click", "click");
Fragment fragment = new Locate();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.parent_linear, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
}
}
}
Locate.java
01.235.Fragment 2
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_welcome_pager_two, container, false);
locateMe=(Button)v.findViewById(R.id.locateBtn);
locateMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), SignUpActivity.class);
startActivity(i);
((Activity) getActivity()).overridePendingTransition(0,0);
}
});
return v;
}
willkommen xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/parent_linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/splash_color"
android:weightSum="100">
<RelativeLayout
android:id="@+id/rel1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50">
<com.rey.material.widget.ImageView
android:id="@+id/welcome_Image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/welcome"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/welcomeRelativeTwo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:padding="@dimen/universal_margin4"
>
<com.rey.material.widget.TextView
android:id="@+id/Ready"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome_ready"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="@dimen/universal_margin2"
android:layout_centerInParent="true"
/>
<com.rey.material.widget.TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome_descrition"
android:layout_below="@+id/Ready"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/universal_margin"
/>
<com.rey.material.widget.Button
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/welcomeBtn"
app:rd_enable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/description"
android:text="@string/welcome_btntext"
android:textColor="@color/splash_color"
android:background="@color/colorAccent"
android:layout_marginTop="@dimen/universal_margin2"
/>
</RelativeLayout>
<!-- Your RelativeLayout Items -->
</LinearLayout>
Locate.xml
gleiche wie willkommen Layout nur Layout-ID geändert.
Wenn ich auf WelcomeBtn klicken, wird das Fragment Ersatz dieses in meinem Quellcode in Welcome.java
bitte helfen Sie mir nicht working.mentioned.
Ich bekomme nicht Ihren Punkt. Bitte erklären Sie mir. –
sollte ich mein übergeordnetes lineares Layout mit der ID parent_linear ersetzen? –
Ich habe versucht .... aber es funktioniert nicht.Amir –