如果我的recyclerView没有加载,我试图将TextView设置为空视图,并且我无法使setEmptyView工作。它说无法解析方法setEmptyView。这是代码:
@EnableWebSecurity
@Configuration
public class RetailerSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().ignoringAntMatchers("/login/**")
.and()
.authorizeRequests()
.antMatchers("/login", "/login/reset", "/resources/**", "/balance/getResult/**").permitAll()
.antMatchers("/retailers/**", "/balance/**").access("hasRole('ROLE_ADMIN')")
.antMatchers("/tellers/**", "/tellers").hasAnyAuthority("PRIMARY")
.antMatchers("/sell/**", "/sell").hasAnyAuthority("TELLER")
.anyRequest().fullyAuthenticated()
.and()
.formLogin()
.loginPage("/login")
.successForwardUrl("/login/success")
.permitAll()
.and()
.logout()
.permitAll();
}
}
我在片段中,这可能是为什么?
答案 0 :(得分:3)
RecyclerView不支持setEmptyView。可以使用这个类。
public class RecyclerViewEmptyView extends RecyclerView {
private View emptyView;
private AdapterDataObserver emptyObserver = new AdapterDataObserver() {
@Override
public void onChanged() {
Adapter<?> adapter = getAdapter();
if(adapter != null && emptyView != null) {
if(adapter.getItemCount() == 0) {
emptyView.setVisibility(View.VISIBLE);
RecyclerViewEmptySupport.this.setVisibility(View.GONE);
}
else {
emptyView.setVisibility(View.GONE);
RecyclerViewEmptySupport.this.setVisibility(View.VISIBLE);
}
}
}
};
public RecyclerViewEmptySupport(Context context) {
super(context);
}
public RecyclerViewEmptySupport(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RecyclerViewEmptySupport(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setAdapter(Adapter adapter) {
super.setAdapter(adapter);
if(adapter != null) {
adapter.registerAdapterDataObserver(emptyObserver);
}
emptyObserver.onChanged();
}
public void setEmptyView(View emptyView) {
this.emptyView = emptyView;
}
}
或者你可以
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:text="No Data Available" />
Java代码:
private RecyclerView recyclerView;
private TextView emptyView;
// ...
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
emptyView = (TextView) rootView.findViewById(R.id.empty_view);
// ...
if (dataset.isEmpty()) {
recyclerView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
}
else {
recyclerView.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.GONE);
}
答案 1 :(得分:2)
首先,您必须将xml中的emptyView
设置为android:visibility="gone"
。
<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:text="There is no item" />
其次,如果您检索的数据集为空,则必须将recyclerview的可见性设置为已消失。
private RecyclerView recyclerViewUser1 ;
private TextView emptyStateTextView ;
// ...
recyclerViewUser1 = (RecyclerView) rootView.findViewById(R.id.recycler_view_red_room1);
emptyStateTextView = (TextView) rootView.findViewById(R.id.empty_view);
// ...
if (dataset.isEmpty()) {
recyclerViewUser1 .setVisibility(View.GONE);
emptyStateTextView .setVisibility(View.VISIBLE);
}
else {
recyclerViewUser1 .setVisibility(View.VISIBLE);
emptyStateTextView .setVisibility(View.GONE);
}