2016-09-25 11 views
-1

Ich möchte ein Fragment in Viewpager ersetzen und ich schrieb meinen Code, ich mache on't einige Fehler, aber ich bekomme eine leere Fragetn, aber das Fragment hat Layout . Ich möchte es von einer ListView onitemclick bekommen. Hier ist mein Fragment ersetzen. Ich weiß wirklich nicht, wo ich Fehler mache. Vielen Dank für Ihre Hilfe.Ich bekomme eine leere Fragmente, wenn ich versuche, ersetzen

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 

    Fragment dogProfile = new ProfileDogFragment(); 
    Bundle bundle = new Bundle(); 
    bundle.putString("DOG_NAME", dogList.get(position).getName()); 
    bundle.putInt("DOG_PROFILE_PICTURE",dogList.get(position).getImgId()); 
    dogProfile.setArguments(bundle); 
    FragmentTransaction ft = getFragmentManager().beginTransaction(); 
    ft.replace(R.id.viewpager,dogProfile); 
    ft.addToBackStack(null); 
    ft.commit(); 


} 
+0

Gemäß Ihrem Code versuchen Sie einen ViewPager durch ein Fragment zu ersetzen. Willst du das wirklich? Normalerweise wird ein FrameLayout durch ein Fragment ersetzt. – fernandospr

+0

es wäre besser, wenn Sie uns vollständigen Code – androidXP

+0

Verweis auf [MCVE] zeigen! –

Antwort

0

Hier ist das XML-Pager

<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" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
tools:context=".activities.MainActivity" 
android:orientation="vertical"> 

<include layout="@layout/toolbar" android:id="@+id/main_toolbar"/> 

<android.support.v4.view.ViewPager 
    android:id="@+id/viewpager" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:background="#ffffff"/> 

<android.support.design.widget.TabLayout 
    android:id="@+id/sliding_tabs" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:tabTextAppearance="@style/MineCustomTabText" 
    app:tabTextColor="#ffffff" 
    app:tabSelectedTextColor="#A9A9A9" 
    android:background="@color/menu_bottom_bar" 
    /> 

hier ist die listfragment:

public class ListDogFragment extends ListFragment { 



    ArrayList<Dog> dogList; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
    View root = inflater.inflate(R.layout.list_dog,container,false); 

    dogList = new ArrayList<Dog>(); 

    dogList.add(new Dog("Bolyhos",123,2,R.drawable.asd,"male","black and 
    white","Border Collie",null,"This is my dog",false)); 



    setListAdapter(new DogListAdapter(getActivity(),dogList)); 
    return root; 
} 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

} 
    @Nullable 
    public View getView(int position, View convertView, ViewGroup parent) { 
    return convertView; 
    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 

    Fragment dogProfile = new ProfileDogFragment(); 
    Bundle bundle = new Bundle(); 
    bundle.putString("DOG_NAME", dogList.get(position).getName()); 
    bundle.putInt("DOG_PROFILE_PICTURE",dogList.get(position).getImgId()); 
    dogProfile.setArguments(bundle); 
    FragmentTransaction ft = getFragmentManager().beginTransaction(); 
    ft.replace(R.id.viewpager,dogProfile); 
    ft.addToBackStack(null); 
    ft.commit(); 


} 
} 

und der letzte, der Hund Profil, was ich aus der Liste ersetzt werden soll.

public class ProfileDogFragment extends Fragment{ 

    TextView dogName; 
    ImageView dogProfileImageId; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
    container, @Nullable Bundle savedInstanceState) { 
    View root = inflater.inflate(R.layout.profile_dog,container,false); 


    dogName = (TextView) root.findViewById(R.id.dogProfileName); 
    dogProfileImageId = (ImageView) 
    root.findViewById(R.id.dogProfilePicture); 


    Bundle bundle = this.getArguments(); 
    String name = bundle.getString("DOG_NAME"); 
    int picture = bundle.getInt("DOG_PROFILE_PICTURE"); 

    dogName.setText(name); 
    dogProfileImageId.setImageResource(picture); 
    return root; 




} 
} 
Verwandte Themen