当用户按#button然后它将在RecyclerView的焦点EditText上打印#时,它是如何实现的。 我的代码和输出如下。 请帮帮我......
我已经完成了所有事情,但却陷入了困境。我可以获得任何建议或链接有关如何实现这一点的更多信息吗?
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnAt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\@" />
<Button
android:id="@+id/btnHash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\#" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private RecyclerView mRecyclerView; // RecyclerVIew
private CheckItemAdapter myAdapterRecyclerView; //The Adapter for RecyclerVIew
Button btnAt, btnHash;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
setAdapter();
btnAt = (Button) findViewById(R.id.btnAt);
btnHash = (Button) findViewById(R.id.btnHash);
}
public void setAdapter() {
mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); // Set LayoutManager in the RecyclerView
myAdapterRecyclerView = new CheckItemAdapter(this, getData()); // Create Instance of MyAdapterRecyclerView
mRecyclerView.setAdapter(myAdapterRecyclerView); // Set Adapter for RecyclerView
}
public ArrayList<ItemModel> getData() {
ArrayList<ItemModel> itemModelArrayList = new ArrayList<ItemModel>();
ItemModel itemModel = new ItemModel();
itemModel.setChkText("ABC");
itemModelArrayList.add(itemModel);
itemModel = new ItemModel();
itemModel.setChkText("ABC");
itemModelArrayList.add(itemModel);
itemModel = new ItemModel();
itemModel.setChkText("ABC");
itemModelArrayList.add(itemModel);
itemModel = new ItemModel();
itemModel.setChkText("ABC");
itemModelArrayList.add(itemModel);
itemModel = new ItemModel();
itemModel.setChkText("ABC");
itemModelArrayList.add(itemModel);
itemModel = new ItemModel();
itemModel.setChkText("ABC");
itemModelArrayList.add(itemModel);
itemModel = new ItemModel();
itemModel.setChkText("ABC");
itemModelArrayList.add(itemModel);
return itemModelArrayList;
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnAt) {
//print @ on focused EditText of RecyclerView item
} else if (v.getId() == R.id.btnHash) {
//print # on focused EditText of RecyclerView item
}
}
}
CheckItemAdapter
public class CheckItemAdapter extends RecyclerView.Adapter<CheckItemAdapter.MyViewHolder> {
private ArrayList<ItemModel> mList;
Context context;
public CheckItemAdapter(Context context, ArrayList<ItemModel> mList) {
this.mList = mList;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // create a new view
View itemLayoutView = LayoutInflater.from(context).inflate(R.layout.single_check_list, parent, false);
// create ViewHolder
MyViewHolder viewHolder = new MyViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
ItemModel itemModel = mList.get(position);
holder.editCheckItem.setText(itemModel.getChkText());
}
@Override
public int getItemCount() {
if (mList != null)
return mList.size();
else
return 0;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public EditText editCheckItem;
public CheckBox chkItem;
public TextView txtRemove;
public MyViewHolder(View itemLayoutView) {
super(itemLayoutView);
editCheckItem = (EditText) itemLayoutView.findViewById(R.id.editCheckItem);
}
}
}
[![output image][1]][1]
答案 0 :(得分:3)
在您的活动中将按钮声明为静态。
实施例: 在ABCActivity中:
public static Button btnTest;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_family_member);
btnTest = (Button)findviewbyid(R.id.btnTest);
}
在Adpter:
ABCActivity.btnTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
答案 1 :(得分:0)
制作自己的点击监听器。
private static OnItemClickListener listener;
public interface OnItemClickListener {
void onItemClick(View itemView, int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
YourAdapterClass.listener = listener;
}
// and in constructor of ViewHolder use it.
yourButtonView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onItemClick(itemView, getLayoutPosition());
}
}
});
因此代码应该在您的RecyclerView适配器中。在您的活动/片段中,您只需要使用适配器的方法setOnItemClickListener。这是你的问题的正常做法。
答案 2 :(得分:0)
你应该在onBindViewHolder中检查焦点,如下所示:
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
ItemModel itemModel = mList.get(position);
holder.editCheckItem.setText(itemModel.getChkText());
if(holder.editCheckItem.hasFocus()) {
// Add your # character to your edit text
holder.editCheckItem.append("#");
}
}
然后,只需通知您的适配器:
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnAt) {
//print @ on focused EditText of RecyclerView item
} else if (v.getId() == R.id.btnHash) {
//print # on focused EditText of RecyclerView item
myAdapterRecyclerView.notifyDataChanged();
}
}