I have layout file where I have multiple includes with same layout file like below.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="row1Data"
type="com.desaidr.testapp.RowDetails" />
<variable
name="row2Data"
type="com.desaidr.testapp.RowDetails" />
<variable
name="row3Data"
type="com.desaidr.testapp.RowDetails" />
<variable
name="row4Data"
type="com.desaidr.testapp.RowDetails" />
<variable
name="row5Data"
type="com.desaidr.testapp.RowDetails" />
</data>
<LinearLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/row_1"
layout="@layout/include_row_details"
app:rowDetails="@{row1Data}"/>
<include
android:id="@+id/row_2"
layout="@layout/include_row_details"
app:rowDetails="@{row2Data}"/>
<include
android:id="@+id/row_3"
layout="@layout/include_row_details"
app:rowDetails="@{row3Data}"/>
<include
android:id="@+id/row_4"
layout="@layout/include_row_details"
app:rowDetails="@{row4Data}"/>
<include
android:id="@+id/row_5"
layout="@layout/include_row_details"
app:rowDetails="@{row5Data}"/>
</LinearLayout>
</layout>
below is the include layout file like
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="rowDetails"
type="com.desaidr.testapp.RowDetails" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/txt_rot_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={rowDetails.title}"/>
</LinearLayout>
</layout>
I would like to have common onCLickListener
for all those textView inside my include file.
Below is my Activity
code
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ActivityMainBinding mBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mBinding.row1.txtRowTitle.setOnClickListener(this);
mBinding.row2.txtRowTitle.setOnClickListener(this);
mBinding.row3.txtRowTitle.setOnClickListener(this);
mBinding.row4.txtRowTitle.setOnClickListener(this);
mBinding.row5.txtRowTitle.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.txt_row_title:
//TODO I am getting all my click here but how would differentiate which include is responsible for this click listener
break;
}
}
}
I am getting click listener call backs in my onClick
method but how would I differentiate which include is responsible for that click. As I ma getting same ID every time for click.
I could have separate click listener for all those text view. But is there a way to achieve in one click listener.
You can check using binding's id like this :