我有Fragment
,其中包含ListView
。 ListView
项包含TextView
和Button
个元素。当我按下列表视图中的按钮时,我想更新我单击按钮的行以隐藏按钮并显示TextView
(TextView
内的文本也需要每次更新一次秒)。
这可能吗?如何?
MyFragment
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);
}
}
答案 0 :(得分:1)
您可以在片段中获取按钮单击事件,然后将布尔变量设置为模型,并根据适配器中的显示/隐藏文本视图。
答案 1 :(得分:1)
我会以更可靠的方式做到这一点。我想你知道Listview和适配器的工作原理。
因此,您有要传递给适配器的Items列表。我将按以下方式更改我的Item类。请在Item类中添加这两个字段,我认为这是模型
bindings.bind(article.category).all((path, value) -> Optional.of(path.in(value)));
开头创建标题和setter。或者更改任何默认值
现在在适配器的getView方法中最后执行此操作//或者只是复制替换
public boolean isButtonVisible = true;
public boolean isTextViewVisible = false;
最后在您的片段中按照以下方法
执行此操作 @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;
}
重要提示:您需要调用myListAdapter.notifyDataSetChanged() 结束。你不需要调用myListAdapter.displayTextView();如 这不起作用