gpt4 book ai didi

android - notifyItemChanged() 使 RecyclerView 滚动并跳转到 UP

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:35:37 26 4
gpt4 key购买 nike

我有一个带有显示服务器列表的适配器的 RecycleView并且用户必须选择一个服务器。

当我在 onClick() 方法中调用 notifyItemChanged(previousPosition) 时取消选择旧服务器并选择新服务器,这使得 RecycleView 列表恰好跳到列表的中间。

当我单击 RecycleView 列表中的最后 2 或 3 个服务器之一时,就会发生此问题

这是我的 RecyclerView.Adapter 的代码:

public class ServerAdapter extends RecyclerView.Adapter<ServerAdapter.ServerViewHolder> {

private List<Server> listServers = new ArrayList<>();
private int[] icons = new int[]{R.drawable.server1,R.drawable.server2,R.drawable.server3,R.drawable.server4,R.drawable.server5,R.drawable.server6,R.drawable.offline};
private int selected = 0;
private int previousSelected = 0;

public ServerAdapter(List<Server> listServers){
this.listServers = listServers;
}

@Override
public ServerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.server_relative_layout,parent,false);
return new ServerViewHolder(view);
}

@Override
public void onBindViewHolder(final ServerViewHolder holder, final int position) {
if(position == selected){
holder.getBackground().setSelected(true);
}else{
holder.getBackground().setSelected(false);
}
holder.getBackground().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(position != selected){
previousSelected = selected;
selected = position;
holder.getBackground().setSelected(true);
notifyItemChanged(previousSelected);
}
}
});
holder.getImageServer().setImageResource(icons[position%6]);
holder.getTextNameServer().setText(listServers.get(position).getName());
holder.getTextConnected().setText(listServers.get(position).getUrl());
}

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

public class ServerViewHolder extends RecyclerView.ViewHolder{
private ImageView imageServer;
private TextView textNameServer;
private TextView textConnected;
private View background;
public ServerViewHolder(View itemView) {
super(itemView);
imageServer = (ImageView)itemView.findViewById(R.id.imageServer);
textNameServer = (TextView)itemView.findViewById(R.id.textNameServer);
textConnected = (TextView)itemView.findViewById(R.id.textConnected);
background = itemView;
}

public ImageView getImageServer() {
return imageServer;
}

public TextView getTextConnected() {
return textConnected;
}

public TextView getTextNameServer() {
return textNameServer;
}

public View getBackground() {
return background;
}
}
}

有解决这个问题的方法吗?谢谢。

当我指定布局高度并且不让它 wrap_content 时,问题就发生了

<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="400dp"
android:id="@+id/serverRecyclerView"
android:layout_margin="10dp"
/>

或者当我把它放在下面这样的例子时:

<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/serverRecyclerView"
android:layout_margin="10dp"
android:layout_below="@+id/image"/>

我的代码是:

<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/serverRecyclerView"
android:layout_margin="10dp"
android:layout_alignTop="@+id/imageBall"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="@+id/camera"
android:layout_toEndOf="@+id/camera"/>

最佳答案

看起来这是一个错误:https://code.google.com/p/android/issues/detail?id=203574

最好的解决方法似乎是 Bart 将 RecyclerView 的 LinearLayoutManager 的 AutoMeasure 属性设置为 false 的答案。

  LinearLayoutManager llm = new LinearLayoutManager(context);
llm.setAutoMeasureEnabled(false);
recyclerView.setLayoutManager(llm);

将 FixedSize 设置为 true 的解决方案有太多的副作用......

RecyclerView.setHasFixedSize(真)

关于android - notifyItemChanged() 使 RecyclerView 滚动并跳转到 UP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36724898/

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