0
我正在使用android studio的主從佈局。當頁面第一次加載或用戶點擊菜單項時,我已設法突出顯示菜單項。對於我的詳細信息頁面,我添加了兩個按鈕,「下一個」和「上一個」,使用戶在用戶點擊菜單項時可以瀏覽菜單項。我的問題是如何讓菜單項突出顯示與詳細信息頁同步?我設法讓正確項目突出顯示,當我旋轉設備。當用戶點擊詳細片段中的按鈕時突出顯示CaseListActivity中的項目
MainFragment.java
public class Keputusan extends Fragment {
...
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final TextView button_prev = (TextView) view.findViewById(R.id.btn_prev);
TextView button_next = (TextView) view.findViewById(R.id.btn_nxt);
PageTransition.pageContents(this, CaseContent.getSize(), "formA",
getArguments().getString(ARG_ITEM_ID), button_next, button_prev);
}
}
CaseListActivity.java
public class CaseListActivity extends BaseActivity {
int selectedItem = 0; //current item on list
...
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.mItem = mValues.get(position);
holder.mContentView.setText(mValues.get(position).content);
holder.mIcon.setText(mValues.get(position).icon);
if(selectedItem == position || position == PageTransition.getItemPosition()) {
holder.mView.setSelected(true);
//Change text color when highlighted
holder.mContentView.setTextColor(ContextCompat.getColor(getApplicationContext(),
R.color.colorPrimary));
}
else {
holder.mView.setSelected(false);
holder.mContentView.setTextColor(ContextCompat.getColor(getApplicationContext(), android.R.color.black));
}
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedItem = position;
onSelectedMenuItem(holder.mItem.id, v);
notifyDataSetChanged(); //make system recognize selectedItem = position
}
});
}
...
}
PageTransition.java
public class PageTransition {
public static final String TAG = PageTransition.class.getSimpleName();
private static int pageId;
....
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();
}
});
}
//highlighted menu items in next & prev
public static int getItemPosition() {
//since position starts with "1"
Log.d(TAG, "getId: pageID " + pageId);
return pageId - 1 ;
}
....
}