简体   繁体   English

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

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

I have one Activity and two fragments - Fragment1 and Fragment2. 我有一个活动和两个片段-Fragment1和Fragment2。 When the app starts Fragment1 gets attached to my activity, this fragment consists of a ExpandableListView. 当应用程序启动时,Fragment1附加到我的活动中,该片段由ExpandableListView组成。 When I click on any of the childs it changes the background color of the childview and Fragment1 gets replaced with Fragment2. 当我单击任何一个child时,它会更改childview的背景颜色,并且Fragment1被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? 当我从Fragment2返回到Fragment 1时,它应该向我显示更改后的childView背景色,如何实现?

This is what I have done, My Fragment 1 code :- 这就是我所做的,我的片段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;
    }

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

First of all, I would recommend the transition betweeen fragments to be managed by the activity (MainActivity in this case). 首先,我建议片段之间的过渡由活动(在这种情况下为MainActivity)管理。 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. 您应该创建一个接口,使活动实现该接口,第二个片段将通过强制转换为该接口的getActivity()调用接口方法。 In the interface you can change between fragments. 在界面中,您可以在片段之间进行切换。

That would be something like this 那将是这样的

GoBackToList.java GoBackToList.java

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

MainActivity.java 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 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 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() . 用第二个片段替换第一个片段时,应将所需的单击索引存储在onSaveInstanceState() In that way, when your fragment is created again when you return to it, the index can be found in the savedInstanceState bundle. 这样,当您返回到片段时再次创建该片段时,可以在savedInstanceState捆绑包中找到索引。 And then, using that index, you can set the color of that item. 然后,使用该索引,您可以设置该项目的颜色。

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

相关问题 在可扩展ListView Android中更改单击子项的背景颜色 - Change Background Color of Clicked Child in Expandable ListView Android 当在扩展列表视图中单击“子项”中的按钮时,Android更改父项的背景 - Android Change Background of Parent when Button in Child is clicked in Expandable ListView Android-如何更改可扩展listView中单个孩子的背景颜色? - Android - how can change background color of a single child in expandable listView? 如何调用(调用)可扩展listview的onChildClick方法? - how to call (invoke) onChildClick method of expandable listview? 如何仅更改 Android Studio 中 ListView 中单击项的背景颜色? - How to change background color of only the clicked item in ListView in Android Studio? 如何在Android中的可扩展列表视图中单击后禁用子项 - How to disable a child item once clicked in an expandable listview in Android 我可以在android的可扩展列表视图中更改子行的背景颜色吗 - Can I change the background color of child row on expandable list view in android 单击时更改列表视图项的背景颜色 - change the background color of a listview item when clicked Android ListView背景颜色更改 - Android ListView background color change 在ExpandableListView中更改被单击子项的背景颜色 - Change Background Color of Clicked Child in ExpandableListView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM