我正在关注Big nerd ranch的安卓手册(第二版)。下面的代码应该显示一个从CrimeLab类生成的模型数据列表(100个犯罪分别带有标题,日期和已解决的复选框)。在CrimeListFragment中使用recyclelerView来显示数据。但是建筑和建筑运行应用程序,我得到的只是一个空白的白色屏幕。 这是相关代码:
public class CrimeListActivity extends FragmentActivity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_host);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = new CrimeListFragment();
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
}
}}
CrimeListFragment.java
public class CrimeListFragment extends Fragment {
private RecyclerView mCrimeRecyclerView;
private CrimeAdapter mAdapter;
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_crime_list,container,false);
mCrimeRecyclerView = (RecyclerView) view.findViewById(R.id.crime_recycler_view);
mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}
private void updateUI(){
CrimeLab crimeLab = CrimeLab.get(getActivity());
List<Crime> crimes = crimeLab.getCrimes();
mAdapter = new CrimeAdapter(crimes);
mCrimeRecyclerView.setAdapter(mAdapter);
}
public class CrimeHolder extends RecyclerView.ViewHolder{
public TextView mTitleTextView;
private TextView mTitleTextView;
private TextView mDateTextView;
private CheckBox mSolvedCheckBox;
private Crime mCrime;
public CrimeHolder (View itemView){
super(itemView);
// mTitleTextView = (TextView) itemView;
mTitleTextView =(TextView) itemView.findViewById(R.id.list_item_crime_title_text_view);
mDateTextView =(TextView) itemView.findViewById(R.id.list_item_crime_date_text_view);
mSolvedCheckBox = (CheckBox) itemView.findViewById(R.id.list_item_crime_solved_check_box);
}
public void bindCrime(Crime crime){
mCrime = crime ;
mTitleTextView.setText(mCrime.getTitle());
mDateTextView.setText(mCrime.getDate().toString());
mSolvedCheckBox.setEnabled(mCrime.isSolved());
}
}
public class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder>{
private List<Crime> mCrimes;
public CrimeAdapter(List<Crime> crimes){
mCrimes = crimes;
}
@Override
public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType){
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View view = layoutInflater.inflate(R.layout.list_item_crime,parent,false);
return new CrimeHolder(view) ;
}
@Override
public void onBindViewHolder(CrimeHolder holder,int position){
Crime crime = mCrimes.get(position);
holder.bindCrime(crime);
}
@Override
public int getItemCount(){
return mCrimes.size();
}
}
以防万一,CrimeLab.java :(提供模型数据)
public class CrimeLab {
private static CrimeLab sCrimeLab;
private List<Crime> mCrimes;
public static CrimeLab get(Context context){
if (sCrimeLab==null){
sCrimeLab = new CrimeLab(context); //Hence calling the private constructor that no other class can access
}
return sCrimeLab;
}
private CrimeLab(Context context){
mCrimes = new ArrayList<>();
for (int i=0; i<100; i++){
Crime crime = new Crime();
crime.setTitle("Crime #"+i);
crime.setSolved(i%2==0);
mCrimes.add(crime);
}
}
public List<Crime> getCrimes(){
return mCrimes;
}
content_fragment_host.xml(这个是由activity_fragment_host.xml编写的,我从中删除了默认的AppBarLayout&amp; Toolbar代码,如果有帮助的话)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
tools:context="com.bignerdranch.android.crimeintent.CrimeListActivity"
tools:showIn="@layout/activity_fragment_host"
android:id="@+id/fragment_container">
</FrameLayout>
fragment_crime_list.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/crime_recycler_view">
</android.support.v7.widget.RecyclerView>
list_item_crime.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list_item_crime_solved_check_box"
android:layout_alignParentRight="true"
android:padding="4dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_item_crime_title_text_view"
android:layout_toLeftOf="@+id/list_item_crime_solved_check_box"
android:textStyle="bold"
android:padding="4dp"
tools:text="Title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_item_crime_date_text_view"
android:layout_toLeftOf="@+id/list_item_crime_solved_check_box"
android:layout_below="@id/list_item_crime_title_text_view"
android:padding="4dp"
tools:text="Date"/>
</RelativeLayout>
答案 0 :(得分:0)
这只是一个错字。 onCreateView已更改为CrimeListFragment.java中的OnCreateView。但是,谢谢,无论如何,这让我发疯了。
答案 1 :(得分:-2)
每次使用片段并想要显示或替换为另一个时,您需要从FragmentManager调用commit()。在你的情况下将是fm.commit();
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = new CrimeListFragment();
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
}
fm.commit();