所以我试图能够从我自己制作的自定义适配器中更改列表项的外观。当我从自定义适配器的列表项中单击一个按钮时,我希望它能够改变。 This is an example of a Custom List Item looks like. 当我单击Button时,我希望它执行一个操作来修改布局。例如,单击按钮后,我希望它隐藏另一个按钮,或更改按钮的文本。出于某种原因,单击它们时这些命令似乎没有任何效果。
我的最终目标是能够在按钮点击时调整列表项的大小。但正如我所提到的,似乎没有任何效果。在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>
答案 0 :(得分:0)
尝试如下:
class CustomAdapter extends ArrayAdapter<Player> {
private static class ViewHolder {
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) {
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater infater = LayoutInflater.from(getContext());
final View customView = infater.inflate(R.layout.custom_list_item, parent, false);
viewHolder.button = (Button) customView.findViewById(R.id.button);
viewHolder.button2 = (Button) customView.findViewById(R.id.button2);
viewHolder.button.setTag(position);
viewHolder.button2.setTag(position);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.button2.setVisibility(View.VISIBLE);
viewHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewHolder.button2.setVisibility(View.GONE); //Doesn't take effect
notifyItemChanged(Integer.valueOf(v.getTag().toString()));
}
});
return customView;
}
}
答案 1 :(得分:0)
调用notifyDataSetChanged()会导致ListView重建所有内容。它将删除其setVisibility,为所有可见的项调用getView()。 为此,您需要一个逻辑并管理按钮的可见性。
答案 2 :(得分:0)
这种情况正在发生,因为您在onClick
内部使按钮不可见,然后通知您的适配器。
现在您在适配器中写了button2.setVisibility(View.VISIBLE);
。所以每当你的适配器得到通知,它将再次将按钮的可见性设置为可见。
简单的方法是使用SparseBooleanArray
。
class CustomAdapter extends ArrayAdapter<Player> {
Button button, button2;
SparseBooleanArray mArray;
public CustomAdapter(Context context, ArrayList<Player> players) {
super(context, R.layout.player_expanded, players);
mArray = new SparseBooleanArray(lenth of your list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
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);
}
if(mArray.get(position)){
button2.setVisibility(View.GONE);
}else if(!mArray.get(position)){
button2.setVisibility(View.VISIBLE);
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mArray.put(position, true);
notifyDataSetChanged();
}
});
return customView;
}
}
希望这对你有用。