RecyclerView在添加项目时未固定在顶部。

huangapple go评论74阅读模式
英文:

RecyclerView isn't fixed to top when Items are added

问题

我有这个RecyclerView,一旦添加足够的项目使RecyclerView可以滚动,它就不再显示位置0的项目,而似乎会粘在底部项目上并随之滚动,尽管其他项目是在位置0添加的。

public class hauptseite extends AppCompatActivity {
    private ArrayList<ExampleItem> mExampleList;
    private RecyclerView mRecyclerView;
    private ExampleAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private ImageButton btnInput;
    private EditText editTextInput;
    private int position = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // ... (其余代码不变)
        
        mRecyclerView = findViewById(R.id.recyclerName);
        mRecyclerView.setHasFixedSize(false);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new ExampleAdapter(mExampleList);

        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
        
        // 将以下代码添加到onCreate方法中,以使RecyclerView保持在位置0,直到手动滚动
        mRecyclerView.scrollToPosition(position);

        mAdapter.setOnItemClickListener(new ExampleAdapter.OnItemClickListener() {
            @Override
            public void onDeleteClick(int position) {
                removeItem(position);
            }
        });
    }
    
    // ... (其余代码不变)
}

通过在onCreate方法中使用mRecyclerView.scrollToPosition(position);,您可以确保RecyclerView一开始就滚动到位置0,除非手动滚动。

英文:

I have this RecyclerView that doesn't want to show the item at position 0 anymore, after enough items are added to make the RecyclerView scrollable. Instead it seems to stick to the bottom item and scroll down with this one, although the other items are added at position 0.

public class hauptseite extends AppCompatActivity {
private ArrayList&lt;ExampleItem&gt; mExampleList;
private RecyclerView mRecyclerView;
private ExampleAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private ImageButton btnInput;
private EditText editTextInput;
private int position = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    btnInput = findViewById(R.id.btn_add);
    editTextInput = findViewById(R.id.name_add);

    createExampleList();
    buildRecyclerView();

    btnInput = findViewById(R.id.btn_add);
    editTextInput = findViewById(R.id.name_add);

    btnInput.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = editTextInput.getText().toString();
            insertItem(name);
        }
    });
}

public void insertItem(String name) {
    mExampleList.add(position, new ExampleItem(name));
    mAdapter.notifyItemInserted(position);
    editTextInput.setText(&quot;&quot;);
}

public void removeItem(int position) {
    mExampleList.remove(position);
    mAdapter.notifyItemRemoved(position);
}

public void createExampleList() {
    mExampleList = new ArrayList&lt;&gt;();
}

public void buildRecyclerView() {
    mRecyclerView = findViewById(R.id.recyclerName);
    mRecyclerView.setHasFixedSize(false);
    mLayoutManager = new LinearLayoutManager(this);
    mAdapter = new ExampleAdapter(mExampleList);

    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(mAdapter);

   mAdapter.setOnItemClickListener(new ExampleAdapter.OnItemClickListener(){
       @Override
    public void onDeleteClick(int position) {
        removeItem(position);
    }
    });
}

And the adapter

public class ExampleAdapter extends RecyclerView.Adapter&lt;ExampleAdapter.ExampleViewHolder&gt; {
private ArrayList&lt;ExampleItem&gt; mExampleList;
private OnItemClickListener mListener;

public interface OnItemClickListener {
    void onDeleteClick(int position);
}

public void setOnItemClickListener(OnItemClickListener listener) {
    mListener = listener;
}

public static class ExampleViewHolder extends RecyclerView.ViewHolder{
    public TextView mTextView1;
    public ImageView mDeleteName;

    public ExampleViewHolder(View itemView, final OnItemClickListener listener) {
        super(itemView);
        mTextView1 = itemView.findViewById(R.id.name_added);
        mDeleteName = itemView.findViewById(R.id.delete_name);

        mDeleteName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        listener.onDeleteClick(position);
                    }
                }
            }
        });
    }
}

public ExampleAdapter(ArrayList&lt;ExampleItem&gt; exampleList) {
    mExampleList = exampleList;
}

@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
    ExampleViewHolder evh = new ExampleViewHolder(v, mListener);
    return evh;
}

@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
    ExampleItem currentItem = mExampleList.get(position);

    holder.mTextView1.setText(currentItem.getName1());
}

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

So what do I need change when I want my RecyclerView to stay at the item in position 0 unless I start scrolling down?

答案1

得分: 0

你可以滚动到插入的位置:

mLayoutManager.scrollToPosition(yourPosition)
英文:

You can scroll to the desired position on the insert:

mLayoutManager.scrollToPosition(yourPosition)

答案2

得分: 0

有一种简单的方法可以解决这个问题。
不要在位置0添加项目...只需颠倒您的recyclerView的默认顺序。

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
yourRecyclerView.setLayoutManager(linearLayoutManager);

英文:

there is easy way to fix that.
do not add item at position 0 ... just reverse the default order of your recyclerView

 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
            linearLayoutManager.setReverseLayout(true);
            linearLayoutManager.setStackFromEnd(true);
            yourRecyclerView.setLayoutManager(linearLayoutManager);

答案3

得分: 0

One: 不要在你的活动中保持位置,而是使用ArrayList的大小

public void insertItem(String name) {
    mExampleList.add(new ExampleItem(name));
    mAdapter.notifyItemInserted(mExampleList.size() - 1);
    editTextInput.setText("");
}

Two: 将示例列表及其方法移动到你的适配器中。这样你也可以删除删除接口,让适配器处理所有操作。我还添加了一个方法,以从适配器中检索当前项目列表,以防需要在适配器外部使用该数据。

Activity:

public class hauptseite extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private ExampleAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private ImageButton btnInput;
    private EditText editTextInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        btnInput = findViewById(R.id.btn_add);
        editTextInput = findViewById(R.id.name_add);

        buildRecyclerView();

        btnInput = findViewById(R.id.btn_add);
        editTextInput = findViewById(R.id.name_add);

        btnInput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = editTextInput.getText().toString();
                mAdapter.insertItem(name);
            }
        });
    }

    public void removeItem(int position) {
        mAdapter.notifyItemRemoved(position);
    }

    public void createExampleList() {
        mExampleList = new ArrayList<>();
    }

    public void buildRecyclerView() {
        mRecyclerView = findViewById(R.id.recyclerName);
        mRecyclerView.setHasFixedSize(false);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new ExampleAdapter();

        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }
}

适配器:

public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
    private ArrayList<ExampleItem> mExampleList;

    public void insertItem(String name) {
        mExampleList.add(new ExampleItem(name));
        this.notifyItemInserted(mExampleList.size() - 1);
    }

    public void removeItem(int position) {
        mExampleList.remove(position);
        this.notifyItemRemoved(position);
    }

    public ArrayList<ExampleItems> getItems() {
        return mExampleList;
    }

    public static class ExampleViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView1;
        public ImageView mDeleteName;

        public ExampleViewHolder(View itemView, final OnItemClickListener listener) {
            super(itemView);
            mTextView1 = itemView.findViewById(R.id.name_added);
            mDeleteName = itemView.findViewById(R.id.delete_name);

            mDeleteName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            removeItem(position);
                        }
                    }
                }
            });
        }
    }

    public ExampleAdapter() {
        mExampleList = new ArrayList<>();
    }
}
英文:

I see two good ways of solving this:

One: Don't hold position in your activity, but use the size of the ArrayList

public void insertItem(String name) {
mExampleList.add(new ExampleItem(name));
mAdapter.notifyItemInserted(mExampleList.size() - 1);
editTextInput.setText(&quot;&quot;);
}

Two: Move the example list and its methods into your adapter. This way you can also remove the interface for deletion and let the adapter handle it all. I also added a method to retrieve the current list of items from the adapter incase you need that data outside of the adapter.

Activity:

public class hauptseite extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ExampleAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private ImageButton btnInput;
private EditText editTextInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnInput = findViewById(R.id.btn_add);
editTextInput = findViewById(R.id.name_add);
buildRecyclerView();
btnInput = findViewById(R.id.btn_add);
editTextInput = findViewById(R.id.name_add);
btnInput.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editTextInput.getText().toString();
mAdapter.insertItem(name);
}
});
}
public void removeItem(int position) {
mAdapter.notifyItemRemoved(position);
}
public void createExampleList() {
mExampleList = new ArrayList&lt;&gt;();
}
public void buildRecyclerView() {
mRecyclerView = findViewById(R.id.recyclerName);
mRecyclerView.setHasFixedSize(false);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ExampleAdapter();
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}

And the adapter:

public class ExampleAdapter extends RecyclerView.Adapter&lt;ExampleAdapter.ExampleViewHolder&gt; {
private ArrayList&lt;ExampleItem&gt; mExampleList;
public insertItem(String name) {
mExampleList.add(name);
this.notifyItemAdded(mExampleList.size() -1)
}
public removeItem(int position) {
mExampleList.remove(position);
this.notifyItemRemoved(position);
}
public ArrayList&lt;ExampleItems&gt; getItems() {
return mExampleList();
}
public static class ExampleViewHolder extends RecyclerView.ViewHolder{
public TextView mTextView1;
public ImageView mDeleteName;
public ExampleViewHolder(View itemView, final OnItemClickListener listener) {
super(itemView);
mTextView1 = itemView.findViewById(R.id.name_added);
mDeleteName = itemView.findViewById(R.id.delete_name);
mDeleteName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
removeItem(position);
}
}
}
});
}
}
public ExampleAdapter() {
mExampleList = new ExampleList();
}

huangapple
  • 本文由 发表于 2020年1月7日 02:33:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定