更改自定义适配器上的边距onClick回调无法正常工作,android(Changing the margin in a custom adapter onClick callback not working, android)

我正在使用自定义适配器显示ListView 。 在视图中我有一个textView和一个布局中的按钮。

应该发生什么 - >一旦我点击文本视图, TextView的自定义适配器类中的onClick回调设置了button_layout的边距,就像这样

View button_layout = (((View)(View)v.getParent()).getParent()).findViewById(R.id.button_layout)); MarginLayoutParams margins=(MarginLayoutParams)button_layout.getLayoutParams(); margins.bottomMargin=-100;

但这没有发生。我能够改变背景颜色。 但无法改变底部保证金。 customadapter是较大代码的一部分,我无法透露。

该应用程序不会崩溃,但也无法正常工作。 如果我在调试器中看到布局的bottomMargin的值已经改变但它没有反映在UI中:(我已经在这里放了一些代码。假设已经设置了onClickListener。它正在工作,因为正如我说的我可以在单击文本视图时更改布局的背景颜色。

public class MyCustomAdapter extends ArrayAdapter<someClass> implements OnClickListener{ public View getView(int position, View convertView, ViewGroup parent) { } public void onClick(View v){ View row_to_hide = (((View ((View)v.getParent()).getParent()).findViewById(R.id.row_to_hide));` MarginLayoutParams margins=(MarginLayoutParams)row_to_hide.getLayoutParams();` margins.bottomMargin=-100;` } }

我是android的新手,想知道这种方法是否存在概念上的错误。 textView和按钮也分别位于Relative布局和Linear布局中。

我正在尝试更改按钮所在的线性布局的边距,以便我可以隐藏按钮。

I am displaying a ListView using a custom adapter. In the view i have a textView and a button in a layout.

What is supposed to happen -> As soon as i click on the text view the onClick callback in the custom adapter class for the TextView sets the margin for the button_layout like so

View button_layout = (((View)(View)v.getParent()).getParent()).findViewById(R.id.button_layout)); MarginLayoutParams margins=(MarginLayoutParams)button_layout.getLayoutParams(); margins.bottomMargin=-100;

But this is not happening.I am able to change the background color. But not able to change the bottom margin. The customadapter is a part of the larger code which i cannot disclose.

The app doesnt crash but doesnt work either. If i see in the debugger the value of bottomMargin for the layout has changed but it is not reflected in the UI :( I have put some part of the code here. assume that the onClickListener has been set. It is working because as i said i am able to change the background color of the layout on Clicking the Text View.

public class MyCustomAdapter extends ArrayAdapter<someClass> implements OnClickListener{ public View getView(int position, View convertView, ViewGroup parent) { } public void onClick(View v){ View row_to_hide = (((View ((View)v.getParent()).getParent()).findViewById(R.id.row_to_hide));` MarginLayoutParams margins=(MarginLayoutParams)row_to_hide.getLayoutParams();` margins.bottomMargin=-100;` } }

I am new to android and would like to know if there something conceptually wrong with this approach. Also the textView and the button are in a Relative layout and a Linear layout respectively.

I am trying to change the margins of the linear layout in which the button is situated so that i can hide the button.

最满意答案

您更改了布局参数,但您必须将它们设置回来查看。 因此缺少方法setLayoutParams 。 代码shold看起来像这样:

View button_layout = (((View)(View)v.getParent()).getParent()).findViewById(R.id.button_layout)); MarginLayoutParams margins=(MarginLayoutParams)button_layout.getLayoutParams(); margins.bottomMargin=-100; button_layout.setLayoutParams(margins);

附注:您应该使用getView方法的convertView参数替换((View)(View)v.getParent()).getParent()) 。

You changed layout params but you have to set them back to view. So method setLayoutParams is missing there. Code shold look like this:

View button_layout = (((View)(View)v.getParent()).getParent()).findViewById(R.id.button_layout)); MarginLayoutParams margins=(MarginLayoutParams)button_layout.getLayoutParams(); margins.bottomMargin=-100; button_layout.setLayoutParams(margins);

Side note: You should replace ((View)(View)v.getParent()).getParent()) with convertView parameter from getView method.

