因此,我试图从我自定义的适配器中更改列表项目的外观。当我从我的自定义适配器的列表项中单击按钮时,我希望它会更改。 This is an example of a Custom List Item looks like. 当我点击按钮时,我希望它执行一个操作来修改布局。例如,点击按钮后,我想让它隐藏另一个按钮,或更改按钮的文本。由于某些原因,这些命令在点击时似乎没有效果。在内部按钮上更新自定义适配器内容点击
我的最终目标是能够通过单击按钮来调整列表项目的大小。但正如我所提到的,似乎没有任何效果。在onClick方法之外设置可见性似乎可行,但在内部做不到。我知道代码已经达到,但我认为它与适配器不更新有关,但我在那里有notifyDataSetChanged方法。任何帮助表示赞赏。谢谢。
这里是我的自定义适配器代码
class CustomAdapter extends ArrayAdapter<Player> {
Button button, button2;
public CustomAdapter(Context context, ArrayList<Player> players) {
super(context, R.layout.player_expanded, players);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater infater = LayoutInflater.from(getContext());
final View customView = infater.inflate(R.layout.custom_list_item, parent, false);
button = (Button) customView.findViewById(R.id.button);
button2 = (Button) customView.findViewById(R.id.button2);
button2.setVisibility(View.VISIBLE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button2.setVisibility(View.GONE); //Doesn't take effect
notifyDataSetChanged();
}
});
return customView;
}
}
而且我Custom_List_Item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView3"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:layout_alignParentTop="true"
android:focusable="false"
android:id="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:focusable="false"
android:id="@+id/button2" />
</LinearLayout>
你的button是通过了什么? –
对不起,只是一个错字。固定。 – CrossStitch
android:focusable =“false”删除并尝试 – comeback4you