如何使回收视图的标题不可见

时间:2016-02-01 07:51:16

标签: android android-recyclerview material

我有一个带有header view as slider images的Recyclerview,剩下的视图为normal recycler items。我想知道是否有任何方法可以根据某种情况使标题视图不可见。回收者视图包含为此目的使用两个单独的布局文件:标题项为layout1,普通回收项为layout2,适配器将选择布局并在运行时绑定相应的数据。

这是我的RecyclerView适配器RestaurantAdapter.java

    public class RestaurantAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private static final String TAG = RestaurantAdapter.class.getName();
        private List<Restaurant> mList;
        private Context mContext;
        private RestaurantType mRestaurantType;
        private static final int RECYCLER_HEADER=0,RECYCLER_ITEMS=1;
        private LayoutInflater inflater;
        private SlideItemViewHolder slideItemViewHolder;
        private List<ImageSliderPOJO> mData;

        public RestaurantAdapter(Context context, List<Restaurant> list, RestaurantType restaurantType) {
            this.mContext = context;
            this.mList = list;
            this.mRestaurantType = restaurantType;
            inflater=LayoutInflater.from(context);

        }

        public void updateAdapter(List<ImageSliderPOJO> data){
            this.mData = data;
            this.notifyDataSetChanged();
        }


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

        @Override
        public int getItemViewType(int position) {
            return position == 0 ? RECYCLER_HEADER : RECYCLER_ITEMS;
        }

        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int i) {
            int viewType=viewHolder.getItemViewType();
            switch (viewType){
                case RECYCLER_HEADER:
                    slideItemViewHolder = (SlideItemViewHolder) viewHolder;
                    slideItemViewHolder.updateHeader();
                    break;

                case RECYCLER_ITEMS:
                    final RecyclerItemViewHolder holder = (RecyclerItemViewHolder) viewHolder;
                    final Restaurant restaurant = mList.get(i);
                    Picasso.with(mContext)
                            .load(restaurant.getVendorLogo())
                            .placeholder(R.mipmap.ic_launcher)
                            .error(R.mipmap.ic_launcher)
                            .into(holder.restaurentImageView);

                    holder.restaurentNameTextView.setText(restaurant.getName());
                 //Remaining code here

                    break;

                default:
                    throw new RuntimeException(TAG+":Unable to bind the viewType"+viewType);

            }
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

            switch (viewType){
                case RECYCLER_HEADER:
                    return new SlideItemViewHolder(inflater.inflate(R.layout.slide_show_restaurant_fragment_list,viewGroup,false));

                case RECYCLER_ITEMS:
                    return new RecyclerItemViewHolder(inflater.inflate(R.layout.new_restautant_list_items, viewGroup, false));

                default:
                  throw new RuntimeException(TAG+":Invalid ViewType "+viewType);
            }

        }

        public static class RecyclerItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            private ClickListener clickListener;
             //Initialization here.


            public RecyclerItemViewHolder(View v) {
                super(v);
                ButterKnife.inject(this, v);
                v.setOnClickListener(this);


            }

            @Override
            public void onClick(View v) {
                clickListener.onClick(v, getPosition(), false);
            }

            public interface ClickListener {

                /**
                 * Called when the view is clicked.
                 *
                 * @param v           view that is clicked
                 * @param position    of the clicked item
                 * @param isLongClick true if long click, false otherwise
                 */
                public void onClick(View v, int position, boolean isLongClick);

            }

            /* Setter for listener. */
            public void setClickListener(ClickListener clickListener) {
                this.clickListener = clickListener;
            }
        }


        // id = 87,170

        private class SlideItemViewHolder extends RecyclerView.ViewHolder {
            SliderLayout sliderLayout;
            LinearLayout rootLinearLayout;
            public SlideItemViewHolder(View recyclerHeader) {
                super(recyclerHeader);
                sliderLayout = (SliderLayout) recyclerHeader.findViewById(R.id.home_slider);
                rootLinearLayout = (LinearLayout) recyclerHeader.findViewById(R.id.rootLinearLayout);
            }

            private void updateHeader() {
                if(Util.isNetworkAvailable(mContext)){
                    for (int i = 0; i < mData.size(); i++) {
                        DefaultSliderView defaultSliderView = new DefaultSliderView(mContext);
                        final int finalI = i;
                        defaultSliderView.image(mData.get(finalI).getImageUrl())
                                .setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                                    @Override
                                    public void onSliderClick(BaseSliderView slider) {
                                        Restaurant restaurantById = Restaurant.searchByRestaurantId(mData.get(finalI).getTargetVendorId());
                                        if(restaurantById != null)
                                            openDetailFragment(restaurantById);
                                    }
                                });
                        sliderLayout.addSlider(defaultSliderView);
                    }
                }


            }

        }


        public void openDetailFragment(Restaurant restaurant) {
            Intent intent = new Intent(mContext, DetailTabActivity.class);
            intent.putExtra(DetailTabActivity.INTENT_RESTAURANT_DATA, restaurant);
            mContext.startActivity(intent);

        }

        public SliderLayout getSliderLayout(){
            return slideItemViewHolder.sliderLayout;
        }
    }

此适配器已从此片段RestaurantFragment.java设置并更新为:

private void setAdapter()  {
        dismissDialog();
        if (getActivity() != null)
            getActivity().runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    if (restaurantList != null && restaurantList.size() > 0) {
                        restaurantRecyclerView.setVisibility(View.VISIBLE);
                        mEmptyListTextView.setVisibility(View.GONE);
                        restaurentListAdapter = new RestaurantAdapter(getActivity(), restaurantList, mRestaurantType);
                        restaurantRecyclerView.setAdapter(restaurentListAdapter);
                        restaurentListAdapter.updateAdapter(mData);

                        restaurantRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) {
                            @Override
                            public void onLoadMore(int current_page) {

                                mCurrentPage = mCurrentPage + 1;
                                getHttpResturantData();
                            }
                        });
                    }
                }
            });
    }

这些解释是否有用,还是应该粘贴更多代码?

1 个答案:

答案 0 :(得分:2)

根据您的代码,可以根据特定条件删除标题。

调整代码以满足以下条件:

btnEjecutar.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
        //the function
        generarDocumento(nombre);
            }

此代码也需要更改(目前这是错误的,因为它没有考虑标题为位置添加1的事实。)

@Override
public int getItemViewType(int position) {
    if(hasHeaeder()) { // where you add the header
        return position == 0 ? RECYCLER_HEADER : RECYCLER_ITEMS;
    } else {  // where you don't add the header
       return RECYCLER_ITEMS;
    }
}

将其替换为

final Restaurant restaurant = mList.get(i);

final Restaurant restaurant = hasHeader() ? mList.get(i +1) : mList.get(i); 是您需要编写的代码,以确定回收者是否应该包含标题。