在Recycler GridLayoutManager中为3到4行项目提供更多间距

时间:2016-02-08 19:53:41

标签: android android-custom-view android-recyclerview recycler-adapter gridlayoutmanager

我使用RecyclerView和GridLayoutManager作为公交车票预订的布局。一切都很好,但除了我必须使用的逻辑,我必须在所有行中留出3到4列项目之间的间距。基本上我的布局应该像下面的截图一样。

我的要求:

enter image description here

当前输出:

enter image description here

我还发布了完整的RecyclerView GridLayoutManager供您参考,如下所示

MultiSelectRecyclerViewAdapter.java

public class MultiSelectRecyclerViewAdapter extends SelectableAdapter<MultiSelectRecyclerViewAdapter.ViewHolder> {

    private ArrayList<String> mArrayList;
    private Context mContext;
    private ViewHolder.ClickListener clickListener;



    public MultiSelectRecyclerViewAdapter (Context context, ArrayList<String> arrayList,ViewHolder.ClickListener clickListener) {
        this.mArrayList = arrayList;
        this.mContext = context;
        this.clickListener = clickListener;

    }

    // Create new views
    @Override
    public MultiSelectRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                             int viewType) {

        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.row_multiselect, null);

        ViewHolder viewHolder = new ViewHolder(itemLayoutView,clickListener);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

        viewHolder.tvName.setText(mArrayList.get (position));

        viewHolder.selectedOverlay.setVisibility(isSelected(position) ? View.VISIBLE : View.INVISIBLE);

    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder  implements View.OnClickListener,View.OnLongClickListener  {

        public TextView tvName;
        private ClickListener listener;
        private final View selectedOverlay;


        public ViewHolder(View itemLayoutView,ClickListener listener) {
            super(itemLayoutView);

            this.listener = listener;

            tvName = (TextView) itemLayoutView.findViewById(R.id.tvName);
            selectedOverlay = (View) itemView.findViewById(R.id.selected_overlay);

            itemLayoutView.setOnClickListener(this);

            itemLayoutView.setOnLongClickListener (this);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onItemClicked(getAdapterPosition ());
            }
        }
        @Override
        public boolean onLongClick (View view) {
            if (listener != null) {
                return listener.onItemLongClicked(getAdapterPosition ());
            }
            return false;
        }

        public interface ClickListener {
            public void onItemClicked(int position);

            public boolean onItemLongClicked(int position);
        }
        }
    }

MultiSelectRecyclerViewActivity.java

public class MultiSelectRecyclerViewActivity extends AppCompatActivity implements MultiSelectRecyclerViewAdapter.ViewHolder.ClickListener {

    private Toolbar toolbar;

    private RecyclerView mRecyclerView;
    private MultiSelectRecyclerViewAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    private ArrayList<String> mArrayList  = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_multiselect);
        toolbar = (Toolbar) findViewById(R.id.toolbar);


        for (int i = 1; i <= 48; i++) {

            mArrayList.add ( ""+ i);
        }

        if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle("MultiSelectRecylcerView");

        }

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new GridLayoutManager (this,4));
        mAdapter = new MultiSelectRecyclerViewAdapter (MultiSelectRecyclerViewActivity.this,mArrayList,this);
        mRecyclerView.setAdapter (mAdapter);


    }

    @Override
    public void onItemClicked (int position) {

        toggleSelection(position);
    }

    @Override
    public boolean onItemLongClicked (int position) {
        toggleSelection(position);

        return true;
    }

    private void toggleSelection(int position) {
        mAdapter.toggleSelection (position);

    }
}

我在编码方面尝试了什么:

我已按照此enter image description here使用ItemDecoration为我的网格项指定间距,如下所示。但不幸的是,我能够在所有项目之间平均分配间距。

mRecyclerView.setLayoutManager(new GridLayoutManager(context, NUM_COLUMNS);
ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(context, R.dimen.item_offset);
mRecyclerView.addItemDecoration(itemDecoration);

请帮助我解决我的案件的任何提示和解决方案。我完全不知道如何使用RecyclerView继续这个要求和逻辑,我认为应该是优化的方法。提前谢谢。

1 个答案:

答案 0 :(得分:4)

在这里使用项目装饰可能会产生一些问题。如果您计划处理单击该项目,则单击空白区域将被解释为单击位于内部列中的项目。

您可以将GridLayoutManager配置为处理一个列并将空间作为特定视图类型处理。 Here我把代码实现了这种方法。

希望这可以提供帮助。