如何在RecyclerView中设置可见性小部件 [英] How set visibility widget in recyclerView

查看:63
本文介绍了如何在RecyclerView中设置可见性小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有谷歌地图和集群数据,当我单击某个集群时,它显示水平recyclerview.我有imageButton这是CardView中的下一个或上一个按钮,当我单击它时,cardView会滚动到下一个位置.它工作完美,但我有一个问题.我的第一个CardView和最后一个CardView应该没有此图像按钮,因此我尝试将可见性"设置为消失",如下所示:

I have google maps and cluster data, when i click to some cluster then is showing horizontal recyclerview. I have imageButton which is next or previous button in CardView, when i click it then cardView are scroll to next position. It works perfect, but i have one problem. My first cardView and the last CardView should'nt have this image button so I try set Visibility to Gone like this:

private void nextImageButtonAction(final ViewHolder holder, final int position, MyClusterMarkerMapItem cluster){
                if(position == clusterDataList.size()-1){
                    Log.i("position", "position: " + position + " visibility GONE for next");
                    holder.nextImageButton.setVisibility(View.GONE);
                }else {
                    holder.nextImageButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int currentPosition = position;
                            currentPosition = currentPosition + 1;
                           // Log.i("pos", "currpos " + position + " nextPos " + currentPosition);
                            recyclerView.smoothScrollToPosition(currentPosition++);
                        }

                    });
                }
    }

    private void previousImageButtonAction(final ViewHolder holder, final int position, MyClusterMarkerMapItem cluster) {
        if (position == 0) {
            Log.i("position", "position: " + position + " visibility GONE for prev");
            holder.previousImageButton.setVisibility(View.GONE);
        } else {
            holder.previousImageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int currentPosition = position;
                    currentPosition = currentPosition - 1;
                    //Log.i("pos", "currpos " + position + " nextPos " + currentPosition);
                    recyclerView.smoothScrollToPosition(currentPosition++);
                }
            });
        }
    }

我在

  public void onBindViewHolder(final ViewHolder holder, final int position) {..My Method}

但是imageButton在第一个cardView中消失,但在不是first或last的cardView中消失,并且最后一个cardview没有上一个和下一个图像按钮,但是应该具有上一个图像按钮. 因此,如何在所有cardView中都具有下一个和上一个图像按钮,而在第一个中应该只有下一个,而最后一个应该只有上一个

But the imageButton disappear in first cardView but also in the cardView which isn't first or last, and the last cardview doesn't have prev and next image button but should have prev image button. So how can I have next and prev image button in all cardView instead in first should have only next and the last should have only prev

我的适配器以及更多:

    public class ClusterInfoWindow extends Fragment {
    private MyClusterMarkerMapItem clusterData;
    private List<MyClusterMarkerMapItem> clusterDataList;
    public void setClusterData(MyClusterMarkerMapItem clusterData) {
        clusterDataList = new ArrayList<>();
        this.clusterData = clusterData;
    }
    public void setClusterDataList(List<MyClusterMarkerMapItem> clusterDataList) {
        this.clusterDataList = new ArrayList<>(clusterDataList);
    }
    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapterRecyclerView;
    private RecyclerView.LayoutManager layoutManager;

    private  View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_info_window_marker_map, container, false);
        setUpCardView(rootView);
        return rootView;
    }
    private void setUpCardView(View rootView){
        recyclerView = rootView.findViewById(R.id.recyclerViewInfoWindowMarker);
        layoutManager = new LinearLayoutManager(rootView.getContext(),LinearLayoutManager.HORIZONTAL,false); //horizontal
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        adapterRecyclerView = new AdapterRecyclerView(getActivity());
        recyclerView.setAdapter(adapterRecyclerView);
    }

    class AdapterRecyclerView extends RecyclerView.Adapter<AdapterRecyclerView.ViewHolder> {
        private Context context;
        public AdapterRecyclerView(Context context){
            this.context = context;
        }
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_for_info_window_map,parent,false);
            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
//some function, some i put above in this post
}
 @Override
        public int getItemCount() {
            int itemCount = 0;
            if (clusterDataList.size() == 0)
                itemCount = 1;
            else if(clusterDataList.size() > 0 )
                itemCount = clusterDataList.size();
            return itemCount;
        }
        public  class ViewHolder extends RecyclerView.ViewHolder{
            private ImageButton nextImageButton;
            private ImageButton previousImageButton;

            public ViewHolder(View itemView) {
                super(itemView);

                nextImageButton = itemView.findViewById(R.id.cardViewNext);
                previousImageButton = itemView.findViewById(R.id.cardVievPrevious);

            }
        }

    }

}

推荐答案

只需将holder.nextImageButton.setVisibility(View.VISIBLE);添加到else语句

为什么?:
因为RecyclerView正在重用视图.一旦视图从顶部移出屏幕,它将移至底部以表示下一个列表项.而且,由于您已将按钮隐藏在其中,因此它不会自动显示,因此您需要手动进行操作.

WHY?:
Because RecyclerView is reusing views. Once the view is out of the screen from top, it is moved to the bottom to represent the next list item. And since you have hidden the button in it, it won't automatically show it, you need to do that manually.

这篇关于如何在RecyclerView中设置可见性小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