这是活动类中的onclick项侦听器,其中传递了名为movieTagsItem的对象
public void onItemClick(MovieTagsItem movieTagsItem) {
Log.d(TAG,"It is clicked");
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.news_details, MovieProfile2.newInstanceFromMovieTags(movieTagsItem));
ft.commit();
}
这是我的片段类,其中调用和执行对象。问题是我无法在onCreatView
中定义布局public class MovieProfile2 extends Fragment {
private static final String TAG = MovieProfile2.class.getSimpleName();
MovieProfile movieInfo;
public static MovieProfile2 newInstanceFromMovieTags(MovieTagsItem movieTagsItem) {
Bundle args = new Bundle();
args.putSerializable("movieTag", movieTagsItem);
MovieProfile2 fragment = new MovieProfile2();
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.layout_movie_profile2, container,false);
Log.d(TAG, "layout is working: "+ inflater.inflate(R.layout.layout_movie_profile2, container,false));
MovieTagsItem movieTagsItem = (MovieTagsItem)getArguments().getSerializable("movieTag");
Log.d(TAG, "Movie Id: "+ movieTagsItem.getMovieId());
int id = movieTagsItem.getMovieId();
return view;
}
}
这是我的片段布局,它应该在从活动类执行onclick操作后膨胀,其中需要数据即将到来,但布局没有显示
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_movie_tag_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_movie_tag_Movie_Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="4dp"
android:maxLines="1"
android:text="@string/movie_name"
android:textSize="25sp">
</TextView>
<TextView
android:id="@+id/tv__movie_tag_Name_Nepali"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="4dp"
android:maxLines="1"
android:text="@string/name_nepali"
android:textSize="20sp">
</TextView>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_movie_tag_released_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:maxLines="2"
android:text="@string/released_date"
android:textSize="12sp"
/>
<TextView
android:id="@+id/tv_movie_tag_run_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginStart="100dp"
android:layout_marginTop="8dp"
android:text="@string/run_time"
android:layout_toRightOf="@+id/tv_released_date"
android:maxLines="1"
android:textSize="12sp"/>
</FrameLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#c0c0c0"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="110dp"
android:layout_height="160dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
>
<android.support.v7.widget.AppCompatImageView
android:id="@+id/movie_tag_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/ic_action_action_search" />
</android.support.v7.widget.CardView>
<TextView
android:id="@+id/tv_movie_tag_synopsis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginStart="120dp"
android:layout_marginTop="8dp"
android:layout_toRightOf="@+id/movie_image"
android:ellipsize="none"
android:scrollHorizontally="false"
android:text="@string/synopsis"
android:textSize="13sp" />
</FrameLayout>
</LinearLayout>
</ScrollView>
答案 0 :(得分:0)
您正在尝试替换 news_details 片段,但在您的片段XML中,没有任何内容与 layout_movie_profile2 的ID相匹配强>
答案 1 :(得分:0)
在联系人ID中定义xml文件中将此传递ID转换为news_details,如下面的代码..
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_main"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activities.MainActivity"
tools:showIn="@layout/app_bar_main">
</FrameLayout>
然后将id定义为替换片段..
ft.replace(R.id.content_main, MovieProfile2.newInstanceFromMovieTags(movieTagsItem)).addToBackStack(null).commit();