我有一个 Activity 和两个 fragment - Fragment1 和 Fragment2。当应用程序启动时,Fragment1 附加到我的 Activity ,此 fragment 由一个 ExpandableListView 组成。当我单击任何子项时,它会更改 subview 的背景颜色,并且 Fragment1 会被 Fragment2 替换。当我从 Fragment2 返回到 Fragment 1 时,它应该会显示已更改的 childView 背景颜色。如何实现它?
这就是我所做的, 我的 fragment 1 代码:-
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.rooms_frag, container, false);
ExpandableListView expandableListView = (ExpandableListView)view.findViewById(R.id.exp_list);
MoviesAdapter adapter = new MoviesAdapter(getActivity());
expandableListView.setAdapter(adapter);
if(flag)
{
flag = false;
}
else
{
Toast.makeText(getActivity(),""+ MainActivity.grpPosition,Toast.LENGTH_SHORT).show();
expandableListView.expandGroup(MainActivity.grpPosition);
// myView is a static TextView declared in the fragment class itself
myView.setBackgroundColor(Color.rgb(245, 245, 245));//Here I have tried to change the background color but does not get changed(No errors or warnings either)
}
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
MainActivity.grpPosition = groupPosition;
MainActivity.chldPosition = childPosition;
SpotlightsFragment myFrag = new SpotlightsFragment();
String childName = parent.getExpandableListAdapter().getChild(groupPosition, childPosition).toString();
String parentName = parent.getExpandableListAdapter().getGroup(groupPosition).toString();
TextView childView = (TextView) v.findViewById(R.id.child_txt);
myView = childView;
childView.setBackgroundColor(Color.rgb(245, 245, 245));
return false;
}
});
return view;
}
fragment 2:-
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment1 myFrag = new Fragment1();
FragmentTransaction transaction = MainActivity.fragmentManager.beginTransaction();
transaction.replace(R.id.frag, myFrag);
transaction.commit();
}
});
最佳答案
首先,我建议由 Activity (在本例中为 MainActivity)管理 fragment 之间的转换。一个 fragment 越少越好。
您应该创建一个接口(interface),让 Activity 实现该接口(interface),第二个 fragment 将通过转换为该接口(interface)的 getActivity() 调用接口(interface)方法。 在界面中,您可以在 fragment 之间切换。
应该是这样的
返回列表.java
public interface GoBackToList {
public void andHighlightPosition(int group, int position);
}
主 Activity .java
public class MainActivity implements GoBackToList {
...
private Fragment1 f1;
@Override
public void andHighlightPosition(int group, int position) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frag, f1);
transaction.commit();
f1.highlight(group, position);
}
}
fragment 1.java
public class Fragment1 extends Fragment {
...
private int previousHighlightGroup;
private int previousHighlightPosition;
public void highlight(int group, int position) {
// Reset previous highlight color
expandableListView.getExpandableListAdapter().getChild(previousHighlightGroup, previousHighlightPosition)
.findViewById(R.id.child_txt).setBackgroundColor(ORIGINAL_COLOR);
// Highlight new view
expandableListView.getExpandableListAdapter().getChild(group, position)
.findViewById(R.id.child_txt).setBackgroundColor(HIGHLIGHT_COLOR);
// Store values
previousHighlightGroup = group;
previousHighlightPosition = position;
}
}
fragment 2.java
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((GoBackToList)getActivity())).andHighlight(currentGroup, currentPosition);
}
});
关于android - 在 onChildClick() 方法之外的 Expandable ListView Android 中更改被点击子项的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32044616/