我使用了android studio的Master-Detail布局。当页面首次加载或用户点击菜单项时,我设法突出显示菜单项。对于我的详细信息页面,我添加了两个按钮," Next"和"以前"这使用户可以在用户点击菜单项时浏览菜单项。我的问题是如何让菜单项与详细信息页面同步突出显示?我设法在旋转设备时突出显示正确的项目。
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 ;
}
....
}
答案 0 :(得分:0)
我无法找到有关如何从其他活动直接访问回收站视图的解决方案。相反,我打电话给下一个&amp;在 CaseListActivity (与Recycler视图相同的文件)中的pre按钮然后,在ViewHolder中,我设置了Next&amp; prev onClickedListeners。我不知道这是不是正确的方式......但它完成了工作。