简体   繁体   English

为主从视图片段创建“下一个”和“上一个”方法的类

[英]Create a class for “Next” & “Previous” method for Master-Detail view fragments

I wanted to create a class that holds my previous and next button. 我想创建一个包含上一个和下一个按钮的类。 The class will help to reduce the number of repeating codes that I had to write for each button in each class. 该类将有助于减少我必须为每个类中的每个按钮编写的重复代码的数量。 For example, the following code is called inside FirstPage.class fragment. 例如,在FirstPage.class片段内部调用以下代码。

PageTransition.pageContents(CaseContent.getSize(), getArguments().getString(ARG_ITEM_ID), button_next, button_prev); PageTransition.pageContents(CaseContent.getSize(),getArguments()。getString(ARG_ITEM_ID),button_next,button_prev);

The next and previous buttons are stored in the header.xml which I've called in each layout. 下一个和上一个按钮存储在header.xml中,在每个布局中我都调用了它。 I used a master-detail view for my codes. 我为代码使用了主从视图。

I tried to use the same code in CaseListActivity for my class. 我试图为我的课程在CaseListActivity中使用相同的代码。 However, I have problem with getSupportFragmentManager when I tried to add the codes to nextPage() method inside PageTransition . 但是,我有一个问题getSupportFragmentManager当我试图把代码添加到里面下一页PageTransition()方法。

Bundle arguments = new Bundle(); 捆绑参数= new Bundle(); Fragment fragment; 片段片段; arguments.putString(FirstPage.ARG_ITEM_ID, id); arguments.putString(FirstPage.ARG_ITEM_ID,id); fragment = new FirstPage(); 片段= new FirstPage(); fragment.setArguments(arguments); fragment.setArguments(arguments); getSupportFragmentManager().beginTransaction() getSupportFragmentManager()。beginTransaction()
.replace(R.id.case_detail_container, fragment) .commit(); .replace(R.id.case_detail_container,片段).commit();

Is there a better way to construct my transition class? 有没有更好的方法来构造我的过渡班?

CaseListActivity.java CaseListActivity.java

public class CaseListActivity extends AppCompatActivity {

    ....

    //Method:- direct user to selected Item page

        void onSelectedMenuItem(String id, View v) {

            Bundle arguments = new Bundle();
            Fragment fragment;

            //in landscape
            switch (id) {
                case "1":
                    arguments.putString(FirstPage.ARG_ITEM_ID, id);
                    fragment = new FirstPage();
                    break;

                ... // case continues untill 12

                    break;
                case "11":
                    arguments.putString(ElevethPage.ARG_ITEM_ID, id);
                    fragment = new ElevethPage();

                    break;
                case "12":
                    arguments.putString(LastPage.ARG_ITEM_ID, id);
                    fragment = new LastPage();

                    break;
                default:
                    arguments.putString(FirstPage.ARG_ITEM_ID, id);
                    fragment = new FirstPage();
                    break;
            }

            fragment.setArguments(arguments);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.case_detail_container, fragment)
                    .commit();

            //in potrait
            if (onePane) {

                //hide recyclerview list and show containerview
                final DisplayMetrics displayMetrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

                assert recyclerView != null;
                final ViewGroup.LayoutParams paramsRecyclerView = recyclerView.getLayoutParams();
                assert containerView != null;
                final ViewGroup.LayoutParams paramsContainerView = containerView.getLayoutParams();
                paramsRecyclerView.width = 0;
                paramsContainerView.width = displayMetrics.widthPixels;
                recyclerView.setLayoutParams(paramsRecyclerView);
                containerView.setLayoutParams(paramsContainerView);

                //show expand recylcerview list button
                ActionButton.setVisibility(View.VISIBLE);

            }
        }

        ....
    }

FirstPage.java FirstPage.java

public class FirstPage extends Fragment {

public static final String ARG_ITEM_ID = "item_id";
...

public void onViewCreated(final View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

//      TODO: next & prev button
        TextView button_prev = (TextView) view.findViewById(R.id.btn_prev);
        TextView button_next = (TextView) view.findViewById(R.id.btn_nxt);
        PageTransition.pageContents(CaseContent.getSize(), getArguments().getString(ARG_ITEM_ID), button_next, button_prev);
        ....
    }

}

PageTransition.java PageTransition.java

public class PageTransition {

    public static final String TAG = PageTransition.class.getSimpleName();

