通过上一次按钮单击更改片段上的内容

时间:2017-05-20 21:47:57

标签: java android android-fragments textview

我是新来的! 希望你能帮助我!

我正在尝试做一些聪明的事情。有我的“Article.java”片段,当我点击我的文章布局(fragment_article.xml)中的Fragment之一时,我希望它带我到另一个ImageButton

问题在于我希望它通过我在Article.java上做出的选择来改变即将到来的片段的内容(对于每个ImageButton,我想要一个不同的文本)。我真的不知道该怎么做。

我想过用我的文本制作一些字符串,并以某种方式使用setText()通过ImageButton选项设置它,但我真的不知道该怎么做。

所以我想我需要你的帮助。 如何通过仅使用一个java类和多个字符串,通过前一个片段上的图像按钮选项更改文本? 也许有更聪明的方法?

另请参阅setTitle()方法,我也希望通过该选项进行更改。

这是Article.java:

public class Article extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_article, container, false);

    }

    public void handleOnBackPress() {
        System.exit(0);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getActivity().setTitle("Home screen: Articles");
        imageButtonListener();

    }

    public void imageButtonListener(){
        ImageButton button1 =(ImageButton) getView().findViewById(R.id.SugarButton);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment = new ArticleFragment();
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.setCustomAnimations(R.anim.bottomtop, R.anim.topbottom,R.anim.popbottomtop,R.anim.poptopbottom);
                fragmentTransaction.replace(R.id.content_main, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
     }
    public void imageButtonListener(){
        ImageButton button2 =(ImageButton) getView().findViewById(R.id.SugarButton);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment = new ArticleFragment();
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.setCustomAnimations(R.anim.bottomtop, R.anim.topbottom,R.anim.popbottomtop,R.anim.poptopbottom);
                fragmentTransaction.replace(R.id.content_main, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
    }
}

这是ArticleFragment.java(具有动态可变文本的一个片段):

public class ArticleFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.articlefragmentviewer, container, false);

    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getActivity().setTitle("article Title");
        TextView article = new TextView(getActivity());
        article.setText("Hey");

    }
}

1 个答案:

答案 0 :(得分:0)

使用一些数据初始化片段只需添加静态工厂方法:

  public class ArticleFragment extends Fragment {

    private static final String ARG_PARAM_TITLE = "title";
    private static final String ARG_PARAM = "content";

    private String title;
    private String content;

    /**
     * Static factory method that takes a String parameter,
     * initializes the fragment's arguments, and returns the
     * new fragment to the client.
     */
    public static ArticleFragment newInstance(String title, String content) {
        ArticleFragment f = new ArticleFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM_TITLE, title);
        args.putString(ARG_PARAM, content);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            title = getArguments().getString(ARG_PARAM_TITLE);
            content = getArguments().getString(ARG_PARAM);
        }
    }

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

        getActivity().setTitle(title);
        TextView tvArticle = (TextView) view.findViewById(R.id.tv_article);
        tvArticle.setText(content);
        return view;
    }
}

为片段提供静态工厂方法是很好的做法,因为它封装并抽象了从客户端设置对象所需的步骤。

要实例化片段,而不是:

Fragment fragment = new ArticleFragment();

你必须使用工厂方法:

Fragment fragment = ArticleFragment.newInstance("Some content goes here");

另外,请注意您需要以“onCreateView”方式初始化视图,而不是“onViewCreated”。

将方法提取到初始化片段:

private void openArticleWith(String title, String content){
            Fragment fragment = new ArticleFragment.newInstance(title, content);
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.setCustomAnimations(R.anim.bottomtop, R.anim.topbottom,R.anim.popbottomtop,R.anim.poptopbottom);
            fragmentTransaction.replace(R.id.content_main, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
}

你的按钮监听器将是:

public void imageButtonListener(){
    ImageButton button1 =(ImageButton) getView().findViewById(R.id.SugarButton);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           openArticleWith("This is title for first button", "This is content for first buttton");
        }
    });
 }