2

我正在使用带有 GridLayoutManager 的 RecyclerView 作为巴士票预订的布局。一切正常,但除了我坚持使用逻辑的地方,我必须使用这些逻辑在所有行中的 3 到 4 列项目之间留出更多间距。基本上我的布局应该如下面的截图所示。

我的要求:

在此处输入图像描述

电流输出:

在此处输入图像描述

我还发布了我的完整 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);

    }
}

我在编码中尝试过的内容:

我已经按照这个要点使用 ItemDecoration 为我的网格项目分配间距,如下所示。但不幸的是,我只能在所有项目之间平均分配间距。

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

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

4

1 回答 1

5

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

您可以配置 GridLayoutManager 以处理多列并将空间处理为特定的视图类型。在这里,我放置了实现这种方法的代码。

希望这会有所帮助。

于 2016-02-09T10:14:05.137 回答