2016-09-25 86 views
-1

我想在viewpager中替換一個片段,我寫了我的代碼,我做了 on't得到一些錯誤,但我得到一個空的fragemnt,但片段有佈局。我想從listview onitemclick中獲取它。 這裏是我的片段替換。我真的不知道我在哪裏犯錯。 謝謝你的幫助。我得到一個空片段,當我嘗試替換

@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

根據你的代碼,你試圖用碎片代替ViewPager。這是你真正想要的嗎?通常一個FrameLayout被一個Fragment替換。 – fernandospr

+0

如果您向我們展示完整代碼 – androidXP

+0

參考[mcve],會更好! –

回答

0

這裏是尋呼機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" 
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" 
    /> 

這裏是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(); 


} 
} 

最後,狗的個人資料是什麼,我想從列表取代。

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; 




} 
} 
相關問題