Android 两个Fragment之间如何传递数据

分类:Android     发布:2019-12-31     来源:本站     浏览:1016 次

有这样一个场景,一个列表页,一个内容页,实现方式是用Activity+2个Fragment实现。目前想实现,点击列表页中的某一项,跳转到内容页。

一、代码结构

主Activity:MainActivity.java

列表页:HomeFragment.java

内容页:NewsInfoFragment.java

二、实现方式

由于Fragment之间不能直接通信,必须通过Activity来完成,具体步骤如下:

1.在HomeFragment中定义通信接口,通过该接口向Activity发送数据。

public class HomeFragment extends Fragment implements CustomListView.LoadListener {
    View view;
    private CustomListView listView;
    private List<Article> list = new ArrayList<>();
    private ArticleAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.page_home, container, false);
        adapter = new ArticleAdapter(view.getContext(), R.layout.listview_item, list);
        listView = view.findViewById(R.id.list_view);
        listView.setInteface(HomeFragment.this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Article article = (Article) adapter.getItem(position - 1);
                Toast.makeText(view.getContext(), position + " " + article.getTitle(), Toast.LENGTH_SHORT).show();
                listener.onSelected(article);
            }
        });
        return view;
    }
    private onItemClickListener listener;
    public interface onItemClickListener {
        void onSelected(Article article);
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            listener = (onItemClickListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement onOkButtonPressed");
        }
    }
}

2. 在Activity中实现该接口,并通过该接口向NewsInfoFragment.java传递数据。

public class MainActivity extends Activity implements View.OnClickListener, HomeFragment.onItemClickListener {
//其他代码... ... 
@Override
public void onSelected(Article article) {
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    hideFragment(transaction);
    if (newsInfoFragment == null) {
        newsInfoFragment = NewsInfoFragment.getInstance(article);
        transaction.add(R.id.fl_content, newsInfoFragment);
    } else {
        newsInfoFragment.Load(article);
        transaction.show(newsInfoFragment);
    }
    transaction.commit();
}
} 

3.NewsInfoFragment接收数据

package com.rc.bottombar.ui;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.rc.bottombar.R;
import com.rc.bottombar.model.Article;

public class NewsInfoFragment extends Fragment {
    private static Article _article;
    View view;
    TextView title;

    //从另外一个Fragment传递过来,供第一次加载
    public static NewsInfoFragment getInstance(Article article) {
        NewsInfoFragment fragment = new NewsInfoFragment();
        Bundle bundle = new Bundle();
        _article = article;
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.page_newsinfo, container, false);
        Load(_article);
        return view;
    }

    //外部调用,供更换文章,非第一次加载
    public void Load(Article article) {
        title = view.findViewById(R.id.txt_home_title);
        if (article != null) {
            title.setText(article.getTitle());
        }
    }

}

参考文章:

https://www.jb51.net/article/100765.htm

如果觉得文章对您有帮助,您可以对我进行打赏 ¥1.88 元(金额随机^_^,每次刷新金额不同)。

请使用支付宝扫码支付

留言评论

*称  呼:
*联系方式: 方便与您取得联系,推荐使用邮箱。
*内  容:

已有评论

暂无数据

上一篇:Android ListView图文并排+下拉刷新+上拉加载更多

下一篇:流动人员人事档案基础信息采集规范