在RecyclerView上专注于以前的EditText

时间:2018-09-04 11:53:34

标签: java android user-interface

我在EditText中有一些垂直的RecyclerView

当我在下半场垂直屏幕上触摸EditText时。光标移至上一个EditText并专注于此。

我对此进行了搜索,但找不到任何解决方案。

请帮助。

编辑:

我点击了“回收者”视图中位置10的编辑文本。

但是android专注于编辑位置9的文本

编辑2: 我的适配器类:

public class inputsAdapter extends RecyclerView.Adapter<holder> implements View.OnFocusChangeListener {
    public List<String> paramList,resultList;
    Context mContext;
    boolean doneParam,doneResult;
    public View focusedView;
    public ArrayAdapter<String> adapter ;
    public inputsAdapter(List<String> paramList, List<String> resultList, Context mContext) {
        this.paramList = paramList;
        this.resultList = resultList;
        this.mContext = mContext;
        this.doneParam=false;
        this.doneResult=false;
        this.focusedView=null;
        this.adapter = new ArrayAdapter<String>
                (mContext,android.R.layout.simple_list_item_1,mContext.getResources().getStringArray(R.array.correct_params) );
    }

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

    @Override
    public void onBindViewHolder(holder holder, final int position) {
        if(position<paramList.size()){
            holder.paramConst.setVisibility(View.VISIBLE);
            holder.paramConst.setTag(position);
            holder.paramEt.setAdapter(adapter);
            holder.paramEt.setText(paramList.get(position));
            holder.addParam.setVisibility(View.GONE);
            doneParam=false;
            holder.paramEt.setOnFocusChangeListener(this);
        }else if(position==paramList.size()){
            holder.paramConst.setVisibility(View.INVISIBLE);
            holder.addParam.setBackground(mContext.getResources().getDrawable(R.drawable.ic_add_box_black_24dp));
            holder.addParam.setVisibility(View.VISIBLE);
        }else{
            holder.paramConst.setVisibility(View.INVISIBLE);
            holder.addParam.setVisibility(View.GONE);
        }

        if(position<resultList.size()){
            holder.resultConst.setVisibility(View.VISIBLE);
            holder.resultConst.setTag(position);
            holder.resultEt.setText(resultList.get(position));
            holder.addResult.setVisibility(View.GONE);
            doneResult=false;

        }else if(position==resultList.size()){
            holder.resultConst.setVisibility(View.INVISIBLE);
            holder.addResult.setBackground(mContext.getResources().getDrawable(R.drawable.ic_add_box_black_24dp));
            holder.addResult.setVisibility(View.VISIBLE);
        }else{
            holder.resultConst.setVisibility(View.INVISIBLE);
            holder.addResult.setVisibility(View.GONE);
        }


    }

    @Override
    public int getItemCount() {
        if(paramList.size()>resultList.size())
            {return paramList.size()+1;}
        else {return resultList.size()+1; }
    }


    @Override
    public void onFocusChange(View view, boolean b) {
        if(b){
            this.focusedView=view;

        }else{
            ConstraintLayout constraintLayout= (ConstraintLayout) view.getParent();
            int index= (int) constraintLayout.getTag();
            EditText et= (EditText) view;
            paramList.set(index,et.getText().toString());
        }
    }
}
class holder extends RecyclerView.ViewHolder {
    AutoCompleteTextView paramEt,resultEt;
    Button addParam,addResult;
    ConstraintLayout paramConst,resultConst;
    public holder(View itemView) {
        super(itemView);
        paramEt=itemView.findViewById(R.id.paramEt);
        resultEt=itemView.findViewById(R.id.resultEt);
        paramConst=itemView.findViewById(R.id.paramConstraint);
        resultConst=itemView.findViewById(R.id.resultConstraint);
        addParam=itemView.findViewById(R.id.paramAddAfterBtn);
        addResult=itemView.findViewById(R.id.resultAddAfterBtn);
    }
}

1 个答案:

答案 0 :(得分:0)

您的getItemCount方法返回了错误的值。您无需在其中添加+1。因此,将您的方法更改为以下代码,看看它是否可以解决您的问题:

@Override
public int getItemCount() {
    if(paramList.size()>resultList.size())
        {return paramList.size();}
    else {return resultList.size(); }
}