在选项卡中的列表视图中刷新活动 (Android)
Posted
技术标签:
【中文标题】在选项卡中的列表视图中刷新活动 (Android)【英文标题】:Refresh activity in List-View in Tab (Android) 【发布时间】:2018-12-19 23:23:29 【问题描述】:我在选项卡活动的选项卡中有一个列表视图。假设 Activity 的名称是“Main”。我想要它,所以当我按下列表视图中的一个项目(它在一个选项卡中,它在一个活动中)时,它会刷新“主要”(整体活动)。
【问题讨论】:
【参考方案1】:使用 onItemClickListener() 作为您的列表视图,如下所示。
在下面的例子中,当点击“Item 1”时,MainActivity 将重新启动。
ListView listView=findViewById(R.id.list_view);
List<String> mList= Arrays.asList("Item 1","Item 2","Item 3");
listView.setAdapter(new ListViewAdapter(this,R.layout.list_item_layout,mList));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
//Restart activity when Item 1 gets clicked
if("Item 1".equals(parent.getAdapter().getItem(position).toString()))
startActivity(new Intent(MainActivity.this,MainActivity.class));
finish();
);
示例:ListViewAdapter.class
public class ListViewAdapter extends ArrayAdapter<String>
private List<String> mList;
public ListViewAdapter(@NonNull Context context, int resource, @NonNull List<String> list)
super(context, resource, list);
this.mList = list;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
if (convertView == null)
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_layout,parent,false);
((TextView) convertView.findViewById(R.id.tv_item)).setText(getItem(position));
return convertView;
@Nullable
@Override
public String getItem(int position)
return mList.get(position);
考虑到您在选项卡式活动中使用 FragmentPagerAdapter, 您可以将ListView的代码放在相应片段实例的onCreateView()中,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ListView listView=rootView.findViewById(R.id.list_view);
List<String> mList= Arrays.asList("Item 1","Item 2","Item 3");
listView.setAdapter(new ListViewAdapter(rootView.getContext(),R.layout.list_item_layout,mList));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
//Restart activity when Item 1 gets clicked
if("Item 1".equals(parent.getAdapter().getItem(position).toString()))
if(getActivity()!=null)
startActivity(new Intent(getActivity(),MainActivity.class));
getActivity().finish();
);
return rootView;
【讨论】:
以上是关于在选项卡中的列表视图中刷新活动 (Android)的主要内容,如果未能解决你的问题,请参考以下文章