我正在尝试使用Android RecyclerView
设置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>
Android Manifest.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(“回收者视图”将添加到此容器中)
<?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());
}
}
Adapter.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);
}
}
答案 0 :(得分:1)
如果将主Activity更改为AppCompatActivity,则将获得操作栏,并且不需要fragment_crime_list.xml中的paddingTop。我猜是问题在于您将正常的Activity与AppCompat主题结合了。
(我尝试过)
答案 1 :(得分:0)
即使您将DarkActionBar用作父主题,我也不知道为什么它不会显示在您的应用中。但是您可以使用this guide手动添加应用栏。
您需要:
修改清单,以确保android不会为您创建应用栏
<application
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
...
然后在布局中添加工具栏小部件
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
然后在您的活动课程中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}