焦点在回收器视图中编辑文本时出错

时间:2017-10-09 08:33:33

标签: android android-recyclerview

我有一个RecyclerView。 Recycler视图中的每一行都有一个编辑文本组件。

当键盘可见时,应用程序崩溃,并且用户点击弹出键盘时将被键盘占用的区域中的任何EditText组件 。 换句话说,屏幕上半部分的EditText框不会抛出异常。

此错误特定于使用Android 6.0或更高版本的设备。

对于版本低于 Android 6.0 的设备,该应用效果非常好,因为EditText会自动滚动到键盘上方。

以下是例外。

E/AndroidRuntime: FATAL EXCEPTION: main
 Process: in.haplotech.aims, PID: 12723
 java.lang.IllegalArgumentException: parameter must be a descendant of this view
     at android.view.ViewGroup.offsetRectBetweenParentAndChild(ViewGroup.java:5502)
     at android.view.ViewGroup.offsetDescendantRectToMyCoords(ViewGroup.java:5431)
     at android.widget.ScrollView.isWithinDeltaOfScreen(ScrollView.java:1167)
     at android.widget.ScrollView.onSizeChanged(ScrollView.java:1576)
     at android.view.View.sizeChange(View.java:17641)
     at android.view.View.setFrame(View.java:17603)
     at android.view.View.layout(View.java:17520)
     at android.view.ViewGroup.layout(ViewGroup.java:5618)
     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
     at android.view.View.layout(View.java:17523)
     at android.view.ViewGroup.layout(ViewGroup.java:5618)
     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
     at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
     at android.view.View.layout(View.java:17523)
     at android.view.ViewGroup.layout(ViewGroup.java:5618)
     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
     at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
     at android.view.View.layout(View.java:17523)
     at android.view.ViewGroup.layout(ViewGroup.java:5618)
     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
     at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
     at android.view.View.layout(View.java:17523)
     at android.view.ViewGroup.layout(ViewGroup.java:5618)
     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
     at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
     at android.view.View.layout(View.java:17523)
     at android.view.ViewGroup.layout(ViewGroup.java:5618)
     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
     at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
     at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
     at android.view.View.layout(View.java:17523)
     at android.view.ViewGroup.layout(ViewGroup.java:5618)
     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2374)
     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2101)
     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1278)
     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6357)
     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
     at android.view.Choreographer.doCallbacks(Choreographer.java:683)
     at android.view.Choreographer.doFrame(Choreographer.java:619)
     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
     at android.os.Handler.handleCallback(Handler.java:751)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:154)
     at android.app.ActivityThread.main(ActivityThread.java:6077)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

以下是我的适配器代码:

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {

private Context context;
private List<User> dataList;
private ImageLoader mImageLoader;

public StudentAdapter(List<User> dataList) {
    this.dataList = dataList;
    this.mImageLoader = CustomVolleyRequestQueue.getInstance(AppName.getInstance().getApplicationContext()).getImageLoader();
}

public List<User> getDataList() {
    return dataList;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    View itemView = inflater.inflate(R.layout.item_exam_student, parent, false);

    ViewHolder viewHolder = new ViewHolder(itemView);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
    final User std = dataList.get(position);

    RelativeLayout studentHolder = viewHolder.studentHolder;
    NetworkImageView imgStudentPic = viewHolder.imgStudentPic;
    final TextView tvName = viewHolder.tvName;
    final EditText etMarks = viewHolder.etMarks;

    tvName.setText(std.toString());
    etMarks.setVisibility(View.GONE);
}

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

@Override
public int getItemViewType(int position) {//VVIMP : used to maintain the state of scrolled items.
    //Resource : http://stackoverflow.com/questions/32065267/recyclerview-changing-items-during-scroll
    return position;
}

public static class ViewHolder extends RecyclerView.ViewHolder {

    public RelativeLayout studentHolder;
    public NetworkImageView imgStudentPic;
    public TextView tvName;
    public EditText etMarks;

    public ViewHolder(View itemView) {
        super(itemView);

        studentHolder = (RelativeLayout) itemView.findViewById(R.id.studentHolder);
        imgStudentPic = (NetworkImageView) itemView.findViewById(R.id.imgStudentPic);
        tvName = (TextView) itemView.findViewById(R.id.tvName);
        etMarks = (EditText) itemView.findViewById(R.id.etMarks);
    }
}
}

2 个答案:

答案 0 :(得分:0)

Hello这是适配器的代码我可以成功地使用包含每行编辑文本的回收器视图进行滚动。  公共类MovieAdapter扩展了RecyclerView.Adapter {

private List<Movie> movieList;

@Override
public MovieHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.movie_list_row, parent, false);

    return new MovieHolder(itemView);
}

@Override
public void onBindViewHolder(MovieHolder holder, int position) {
    Movie movie = movieList.get(position);

    holder.ed.setText(movie.getTitle());
}
public MovieAdapter(List<Movie> movieList)
{

    this.movieList = movieList;
}


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



class MovieHolder extends RecyclerView.ViewHolder
{

    public EditText ed;
    public MovieHolder(View view)
    {
        super(view);

        ed = (EditText) view.findViewById(R.id.name);
    }
}}

MainActivity类代码:

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    mAdapter = new MovieAdapter(movieList);

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mAdapter);

如果您仍然遇到问题,请告诉我。

由于

答案 1 :(得分:0)

我将以下内容添加到了android清单文件中的activity标记。

android:windowSoftInputMode="adjustPan"

这只是一个临时解决方案,因为很少有用户体验问题。

可以在这个问题中研究实际问题:Recyclerview not scrolling to end when keyboard opens