我想创建一个包含上一个和下一个按钮的类。该类将有助于减少我必须为每个类中的每个按钮编写的重复代码的数量。例如,在 FirstPage.class 片段内调用以下代码。
PageTransition.pageContents(CaseContent.getSize(),getArguments()。getString(ARG_ITEM_ID),button_next,button_prev);
下一个和上一个按钮存储在我在每个布局中调用的header.xml中。我为我的代码使用了主 - 详细信息视图。
我尝试在我的课程中使用 CaseListActivity 中的相同代码。但是,当我尝试将代码添加到 PageTransition 中的 nextPage()方法时,我遇到了 getSupportFragmentManager 的问题。
Bundle arguments = new Bundle();片段片段; arguments.putString(FirstPage.ARG_ITEM_ID,id); fragment = new 第一页(); fragment.setArguments(参数); getSupportFragmentManager()。的BeginTransaction()
.replace(R.id.case_detail_container,fragment).commit();
有没有更好的方法来构建我的过渡类?
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
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
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);
}
}
答案 0 :(得分:0)
PageTransition.pageContents( this ,CaseContent.getSize(),“ formA ”, getArguments()。getString(ARG_ITEM_ID),button_next,button_prev);
然后在项目列表中,每当用户点击其中一个项目列表菜单时,我都会添加此代码。
Fragment fragment = PageTransition.init(id,“formA”); getSupportFragmentManager()。的BeginTransaction() .replace(R.id.case_detail_container,片段) .commit();
<强> 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) {}
}