本文介绍了维持Backpress事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好, 我有一个片段,其中包含自定义ListView,所有ListView事件都在片段类本身处理。现在,当我单击ListView项目时,我再次根据条件填充相同的ListView。但是,如果我按下后退按钮,我想再次在上一个状态下填写ListView。 这可能吗?如果有,怎么样? 下面是ListView的片段代码 public class HomeFragment extends 片段{ 字符串 CourseName [] = { 特殊课程, 工程, BScIT, BScCS}; 字符串 UniversityEngArray [] = { 孟买, Pune, 德里, 拉贾斯坦邦}; 字符串 UniversityBScITArray [] = { 孟买, Pune, 德里, 拉贾斯坦邦}; 整数ImageId [] = { R.drawable.letters, R.drawable.lettere, R.drawable.letterb, R.drawable.letterb }; private ListView lv; CustomList customList; // Listview适配器 ArrayAdapter< String>适配器; public HomeFragment(){ // 必需的空公共构造函数 } @覆盖 public void onCreate(Bundle savedInstanceState){ super .onCreate(savedInstanceState); } @ Override public 查看onCreateView(LayoutInflater inflater,ViewGroup容器, Bundle savedInstanceState){查看rootView = inflater.inflate(R.layout.fragment_home,container,false); customList = new CustomList(getActivity(),CourseName,ImageId); lv =(ListView)rootView.findViewById(R.id.listView1); lv.setAdapter(customList); lv.setOnItemClickListener( new AdapterView.OnItemClickListener(){ @ Override public void onItemClick(AdapterView<?> parent,View view, int position, long id){ // Toast.makeText(getActivity(),You Clicked at+ CourseName [+ position],Toast.LENGTH_SHORT).show(); if (CourseName [+ position] .toString()。equalsIgnoreCase( 工程)){ customList = new CustomList(getActivity(),UniversityEngArray,ImageId); lv.setAdapter(customList); } else if (CourseName [+ position] .toString()。equalsIgnoreCase( BScIT)){ customList = new CustomList(getActivity(),UniversityBScITArray,ImageId); lv.setAdapter(customList); } } }); // 为此片段的布局充气 return rootView; } @覆盖 public void onAttach(活动活动){ super .onAttach(activity); } @覆盖 public void onDetach(){ super .onDetach(); } 解决方案 当然可以,但是你应该有一个带历史记录的状态机。最好是操纵数据,而不是UI。 UI应该简单地从数据中填充UI,比如来自历史的数据。由于历史可能需要大量数据,因此您可能需要保留它。抱歉,进一步的细节取决于您对数据的使用,数据库的使用或其他等等。 -SA Hello,I have a Fragment Which has a Custom ListView and All the ListView Events is Handled in Fragment Class Itself. Now When I Click On The ListView Item I Am Populating Same ListView Again Based On Condition. But If I Press Back Button I Want to again Fill ListView on Previous State.Is this Possible? If yes how? Below is the Code For Fragment With ListViewpublic class HomeFragment extends Fragment { String CourseName[]={"Special Courses","Engineering","BScIT","BScCS"}; String UniversityEngArray[]={"Mumbai","Pune","Delhi","Rajasthan"}; String UniversityBScITArray[]={"Mumbai","Pune","Delhi","Rajasthan"}; Integer ImageId[]={ R.drawable.letters, R.drawable.lettere, R.drawable.letterb, R.drawable.letterb }; private ListView lv; CustomList customList; // Listview Adapter ArrayAdapter<String> adapter; public HomeFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_home, container, false); customList = new CustomList(getActivity(), CourseName, ImageId); lv = (ListView) rootView.findViewById(R.id.listView1); lv.setAdapter(customList); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { //Toast.makeText(getActivity(), "You Clicked at " + CourseName[+position], Toast.LENGTH_SHORT).show(); if(CourseName[+position].toString().equalsIgnoreCase("Engineering")){ customList =new CustomList(getActivity(),UniversityEngArray,ImageId); lv.setAdapter(customList); } else if(CourseName[+position].toString().equalsIgnoreCase("BScIT")){ customList =new CustomList(getActivity(),UniversityBScITArray,ImageId); lv.setAdapter(customList); } } }); // Inflate the layout for this fragment return rootView; } @Override public void onAttach(Activity activity) { super.onAttach(activity); } @Override public void onDetach() { super.onDetach(); } 解决方案 Of course it's possible, but then you should have a state machine with History. It's better to manipulate data, not UI. The UI should simple populate UI from data, say, data from history. As history may need a lot of data, you may need to persist it. Sorry, but further detail depends on what you have for data, the use of databases or something else, and so on.—SA 这篇关于维持Backpress事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-11 20:50