java - 使用 Recycle View 时无法在 Android Emulator 中看到应用程序栏
问题描述
我正在尝试RecyclerView
使用 Android进行设置Fragments
。我无法在我的片段中看到应用栏。作为概念证明,我创建了 100 个 POJO that I want to display in my
RecyclerView`。我能够看到 100 个元素,但我无法查看应用栏。
我不知道我做错了什么。这是在模拟器中呈现的视图。我使用的是标准活动 API,而不是支持库中的 API。
这是我对布局检查器的看法。
主题
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
安卓清单.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.fragments">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".CrimeListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
活动.java
public class CrimeListActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getFragmentManager();
Fragment fragment =
fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = new CrimeListFragment();
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
}
}
}
activity_fragment.xml(Recycler View 将被添加到这个容器中)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
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:id="@+id/crime_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize"/>
list_item_crime.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/crime_title"
android:text="@string/crime_title_hint"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/crime_date"
android:text="@string/crime_date"/>
</LinearLayout>
POJO.java
public class Crime {
private final UUID mId;
private String mTitle;
private final Date mDate;
private boolean mSolved;
public Crime() {
this.mId = UUID.randomUUID();
this.mDate = new Date();
}
public UUID getmId() {
return mId;
}
public String getmTitle() {
return mTitle;
}
public void setmTitle(String mTitle) {
this.mTitle = mTitle;
}
public Date getmDate() {
return mDate;
}
public boolean ismSolved() {
return mSolved;
}
public void setmSolved(boolean mSolved) {
this.mSolved = mSolved;
}
}
查看 Holder.java
private static class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView mCrimeTitle;
private TextView mCrimeDate;
private Crime mCrime;
public CrimeHolder(View view) {
super(view);
this.mCrimeTitle = view.findViewById(R.id.crime_title);
this.mCrimeDate = view.findViewById(R.id.crime_date);
this.itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), mCrime.getmTitle() + " clicked!!", Toast.LENGTH_SHORT).show();
}
void bind(Crime crime) {
mCrime = crime;
mCrimeTitle.setText(mCrime.getmTitle());
mCrimeDate.setText(mCrime.getmDate().toString());
}
}
适配器.java
private static class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
private List<Crime> mCrimes;
public CrimeAdapter(List<Crime> crimes) {
this.mCrimes = crimes;
}
@NonNull
@Override
public CrimeHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
View view = layoutInflater.inflate(R.layout.list_item_crime, viewGroup, false);
return new CrimeHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CrimeHolder crimeHolder, int i) {
Crime crime = this.mCrimes.get(i);
crimeHolder.bind(crime);
}
@Override
public int getItemCount() {
return mCrimes.size();
}
}
分段
public class CrimeListFragment extends Fragment {
private RecyclerView mRecyclerView;
private CrimeAdapter mAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
mRecyclerView = view.findViewById(R.id.crime_recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}
private List<Crime> getCrimes() {
List<Crime> mCrimes = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Crime crime = new Crime();
crime.setmTitle("Title # :" + i);
crime.setmSolved(i % 2 == 0);
mCrimes.add(crime);
}
return mCrimes;
}
private void updateUI() {
List<Crime> crimes = getCrimes();
mAdapter = new CrimeAdapter(crimes);
mRecyclerView.setAdapter(mAdapter);
}
}
解决方案
推荐阅读
- ruby - 根据每个元素中的给定字符集对数组进行排序
- html - 如何使用 HTML 和 CSS 将文本放在幻灯片前面?
- node.js - Angular 和 Node.js 套接字 IO 问题
- c - 为什么不打印这个数组?
- plsql - ORA-06512 和 ORA-01403 未找到数据
- r - 如何在 spat stat 中创建一个 3 维圆形窗口?
- python - 如何将浮点数列表分为两组进行优化,以使每组总和之间的差异最小化?
- python - 创建一个多维 numpy 数组,其中数组的每个维度的长度为 L
- c# - “/”应用程序中的服务器错误。无法找到该资源。(404) 将 WCF 服务部署到 Azure 应用服务时
- c++ - 线程创建后 C/C++ DLL 调用者的访问冲突