我有布局文件,其中我有多个包含相同的布局文件,如下所示。
<?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>
我希望在我的include文件中包含所有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。
我可以为所有这些文本视图设置单独的单击侦听器。但有没有办法在一次点击监听器中实现。
答案 0 :(得分:1)
你应该试试这个。
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;
}
}
}
希望这个帮助
答案 1 :(得分:1)
您可以使用绑定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();
}
}
答案 2 :(得分:0)
试试这个,
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.row_1:
break;
case R.id.row_2:
break;
}
}
答案 3 :(得分:0)
可能为时已晚,但是现在我遇到了同样的问题,并且从这里给出的答案中得出了一个混合结果。这就是我使用许多按钮进行操作的方式(它对于其他任何组件也都适用)。
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);
}
希望对任何人都有帮助=)