繁体   English   中英

在onChildClick()方法之外更改Expandable ListView Android中被单击子项的背景颜色

[英]Change Background Color of Clicked Child in Expandable ListView Android outside the onChildClick() method

我有一个活动和两个片段-Fragment1和Fragment2。 当应用程序启动时,Fragment1附加到我的活动中,该片段由ExpandableListView组成。 当我单击任何一个child时,它会更改childview的背景颜色,并且Fragment1被Fragment2替换。 当我从Fragment2返回到Fragment 1时,它应该向我显示更改后的childView背景色,如何实现?

这就是我所做的,我的片段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;
    }

片段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();
                }
            });

首先,我建议片段之间的过渡由活动(在这种情况下为MainActivity)管理。 至少一个片段知道另一个更好。

您应该创建一个接口,使活动实现该接口,第二个片段将通过强制转换为该接口的getActivity()调用接口方法。 在界面中,您可以在片段之间进行切换。

那将是这样的

GoBackToList.java

public interface GoBackToList {
    public void andHighlightPosition(int group, int position);
}

MainActivity.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);
    }
}

Fragment1.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;
   }
}

Fragment2.java

backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
        ((GoBackToList)getActivity())).andHighlight(currentGroup, currentPosition);
        }
     });

用第二个片段替换第一个片段时,应将所需的单击索引存储在onSaveInstanceState() 这样,当您返回到片段时再次创建该片段时,可以在savedInstanceState捆绑包中找到索引。 然后,使用该索引,您可以设置该项目的颜色。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM