How to get Activity button click listener in Recyc

2019-09-21 10:27发布

问题:

How its possible when user press #button then it will print # on focused EditText of RecyclerView. My code and output is below. Please help me....

I've done everything, but got stuck in here. Can I get any suggestion or link with some more info on how to implement this?

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]

回答1:

Declare button as static in your activity.

Example: In 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);
}

In Adpter:

ABCActivity.btnTest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });


回答2:

Make your own click listener.

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());
                }
            }
});

So that code should be in your RecyclerView adapter. In your activity/fragment you just need to use adapter's method setOnItemClickListener. It's normal practice for your question.



回答3:

You should check focus in onBindViewHolder like this:

        @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("#");
            }
        }

Then, simply notify your adapter:

        @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();
            }
        }