当用户点击详细片段中的按钮时突出显示CaseListActivity中的项目
问题描述:
我正在使用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 ;
}
....
}
答
我无法找到关于如何从其他活动直接访问回收站视图的解决方案。相反,我调用CaseListActivity(与回收者视图相同的文件)中的下一个&预先按钮,然后在ViewHolder内部,我设置了NextClick 012LLprev onClickedListeners。我不知道这是否是正确的方法......但它完成了这项工作。