我尽可能快地以最好的方式解释我的问题。
我正在使用android上的片段,其中一个片段拥有一个TableLayout,我将填充运行时对象。行布局正在膨胀为.xml分隔,其中有一个按钮。单击此按钮可以从表中排除该行。
怀疑是,如果在应用程序运行期间创建该按钮,我将如何将此功能传递给该按钮?
这是行布局:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bordas">
<TextView
android:id="@+id/tv_numero_sacola"
style="@style/TextoCorpoTabela" />
<TextView
android:id="@+id/tv_peso_sacola"
style="@style/TextoCorpoTabela" />
<ImageButton
android:id="@+id/bt_excluir"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_lixeira" />
</TableRow>
这是填充表格的方法:
public void preencheTabelaSacola() {
TableLayout tableLayout = (TableLayout) viewPai.findViewById(R.id.tl_sacola);
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TableRow novaLinha = (TableRow) layoutInflater.inflate(R.layout.layout_linha_tabela_sacola, null);
TextView tvNumeroSacola = (TextView) novaLinha.findViewById(R.id.tv_numero_sacola);
tvNumeroSacola.setText(numeroSacola);
TextView tvPesoSacola = (TextView) novaLinha.findViewById(R.id.tv_peso_sacola);
float peso = itemSacola.getPeso();
tvPesoSacola.setText(String.format("%.2f", peso));
ImageButton excluir = (ImageButton) novaLinha.findViewById(R.id.bt_excluir);
excluir.setOnClickListener(this);
Toast.makeText(contexto, "Sacola " + numeroSacola + " Pesando " + pesoSacola + "(Kg) foi adicionada.", Toast.LENGTH_LONG).show();
//Adiciona Linha
tableLayout.addView(novaLinha);
}
答案 0 :(得分:0)
您可以在运行时设置视图的ID,然后在Button
上设置onClickListenerpublic class Utils {
private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
public static int generateViewId() {
for (;;) {
final int result = sNextGeneratedId.get();
int newValue = result + 1;
if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
if (sNextGeneratedId.compareAndSet(result, newValue)) {
return result;
}
}
}
}
现在按照以下方式设置视图的ID,例如
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
{
textViewId= Utils.generateViewId();
}
else
{
textViewId= View.generateViewId();
}
吨
答案 1 :(得分:0)
我设法以一种非常简单的方式解决了我的问题:
Observable.FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>(
h => yourModel.propertyChanged += h,
h => yourModel.propertyChanged -= h)
.Where(x => x.EventArgs.PropertyName = "your_property_name");
}