我有谷歌地图和群集数据,当我点击某个群集然后显示水平回收查看。我有一个imageButton,它是CardView中的下一个或上一个按钮,当我点击它然后cardView滚动到下一个位置。它工作得很完美,但我有一个问题。我的第一个cardView和最后一个CardView应该没有这个图像按钮,所以我尝试设置Visibility to Gone,如下所示:
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中消失,但也在第一张或最后一张的cardView中消失,而最后一张cardview没有prev和下一张图片按钮,但应该有prev image按钮。 那么我怎样才能在所有cardView中使用next和prev图像按钮而不是第一个应该只有next而且last应该只有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);
}
}
}
}
答案 0 :(得分:0)
只需将div_object_last
添加到holder.nextImageButton.setVisibility(View.VISIBLE);
声明
<强> WHY 强>:
因为RecyclerView正在重用视图。一旦视图从顶部移出屏幕,它将移动到底部以表示下一个列表项。由于您已将按钮隐藏在其中,因此无法自动显示该按钮,您需要手动执行此操作。