更改自定义适配器上的边距onClick回调无法正常工作,android(Changing the margin in a custom adapter onClick callback not working, android)

我正在使用自定义适配器显示ListView 。 在视图中我有一个textView和一个布局中的按钮。

应该发生什么 - >一旦我点击文本视图, TextView的自定义适配器类中的onClick回调设置了button_layout的边距,就像这样

View button_layout = (((View)(View)v.getParent()).getParent()).findViewById(R.id.button_layout)); MarginLayoutParams margins=(MarginLayoutParams)button_layout.getLayoutParams(); margins.bottomMargin=-100;

但这没有发生。我能够改变背景颜色。 但无法改变底部保证金。 customadapter是较大代码的一部分,我无法透露。

该应用程序不会崩溃,但也无法正常工作。 如果我在调试器中看到布局的bottomMargin的值已经改变但它没有反映在UI中:(我已经在这里放了一些代码。假设已经设置了onClickListener。它正在工作,因为正如我说的我可以在单击文本视图时更改布局的背景颜色。

public class MyCustomAdapter extends ArrayAdapter<someClass> implements OnClickListener{ public View getView(int position, View convertView, ViewGroup parent) { } public void onClick(View v){ View row_to_hide = (((View ((View)v.getParent()).getParent()).findViewById(R.id.row_to_hide));` MarginLayoutParams margins=(MarginLayoutParams)row_to_hide.getLayoutParams();` margins.bottomMargin=-100;` } }

我是android的新手,想知道这种方法是否存在概念上的错误。 textView和按钮也分别位于Relative布局和Linear布局中。

我正在尝试更改按钮所在的线性布局的边距,以便我可以隐藏按钮。

I am displaying a ListView using a custom adapter. In the view i have a textView and a button in a layout.

What is supposed to happen -> As soon as i click on the text view the onClick callback in the custom adapter class for the TextView sets the margin for the button_layout like so

View button_layout = (((View)(View)v.getParent()).getParent()).findViewById(R.id.button_layout)); MarginLayoutParams margins=(MarginLayoutParams)button_layout.getLayoutParams(); margins.bottomMargin=-100;

But this is not happening.I am able to change the background color. But not able to change the bottom margin. The customadapter is a part of the larger code which i cannot disclose.

The app doesnt crash but doesnt work either. If i see in the debugger the value of bottomMargin for the layout has changed but it is not reflected in the UI :( I have put some part of the code here. assume that the onClickListener has been set. It is working because as i said i am able to change the background color of the layout on Clicking the Text View.

public class MyCustomAdapter extends ArrayAdapter<someClass> implements OnClickListener{ public View getView(int position, View convertView, ViewGroup parent) { } public void onClick(View v){ View row_to_hide = (((View ((View)v.getParent()).getParent()).findViewById(R.id.row_to_hide));` MarginLayoutParams margins=(MarginLayoutParams)row_to_hide.getLayoutParams();` margins.bottomMargin=-100;` } }

I am new to android and would like to know if there something conceptually wrong with this approach. Also the textView and the button are in a Relative layout and a Linear layout respectively.

I am trying to change the margins of the linear layout in which the button is situated so that i can hide the button.

最满意答案

您更改了布局参数,但您必须将它们设置回来查看。 因此缺少方法setLayoutParams 。 代码shold看起来像这样:

View button_layout = (((View)(View)v.getParent()).getParent()).findViewById(R.id.button_layout)); MarginLayoutParams margins=(MarginLayoutParams)button_layout.getLayoutParams(); margins.bottomMargin=-100; button_layout.setLayoutParams(margins);

附注:您应该使用getView方法的convertView参数替换((View)(View)v.getParent()).getParent()) 。

You changed layout params but you have to set them back to view. So method setLayoutParams is missing there. Code shold look like this:

View button_layout = (((View)(View)v.getParent()).getParent()).findViewById(R.id.button_layout)); MarginLayoutParams margins=(MarginLayoutParams)button_layout.getLayoutParams(); margins.bottomMargin=-100; button_layout.setLayoutParams(margins);

Side note: You should replace ((View)(View)v.getParent()).getParent()) with convertView parameter from getView method.