如何为数据绑定中的相同多个包含编写通用的onclicklistener
how to write common onclicklistener for same multiple include in data binding
我有布局文件,其中包含多个包含相同布局文件的文件,如下所示。
<?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>
下面是像
这样的包含布局文件
<?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>
我希望包含文件中的所有 textView 具有通用 onCLickListener
。
下面是我的 Activity
代码
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;
}
}
}
我在 onClick
方法中收到点击侦听器回调,但我如何区分哪个包含负责该点击。因为我每次点击都会获得相同的 ID。
我可以为所有这些文本视图设置单独的点击监听器。但是有没有办法实现一键监听。
试试这个,
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.row_1:
break;
case R.id.row_2:
break;
}
}
你应该试试这个。
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
switch (((ViewGroup)v.getParent()).getId()) {
case R.id.row_1:
break;
case R.id.row_2:
break;
case R.id.row_3:
break;
case R.id.row_4:
break;
case R.id.row_5:
break;
}
break;
}
}
}
希望对您有所帮助
您可以像这样使用绑定的 ID 进行检查:
@Override
public void onClick(View view) {
if (view == binding.row1.txtRotTitle) {
Toast.makeText(MainActivity.this, "text clicked", Toast.LENGTH_SHORT).show();
} else if (view == binding.row2.txtRotTitle) {
Toast.makeText(MainActivity.this, "text clicked 2", Toast.LENGTH_SHORT).show();
}
}
可能为时已晚,但现在我遇到了同样的问题,我从这里给出的答案中做出了混合。这就是我使用许多按钮的方式(它也适用于任何其他组件)。
public class DashBoardActivity extends AppCompatActivity implements View.OnClickListener {
ActivityDashboardBinding binding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_dashboard);
setOnClickListeners();
}
@Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.btDependencies:
intent = new Intent(this, DependencyListActivity.class);
break;
case R.id.btSections:
//intent = new Intent(this, Li.class);
Toast.makeText(this,"Sections",Toast.LENGTH_SHORT).show();
break;
case R.id.btProducts:
//intent = new Intent(this, .class);
Toast.makeText(this,"Products",Toast.LENGTH_SHORT).show();
break;
case R.id.btProfile:
//intent = new Intent(this, .class);
Toast.makeText(this,"Profile",Toast.LENGTH_SHORT).show();
break;
case R.id.btHelp:
//intent = new Intent(this, .class);
Toast.makeText(this,"Help",Toast.LENGTH_SHORT).show();
break;
case R.id.btSettings:
//intent = new Intent(this, .class);
Toast.makeText(this,"Settings",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
if (intent != null)
startActivity(intent);
}
/**
* Method used to set the OnClickListener on every DashBoard buttons
*/
private void setOnClickListeners() {
binding.btDependencies.setOnClickListener(this);
binding.btSections.setOnClickListener(this);
binding.btProducts.setOnClickListener(this);
binding.btProfile.setOnClickListener(this);
binding.btHelp.setOnClickListener(this);
binding.btSettings.setOnClickListener(this);
}
希望对大家有所帮助=)
我有布局文件,其中包含多个包含相同布局文件的文件,如下所示。
<?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>
下面是像
这样的包含布局文件<?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>
我希望包含文件中的所有 textView 具有通用 onCLickListener
。
下面是我的 Activity
代码
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;
}
}
}
我在 onClick
方法中收到点击侦听器回调,但我如何区分哪个包含负责该点击。因为我每次点击都会获得相同的 ID。
我可以为所有这些文本视图设置单独的点击监听器。但是有没有办法实现一键监听。
试试这个,
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.row_1:
break;
case R.id.row_2:
break;
}
}
你应该试试这个。
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
switch (((ViewGroup)v.getParent()).getId()) {
case R.id.row_1:
break;
case R.id.row_2:
break;
case R.id.row_3:
break;
case R.id.row_4:
break;
case R.id.row_5:
break;
}
break;
}
}
}
希望对您有所帮助
您可以像这样使用绑定的 ID 进行检查:
@Override
public void onClick(View view) {
if (view == binding.row1.txtRotTitle) {
Toast.makeText(MainActivity.this, "text clicked", Toast.LENGTH_SHORT).show();
} else if (view == binding.row2.txtRotTitle) {
Toast.makeText(MainActivity.this, "text clicked 2", Toast.LENGTH_SHORT).show();
}
}
可能为时已晚,但现在我遇到了同样的问题,我从这里给出的答案中做出了混合。这就是我使用许多按钮的方式(它也适用于任何其他组件)。
public class DashBoardActivity extends AppCompatActivity implements View.OnClickListener {
ActivityDashboardBinding binding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_dashboard);
setOnClickListeners();
}
@Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {
case R.id.btDependencies:
intent = new Intent(this, DependencyListActivity.class);
break;
case R.id.btSections:
//intent = new Intent(this, Li.class);
Toast.makeText(this,"Sections",Toast.LENGTH_SHORT).show();
break;
case R.id.btProducts:
//intent = new Intent(this, .class);
Toast.makeText(this,"Products",Toast.LENGTH_SHORT).show();
break;
case R.id.btProfile:
//intent = new Intent(this, .class);
Toast.makeText(this,"Profile",Toast.LENGTH_SHORT).show();
break;
case R.id.btHelp:
//intent = new Intent(this, .class);
Toast.makeText(this,"Help",Toast.LENGTH_SHORT).show();
break;
case R.id.btSettings:
//intent = new Intent(this, .class);
Toast.makeText(this,"Settings",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
if (intent != null)
startActivity(intent);
}
/**
* Method used to set the OnClickListener on every DashBoard buttons
*/
private void setOnClickListeners() {
binding.btDependencies.setOnClickListener(this);
binding.btSections.setOnClickListener(this);
binding.btProducts.setOnClickListener(this);
binding.btProfile.setOnClickListener(this);
binding.btHelp.setOnClickListener(this);
binding.btSettings.setOnClickListener(this);
}
希望对大家有所帮助=)