我有一个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);