    //take number of pages
    public static void pageContents(int totalPage, String id, TextView next, TextView previous) {
        //change string string to int
        int pageID = Integer.parseInt(id);
        final int prevID, nextID; // id for next & prev page

        if(pageID == 1) {
            previous.setVisibility(View.GONE); //hide prev button onn 1st page

            prevID = 0;
            nextID = ++pageID;

            next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    nextPage(nextID);
                }
            });

            Log.d(TAG, "pageContents: first page");

        } else if (pageID < totalPage - 1 && pageID !=1) {
            prevID = --pageID;
            nextID = 2+pageID;

            next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    nextPage(nextID);
                }
            });

            previous.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    prevPage(prevID);
                }
            });


        } else if (pageID == totalPage) {
            next.setVisibility(View.GONE); //hide next button on last page

            prevID = --pageID;
            nextID = 0;

            previous.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    prevPage(prevID);
                }
            });
        }

    }

    private static void nextPage(int id) {

        Log.d(TAG, "nextPage: id " + id);

    }

    private static void prevPage(int id) {

        Log.d(TAG, "prevPage: id " + id);

    }
}

After some thoughts, I finally able to work it out. 经过一番思考,我终于能够解决它。 I've to sent the current fragment, which hold the "next" and "prev" buttons to the class. 我必须发送当前片段,该片段包含该类的“下一步”和“上一步”按钮。 Only then, i'm able to use getFragmentManager . 只有这样,我才能使用getFragmentManager I use the following code inside FirstPage.java to set the "next" & "pre" buttons. 我在FirstPage.java中使用以下代码来设置“下一个”和“上一个”按钮。

PageTransition.pageContents( this , CaseContent.getSize(), " formA ", getArguments().getString(ARG_ITEM_ID), button_next, button_prev); PageTransition.pageContents( this ,CaseContent.getSize(),“ formA ”,getArguments()。getString(ARG_ITEM_ID),button_next,button_prev);

Then on item list, I add this code whenever use clicked on one of the item list menu. 然后在项目列表中,只要在项目列表菜单之一上单击,就添加此代码。

Fragment fragment = PageTransition.init(id, "formA"); 片段片段= PageTransition.init(id,“ formA”); getSupportFragmentManager().beginTransaction() .replace(R.id.case_detail_container, fragment) .commit(); getSupportFragmentManager()。beginTransaction().replace(R.id.case_detail_container,fragment).commit();

PageTransition.java PageTransition.java

public class PageTransition {

    public static final String TAG = PageTransition.class.getSimpleName();
    private static int pageId;

    public static Fragment init(String id, String form) {
        Log.d(TAG, "currentPage: id " + id);
        pageId = Integer.valueOf(id); //set current page Id

        switch(form) {
            case "formA" :
                return formA_caseContent(id);
            case "formB" :
                return formB_caseContent(id);
            case "formC" :
                return formC_caseContent(id);
            default:
                Log.d(TAG, "currentPage: form id " + form + " out of bound.");
                return null;
        }
    }

    public static void pageContents(final Fragment fragment, int totalPage, final String form, String id, TextView next,
                                    TextView previous) {
        //change string string to int
        int pageID = Integer.parseInt(id);
        int prevID = 0, nextID = 0;

        if(pageID == 1) {
            previous.setVisibility(View.GONE); //hide prev button onn 1st page
            nextID = ++pageID;

        } else if (pageID > 1 && pageID < totalPage ) {
            prevID = --pageID;
            nextID = 2+pageID;

        } else if (pageID == totalPage) {
            next.setVisibility(View.GONE); //hide next button on last page
            prevID = --pageID;

        }

        final int finalNextID = nextID;
        next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Fragment _fragment = init(String.valueOf(finalNextID), form);
                    fragment.getFragmentManager().beginTransaction()
                            .replace(R.id.case_detail_container, _fragment).commit();
                }
            });

        final int finalPrevID = prevID;
        previous.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Fragment _fragment = init(String.valueOf(finalPrevID), form);
                    fragment.getFragmentManager().beginTransaction()
                            .replace(R.id.case_detail_container, _fragment).commit();
                }
            });

    }

    private static Fragment formA_caseContent(String id) {

        Bundle arguments = new Bundle();
        Fragment fragment;

        switch (id) {
            case "1":
                arguments.putString(FirstPage.ARG_ITEM_ID, id);
                fragment = new FirstPage();

                break;
            case"2":
                arguments.putString(SecondPage.ARG_ITEM_ID, id);
                fragment = new SecondPage();

                break;
            ....
            default:
                Log.i(TAG, "onSelectedMenuItem: Selection out of bound.");
                arguments.putString(FirstPage.ARG_ITEM_ID, id);
                fragment = new FirstPage();
                break;
        }

        fragment.setArguments(arguments);
        return fragment;
    }

    private static Fragment formB_caseContent(String id) {}

    private static Fragment formc_caseContent(String id) {}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM