我在我的项目中用RecyclerViews替换了ListViews。我有一个与recyclerview对话,我想在点击后更改其中的项目。但是我有问题,当我尝试点击最后一个元素时,RecyclerView会向上滚动。
在空活动中,我只创建如下对话框:
CustDialog dlg = new CustDialog();
dlg.show(getFragmentManager(), "dlg1");
此处的对话框代码:
public class CustDialog extends DialogFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog, null);
RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
MyAdapter adapter = new MyAdapter();
recyclerView.setAdapter(adapter);
return v;
}
static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(android.R.id.text1);
}
}
static class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
List<String> mItems;
public MyAdapter() {
mItems = new ArrayList<>();
for (int i = 0; i < 50; i++) {
mItems.add("Test " + i);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.minimal, parent, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
String item = mItems.get(position);
holder.textView.setText(item);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
notifyItemChanged(position);
}
});
}
@Override
public int getItemCount() {
return mItems.size();
}
}
}
对话框布局仅包含带有replalerviewview的LinearLayout,其中包含match_parent参数。项目布局包含带有textview的LinearLayout。
如果您在点击最后一个元素后尝试此示例,则recyclelerview会以两个或三个元素的距离向上滚动。