简体   繁体   中英

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

I have one Activity and two fragments - Fragment1 and Fragment2. When the app starts Fragment1 gets attached to my activity, this fragment consists of a ExpandableListView. When I click on any of the childs it changes the background color of the childview and Fragment1 gets replaced with Fragment2. When I go back from my Fragment2 to Fragment one, it should show me the changed childView background colour.How do I achieve it?

This is what I have done, My Fragment 1 code :-

     @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;
    }

Fragment2 :-

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

First of all, I would recommend the transition betweeen fragments to be managed by the activity (MainActivity in this case). The least one fragment knows the other, the better.

You should make an interface, make the activity implement that interface and the second fragment will call the interface methods through getActivity() casted to that interface. In the interface you can change between fragments.

That would be something like this

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

When you are replacing the first fragment with the second, you should store the required which index was clicked in onSaveInstanceState() . In that way, when your fragment is created again when you return to it, the index can be found in the savedInstanceState bundle. And then, using that index, you can set the color of that item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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