Cardview with editText和Recyclerview

时间:2017-10-14 18:06:05

标签: android android-recyclerview android-cardview

我在Android编程方面不是很好。我可以使用cardview和recyclerview进行此布局。

但我需要使用editText为每张卡片设置2个监听器。 OnFocusChange侦听器和OnTextChange侦听器。我必须得到每个editText的文本。 我有什么要做的? 这是我目前的代码:

对象

public class unitObject {

private String mtitle;
private String mhint;

unitObject(String title, String hint){
    mtitle=title;
    mhint=hint;
}
public String getMtitle() {
    return mtitle;
}

public String getMhint() {
    return mhint;
}

public void setMtitle(String mtitle) {
    this.mtitle = mtitle;
}

public void setMhint(String mhint) {
    this.mhint = mhint;
}
}

Recycler adapter:

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.DataObjectHolder> {
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<unitObject> mDataset;
private static MyClickListener myClickListener;

public static class DataObjectHolder extends RecyclerView.ViewHolder
        implements View
        .OnClickListener {
    TextView title;
    TextView ET_hint;
    String content;

    public DataObjectHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.textView);
        ET_hint = (TextView) itemView.findViewById(R.id.editText);
        Log.i(LOG_TAG, "Adding Listener");
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        myClickListener.onItemClick(getAdapterPosition(), v);
    }
}



public MyRecyclerViewAdapter(ArrayList<unitObject> myDataset) {
    mDataset = myDataset;
}

@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                           int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_view_row, parent, false);

    DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
    return dataObjectHolder;
}

@Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
    holder.title.setText(mDataset.get(position).getMtitle());
    holder.ET_hint.setHint(mDataset.get(position).getMhint());
}



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

public interface MyClickListener {
    public void onItemClick(int position, View v);
}

课程

public class classic_conversion extends AppCompatActivity {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.classic_conversion_layout);

    mRecyclerView =  findViewById(R.id.my_recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new MyRecyclerViewAdapter(getDataSet());
    mRecyclerView.setAdapter(mAdapter);
}
private ArrayList<unitObject> getDataSet() {
    ArrayList results = new ArrayList<unitObject>();
    results.add(new unitObject("Title 1","hint 1"));
    results.add(new unitObject("Title 2","hint 2"));
    results.add(new unitObject("Title 3","hint 3"));
    return results;
}
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

对于侦听器editText:

yourEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            //BEFORE TEXT CHANGED
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        // WHILE TEXT IS CHANGE
        }

        @Override
        public void afterTextChanged(Editable editable) {
            //AFTER TEXT CHANGED (LIKE YOU PRESS ENTER AFTER TYPING)

        }
    });

让听众更改焦点。

yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {

            if (hasFocus){
                //DO WHAT YOU NEED WHEN editText HAS FOCUS
            }
            else {
                //DO WHAT YOU NEED WHEN editText HAS NO FOCUS
            }

        }
    });

我真的希望这会对你有所帮助,对于迟到的回复感到抱歉。