比如,一个新闻类APP(或者购物类),你点击新闻列表中的某一项后,弹出详细信息界面。这个效果,有两种实现的方法:
1、两个Activity来实现,数据通过Bundle封装然后Intent来传输。(这个不太好,每次关闭返回,列表Activity都会刷新置顶显示,当然可以通过其他手段解决这个问题,不过还是感觉比较麻烦!)
2、两个Fragment(Activity片段)来实现。(通过Fragment栈的addtobackStack和popbackstack来解决上面的问题,很方便!)
(完整Demo地址:https://download.csdn.net/download/lpcrazyboy/10516232)
效果图如下:
主要思路:
1、创建一个NewsListFragment.java继承自Fragment,这个Fragment用于显示ListView,即新闻列表。并且,在该Fragment中给ListView添加OnItemClickListener监听事件,用于触发item点击,然后创建一个NewsContentFragment对象,replace一下。
2、创建一个NewsContentFragment.java继承自Fragment,这个Fragment用于显示新闻的详细内容。获取到ListView中触发点击之后传过来的Bundle对象,接着再获取相应的内容并显示。
3、创建一个MyListAdapter.java继承自BaseAdapter,这个数据适配器用于向ListView中填充内容。(这个很简单,就是自定义个数据适配器)
4、在MainActivity.java中默认显示NewsListFragment对象。
PS:如果出现下面的问题,在类的前面加上@SuppressLint("ValidFragment")即可
use a default constructor plus Fragment#setArguments(Bundle)
主要代码如下:
NewsListFragment.java
package com.deepreality.fragmentcasefourdemo; import android.annotation.SuppressLint; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import java.util.List; @SuppressLint("ValidFragment") public class NewsListFragment extends Fragment implements AdapterView.OnItemClickListener { private ListView lvNewsList; private FragmentManager fragmentManager; private List<Data> dataList; public NewsListFragment() { super(); } public NewsListFragment(FragmentManager fragmentManager, List<Data> dataList) { super(); this.fragmentManager = fragmentManager; this.dataList = dataList; } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fg_newslist, container, false); lvNewsList = view.findViewById(R.id.newslist_lvNewsList); MyListAdapter myListAdapter = new MyListAdapter(getActivity(), dataList); lvNewsList.setAdapter(myListAdapter); lvNewsList.setOnItemClickListener(this); return view; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Bundle bundle = new Bundle(); bundle.putString("content", dataList.get(position).getNew_content()); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); NewsContentFragment newsContentFragment = new NewsContentFragment(bundle); fragmentTransaction.replace(R.id.main_fLayoutFragment, newsContentFragment); //调用addToBackStack将Fragment添加到栈中 fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } }
NewsContentFragment.java
package com.deepreality.fragmentcasefourdemo; import android.annotation.SuppressLint; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; @SuppressLint("ValidFragment") public class NewsContentFragment extends Fragment { private TextView tvNewsContent; private String content; public NewsContentFragment() { super(); } public NewsContentFragment(Bundle bundle) { this.content = bundle.getString("content"); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fg_content, container, false); tvNewsContent = view.findViewById(R.id.content_tvContent); tvNewsContent.setText(content); return view; } }
MainActivity.java
package com.deepreality.fragmentcasefourdemo; import android.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.FrameLayout; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private FrameLayout fLayoutFragment; private List<Data> dataList; private FragmentManager fragmentManager; private long exitTime = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().hide(); setContentView(R.layout.activity_main); fragmentManager = getFragmentManager(); fLayoutFragment = findViewById(R.id.main_fLayoutFragment); dataList = new ArrayList<Data>(); for (int i = 1; i <= 20; i++) { Data data = new Data("新闻标题" + i, i + "~新闻内容~~~~~~~~"); dataList.add(data); } NewsListFragment newsListFragment = new NewsListFragment(fragmentManager, dataList); fragmentManager.beginTransaction().replace(R.id.main_fLayoutFragment, newsListFragment).commit(); } //点击回退键的处理:判断Fragment栈中是否有Fragment //没,双击退出程序,否则像是Toast提示 //有,popbackstack弹出栈 @Override public void onBackPressed() { if (fragmentManager.getBackStackEntryCount() == 0) { if ((System.currentTimeMillis() - exitTime) > 2000) { Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show(); exitTime = System.currentTimeMillis(); } else { super.onBackPressed(); } } else { fragmentManager.popBackStack(); } } }