gpt4 book ai didi

java - Android:设置 RecyclerView 项目的重力

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:36 25 4
gpt4 key购买 nike

我有一个行为类似于列表的 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);

最佳答案

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

关于java - Android:设置 RecyclerView 项目的重力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36680630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com