Android:设置RecyclerView项目的重力

时间:2016-04-17 18:40:58

标签: java android android-recyclerview

我有一个回收列表,其行为类似于列表,当我删除一个项目时,所有其他项目都会上升,我希望它们可以向下移动。我正在考虑将重力设置为项目,或者反转适配器中的位置,使底部位置为0。

我的适配器(自定义):

public class AddEventsAdapter extends HFRecyclerViewAdapter<String, AddEventsAdapter.ViewHolder> {

    public AddEventsAdapter(Context context) {
        super(context);
    }

    @Override
    public void footerOnVisibleItem() {

    }

    @Override
    public void addData(int position, String item) {
        super.addData(position, item);
    }

    @Override
    public ViewHolder onCreateDataItemViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.data_item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindDataItemViewHolder(ViewHolder holder, int position) {
        holder.itemTv.setText(getData().get(position));
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView itemTv;
        LinearLayout theLayout;
        public ViewHolder(View itemView) {
            super(itemView);
            itemTv = (TextView)itemView.findViewById(R.id.itemTv);
            theLayout = (LinearLayout)itemView.findViewById(R.id.layout2);
            theLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    removeData(getAdapterPosition());
                }
            });
        }
    }
}

实施

    AddEventsAdapter myAdapter = new AddEventsAdapter(this);
    assert layout != null;
    layout.setAdapter(myAdapter);

    layout.setLayoutManager(new LinearLayoutManager(this));

    View footerView = LayoutInflater.from(this).inflate(R.layout.footer, layout, false);
    myAdapter.setFooterView(footerView);

    ArrayList<String> data = new ArrayList<>();
    for (int i=0; i<8; i++){
        data.add(String.format("Item %d", i));
    }
    myAdapter.setData(data);

1 个答案:

答案 0 :(得分:4)

通过将此行添加到LayoutManager来解决:.setReverseLayout(true);