android - 无法触发 recyclerview 的空 View

标签 android android-recyclerview

我有一个 recyclerview,我想在它为空时显示文本/图像,但我找不到让它工作的方法。

回收 View xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/list"
        android:name="ViolationFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        app:layoutManager="LinearLayoutManager"
        tools:context=".ViolationFragment"
        tools:listitem="@layout/fragment_violation" />

    <TextView
        android:id="@+id/list_empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Empty" />
</android.support.v4.widget.SwipeRefreshLayout>

fragment

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    context = getActivity();

    View view = inflater.inflate(R.layout.fragment_violation_list, container, false);
    swipeContainer = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
    swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            // Refresh items
            updateList();
        }
    });

    //Filters
    filterPaid = ((ViolationBrowser) context).filterPaid;
    filterUnpaid = ((ViolationBrowser) context).filterUnpaid;

    recyclerView = (RecyclerView) view.findViewById(R.id.list);
    TextView emptyView = (TextView) view.findViewById(R.id.list_empty);

    adapter = new ViolationAdapter(context, getData());
    adapter.SetOnItemClickListener(new ViolationAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View v, int position) {
            showDetailedViolationData(position);
        }
    });

    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    if (adapter.getItemCount() >0) {
        recyclerView.setVisibility(View.GONE);
        emptyView.setVisibility(View.VISIBLE);
    } else {
        recyclerView.setVisibility(View.VISIBLE);
        emptyView.setVisibility(View.GONE);
    }
    return view;
}

适配器

public class ViolationAdapter extends RecyclerView.Adapter<ViolationAdapter.MyViewHolder> {

    OnItemClickListener mItemClickListener;
    List<ViolationItem> data = Collections.emptyList();
    private LayoutInflater inflater;

    public ViolationAdapter(Context context, List<ViolationItem> data) {
        inflater = LayoutInflater.from(context);
        this.data = data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.fragment_violation, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        ViolationItem current = data.get(position);
        holder.carName.setText(current.carName);
        holder.amount.setText(current.amount);
        holder.date.setText(current.date);

        if (current.payStatus == 2) holder.payStatus.setImageResource(R.drawable.status_paid);
        else if (current.payStatus == 1)
            holder.payStatus.setImageResource(R.drawable.status_part_paid);
        else holder.payStatus.setImageResource(R.drawable.status_not_paid);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    public ViolationItem getItem(int position) {
        return data.get(position);
    }

    public void addData(List<ViolationItem> data) {
        for (ViolationItem itm : data) {
            this.data.add(itm);
            notifyDataSetChanged();
        }
    }

    public void clearData() {
        int size = data.size();
        data.clear();
        notifyItemRangeRemoved(0, size);
    }

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView carName, amount, date;
        ImageView payStatus, showAllDetails;

        public MyViewHolder(View itemView) {
            super(itemView);
            carName = (TextView) itemView.findViewById(R.id.itemCarName);
            amount = (TextView) itemView.findViewById(R.id.itemAmount);
            date = (TextView) itemView.findViewById(R.id.itemDate);
            payStatus = (ImageView) itemView.findViewById(R.id.payStatus);
            showAllDetails = (ImageView) itemView.findViewById(R.id.showAllDetails);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) mItemClickListener.onItemClick(v, getLayoutPosition());
        }
    }
}

我应该怎么做才能让它发挥作用?我仍然看到一个空列表而不是文本

最佳答案

在你的例子中,下面的条件应该是这样的:

if (adapter.getItemCount() <=0) 
{
    recyclerView.setVisibility(View.GONE);
    emptyView.setVisibility(View.VISIBLE);
} 
else
{
    recyclerView.setVisibility(View.VISIBLE);
    emptyView.setVisibility(View.GONE);
}

此外,在 xml 布局中:

 <TextView
    android:id="@+id/list_empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Empty" />

将“list_empty”放在 SwipeRefreshLayout 之外。

请参阅下面的完整示例: 如果回收站 View 为空,则显示空 View 。

public class MainActivity extends AppCompatActivity {

SwipeRefreshLayout mSwipeRefreshLayout;
RecyclerView recyclerView;
List<String> arrayList = null;
MyAdapter mAdapter = null;
TextView list_empty;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
    recyclerView = (RecyclerView)findViewById(R.id.itemsRecyclerView);
    list_empty = (TextView)findViewById(R.id.list_empty);
    arrayList = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.mobile)));
    recyclerView.setLayoutManager(new LinearLayoutManager(this));


    mAdapter = new MyAdapter((ArrayList<String>)arrayList);

    recyclerView.setAdapter(mAdapter);       

    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            refreshContent();
        }
    });

}

private void refreshContent(){
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

           arrayList.clear();
            mAdapter.notifyDataSetChanged();

            if(mAdapter.getItemCount()<=0)
            {
                recyclerView.setVisibility(View.GONE);
                list_empty.setVisibility(View.VISIBLE);
            }
            else
            {
                recyclerView.setVisibility(View.VISIBLE);
                list_empty.setVisibility(View.GONE);
            }
            // Stop refresh animation
            mSwipeRefreshLayout.setRefreshing(false);
        };
    },2000);
}

class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private ArrayList<String> itemsData;

    public MyAdapter(ArrayList<String> itemsData) {
        this.itemsData = itemsData;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View itemLayoutView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item, null);

        // create ViewHolder

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

   viewHolder.txtViewTitle.setText(itemsData.get(position));


    }

    // inner class to hold a reference to each item of RecyclerView
    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView txtViewTitle;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            txtViewTitle = (TextView)
itemLayoutView.findViewById(R.id.item_title);
        }
    }


    // Return the size of your itemsData (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return itemsData.size();
    }
  }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context="com.test.demotest.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/itemsRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"

        />
</android.support.v4.widget.SwipeRefreshLayout>

    <TextView
        android:id="@+id/list_empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Empty"
        android:layout_gravity="center"
        android:visibility="gone"/>

</FrameLayout>
</RelativeLayout>

项目.xml:

<!-- title -->
<TextView
    android:id="@+id/item_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/darker_gray"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:textSize="22dp" />

</RelativeLayout>

关于android - 无法触发 recyclerview 的空 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37574545/

相关文章:

android - Sqlite 命令用单个号码检索多条消息

java - LibGDX And​​roid 首选项 - 如何将数据保存到应用程序

android - Android 上有 apktool 吗?我想在 Android 应用程序中组装 ".smali"并制作新的 ".apk"

Android:RecyclerView 滚动问题

android - 从适配器 RecyclerView 打开 fragment

java - 如何取消recyclerView滚动?

Android:从服务器检索图像

android - ListView可以出现在Activity中,但不能出现在Fragment中

android - 当 EditText 处于焦点时无法在 RecyclerView 上通知 DataSetChanged

android - RecyclerView notifyItemMoved() 不更新位置