簡體   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