gpt4 book ai didi

android - 单击后隐藏/显示 ListView 项目中的元素

转载 作者:行者123 更新时间:2023-11-29 18:52:16 24 4
gpt4 key购买 nike

我有一个包含 ListViewFragmentListView 项包含 TextViewButton 元素。当我在 ListView 中按下一个按钮时,我想更新我单击该按钮的行以隐藏该按钮并显示一个 TextView ( TextView 中的文本也需要每秒更新一次)。

这可能吗?如何实现?

我的 fragment
public class MyFragment extends Fragment implements ClickedListener {
private static final String TAG = MyFragment.class.getSimpleName();

private MyListAdapter myListAdapter;

public MyFragment() {
// Required empty public constructor
Log.e(TAG, "MyFragmment");
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e(TAG, "onCreateView");
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_mine, container, false);

ListView myListView = view.findViewById(R.id.listView);

myListAdapter = new MyListAdapter(getActivity(), MyFragment.this);
myListView.setAdapter(myListAdapter);

getList();

return view;
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && isResumed()) {
getList();
}
}

private void getList() { //This part works no need to discuss this.
List<Items> items = myItemsContent.getItems();
myListAdapter.setItems(items);
}

@Override
public void buttonClicked(Item item) {
doSomeStuff();
myListAdapter.displayTextView();
}
}
MyListAdapter
public class MyListAdapter extends ArrayAdapter<Item> {
private static final String TAG = MyListAdapter.class.getSimpleName();

private Button myButton;
private TextView status;

private final Context context;

public interface ClickedListener {
void buttonClicked(Item item);
}

private final ClickedListener clickedListener;

public MyListAdapter(@NonNull Context context, ClickedListener clickedListener) {
super(context, R.layout.list_item);

this.context = context;
this.clickedListener = clickedListener;
}

public void setItems(List<Items> items) {
super.addAll(items);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View row = convertView;

if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item, parent, false);
}

TextView itemName = row.findViewById(R.id.list_item_name);
myButton = row.findViewById(R.id.myButton);
status = row.findViewById(R.id.status);

Item item = getItem(position);
if (item != null) {
itemName.setText(item.getName());

myButton.setOnClickListener(v -> clickedListener.buttonClicked(item));
}

return row;
}

public void displayTextView() {
myButton.setVisibility(View.INVISIBLE);
status.setVisibility(View.VISIBLE);
}
}

最佳答案

我会用更可靠的方式来做。我想您知道 Listview 和适配器的工作原理。

所以您有要传递给适配器的项目列表。我将按以下方式更改我的 Item 类。请在我认为是模型的 Item 类中添加这两个字段

public boolean isButtonVisible = true;
public boolean isTextViewVisible = false;

去创建头文件和 setter。或者更改任何默认值

现在在 Adapter 的 getView 方法中最后执行此操作//或者只是复制替换

    @NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View row = convertView;

if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item, parent, false);
}

TextView itemName = row.findViewById(R.id.list_item_name);
myButton = row.findViewById(R.id.myButton);
status = row.findViewById(R.id.status);

Item item = getItem(position);
if (item != null) {
itemName.setText(item.getName());

myButton.setOnClickListener(v -> clickedListener.buttonClicked(item));

if(item.isButtonVisible){
myButton.setVisiblity(View.VISIBLE);
}else{
myButton.setVisibilty(View.GONE)
}

if(item.isTextViewVisible){
myTextView.setVisiblity(View.VISIBLE);
}else{
myTextView.setVisibilty(View.GONE)
}

}

return row;
}

最后在您的 fragment 中按照以下方法执行此操作

 @Override
public void buttonClicked(Item item) {
doSomeStuff();
item.setButtonVisibility (Whatever you want to do true or false)
ite.setTextViewVisible (Whatever you want to do true or false)
myListAdapter.notifyDataSetChanged();
}

Important: You need to call myListAdapter.notifyDataSetChanged() in the end. you do not need to call myListAdapter.displayTextView(); as this will not work

关于android - 单击后隐藏/显示 ListView 项目中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50714208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com