java - 使用 Recyclerview 时无法在 Android 模拟器中看到应用程序栏

标签 java android android-recyclerview

我正在尝试使用 Android Fragments 设置 RecyclerView。我无法在 fragment 中看到应用栏。作为概念证明,我创建了 100 个 POJO,我想在我的RecyclerView` 中显示它们。我可以看到 100 个元素,但无法查看应用栏。

我不知道我做错了什么。这是在模拟器中呈现的 View 。我使用的是标准 Activity API,而不是支持库中的 API。

Recycler View without app bar

这是我对布局检查器的看法。

Layout Inspector

主题

 <!-- 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>

安卓 list .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>

Activity .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();
    }
  }

fragment

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);
  }
}

最佳答案

如果将主 Activity 更改为 AppCompatActivity,您将获得操作栏,并且不需要 fragment_crime_list.xml 中的 paddingTop。我猜问题出在您将普通 Activity 与 AppCompat 主题组合在一起。

(我试过了)

关于java - 使用 Recyclerview 时无法在 Android 模拟器中看到应用程序栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52157495/

相关文章:

java.lang.AssertionError : No ModelAndView found using webAppContextSetup

java - 如何使用JAVA从加密狗中的sim读取短信

java - org.glassfish.jersey.client.ClientProperties 超时在 WildFly 上不起作用

java - Hibernate 对 Android 应用程序来说是不是太过分了?

java - Android : Error not telling me anything I can work out

Android recyclerview 分隔项 4.4 兼容性

android - 如何检查Recyclerview是否为空

java - 轮盘预测

android - 在 AsyncTask 完成时完成调用 Activity

java - findViewByID 在 FirestoreRecyclerAdapter 中返回 null