当我尝试替换

时间:2016-09-25 21:02:12

标签: android

我想在viewpager中替换一个片段,我编写了我的代码 没有得到一些错误,但我得到了一个空的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();


}

1 个答案:

答案 0 :(得分: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;




}
}