在可扩展的ListView Android上的onChildClick外单击的儿童更改背景颜色()方法 [英] Change Background Color of Clicked Child in Expandable ListView Android outside the onChildClick() method

查看:176
本文介绍了在可扩展的ListView Android上的onChildClick外单击的儿童更改背景颜色()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动和两个片段 - 片段1和Fragment2。当应用程序启动片段1被连接到我的活动,该片段由一个ExpandableListView的。当我点击任何孩子的它改变了childview的背景颜色和片段1获取与Fragment2取代。当我回去从我Fragment2到分片之一,它应该显示我改变childView背景colour.How我实现呢?

这是我做了什么,
我的片段1 code: -

  @覆盖
        公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState)
        {            查看查看= inflater.inflate(R.layout.rooms_frag,集装箱,FALSE);
            ExpandableListView expandableListView =(ExpandableListView)view.findViewById(R.id.exp_list);
            MoviesAdapter适配器=新MoviesAdapter(getActivity());
            expandableListView.setAdapter(适配器);
            如果(标志)
            {
                标志= FALSE;
            }
            其他
            {                Toast.makeText(getActivity(),+ MainActivity.grpPosition,Toast.LENGTH_SHORT).show();
                expandableListView.expandGroup(MainActivity.grpPosition);
               // MyView的是在片段类本身声明一个静态的TextView
                myView.setBackgroundColor(Color.rgb(245,245,245)); //在这里,我试图改变背景颜色,但不会得到改变(没有错误或警告其一)
            }
            expandableListView.setOnChildClickListener(新ExpandableListView.OnChildClickListener(){                @覆盖
                公共布尔onChildClick(ExpandableListView父视图V,INT groupPosition,诠释childPosition,长ID){
                    MainActivity.grpPosition = groupPosition;
                    MainActivity.chldPosition = childPosition;
                    SpotlightsFragment myFrag =新SpotlightsFragment();
                    。字符串childName = parent.getExpandableListAdapter()getChild(groupPosition,childPosition)的ToString();
                    字符串parentName = parent.getExpandableListAdapter()getGroup(groupPosition)的ToString()。
                    TextView的childView =(TextView中)v.findViewById(R.id.child_txt);
                    MyView的= childView;
                    childView.setBackgroundColor(Color.rgb(245,245,245));
                   返回false;
            }
        });
        返回视图。
    }

Fragment2: -

  backButton.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(查看视图){
        片段1 myFrag =新片段1();
        FragmentTransaction交易= MainActivity.fragmentManager.beginTransaction();
        transaction.replace(R.id.frag,myFrag);
        器transaction.commit();
                }
            });


解决方案

首先,我会建议的过渡betweeen片段的活性(MainActivity在这种情况下)进行管理。在至少一个片段知道对方,就更好了。

您应该让一个接口,使活动实现该接口和第二个片段将调用通过getActivity()接口方法铸造到该接口。
在该界面中可以片段之间改变。

这会是这样的。

GoBackToList.java

 公共接口GoBackToList {
    公共无效andHighlightPosition(INT组,诠释位);
}

MainActivity.java

 公共类MainActivity实现GoBackToList {
    ...
    私人片段1 F1;    @覆盖
    公共无效andHighlightPosition(INT组,INT位置){
        FragmentTransaction交易= fragmentManager.beginTransaction();
        transaction.replace(R.id.frag,F1);
        器transaction.commit();
        f1.highlight(组,位置);
    }
}

Fragment1.java

 公共类片段1片段扩展{
    ...
    私人诠释previousHighlightGroup;
    私人诠释previousHighlightPosition;    公共无效高亮(INT组,INT位置){
    //重置previous高亮颜色
    expandableListView.getExpandableListAdapter()。getChild(previousHighlightGroup,previousHighlightPosition)
            .findViewById(R.id.child_txt).setBackgroundColor(ORIGINAL_COLOR);
    //选中新视图
    expandableListView.getExpandableListAdapter()。getChild(组,位置)
            .findViewById(R.id.child_txt).setBackgroundColor(HIGHLIGHT_COLOR);
    //存储值
    previousHighlightGroup =组;
    previousHighlightPosition =位置;
   }
}

Fragment2.java

  backButton.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(查看视图){
        ((GoBackToList)getActivity()))andHighlight(currentGroup,currentPosition);
        }
     });

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

这篇关于在可扩展的ListView Android上的onChildClick外单击的儿童更改背景颜色()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