在GridLayout的2行之间添加一些空间

时间:2018-09-08 07:54:40

标签: java android

This is the pattern i want to implement with

// here is my grid adapter
public class TimeSlotAdapter  extends RecyclerView.Adapter {

    private LayoutInflater mInflater;
    private String strSelectedDate;
     List<Boolean> numberList;
    private List<String> mData;
    private  TextView timetv;
    List<RelativeLayout> activeTimeSlotsArray=new ArrayList<RelativeLayout>();

       // data is passed into the constructor
    public TimeSlotAdapter(Context context, List<String> data, String strSelectedDate,TextView timetv,List<Boolean> numberList) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
        this.strSelectedDate = strSelectedDate;
        this.timetv=timetv;
        this.numberList=numberList;

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
        return new listViewHolder(view);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
       if(position==5||position==11){
           RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
           params.setMargins(0,0,0,100);
           ((listViewHolder)holder).timeslot.setLayoutParams(params);
       }

                           ((listViewHolder) holder).radioButton.setText(mData.get(position));
                    ((listViewHolder)holder).radioButton.setTextColor(R.color.inactiveslots);
                    ((listViewHolder) holder).timeslot.setBackgroundResource(R.drawable.inactive_slots);

                       }

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

    private class listViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        RadioButton radioButton;
        RelativeLayout timeslot;
        String strStartTime;
        int selectedPosition;

        public listViewHolder(View itemView) {
            super(itemView);
            radioButton = itemView.findViewById(R.id.radioBtton);
            timeslot = itemView.findViewById(R.id.mslot1);
            timeslot.setOnClickListener(this);
            radioButton.setOnClickListener(this);
        }
}

当我尝试给空间button的位置5和11更改时。这里我给gridlayout的空间,但是按钮的大小我不想更改应该具有相同的大小。另外,我需要rows中的两个grid之间的空间

1 个答案:

答案 0 :(得分:0)

尝试一下:

   LayoutParams params = ((listViewHolder)holder).timeslot.getLayoutParams();
   params.setMargins(0,0,0,100);
   ((listViewHolder)holder).timeslot.setLayoutParams(params);

并检查R.layout.grid_item文件是否radioButton layout_width属性为wrap_content