Android in the ListView setSelection invalid how do?

2019-09-30

Keywords: ListView automatically selected, ListView dynamically change the background of selected items


 

ListView manually check the option when the option is highlighted to indicate the selected state. But sometimes we tend to think in a ListView loaded automatically when you select a specific option, although it has a setSelection (int position) method, but unfortunately when the method is not loaded in the ListView It will work.

 

This method does not take effect specific reasons I did not go to get to the bottom, just because the feeling is ready ListView internal data is asynchronous, we initialize it after the ListView and to set up Adapter, in fact ListView data item is not yet load out, you let it go to select an option, and that nature is not the end.

 

Although I have not actually been to explore the specific reasons for this phenomenon, but it is still found two kinds of "Quxianjiuguo" approach to solve this problem. Hereby record it in order to help needy students.

 

Both approaches rely on us to manually change the state of the data item by listening to the ListView OnItemClick event of ways. For example, I just realized I have this piece of code to call them:

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if(adapterView.getTag() != null) {
            TextView oldTv = (TextView) adapterView.getTag();
            oldTv.setTextColor(ResourcesManager.getColor(R.color.text_color_black_333));
            oldTv.setBackgroundColor(ResourcesManager.getColor(android.R.color.transparent));
        }

        TextView curTv = (TextView) view;
        curTv.setTextColor(ResourcesManager.getColor(android.R.color.white));
        curTv.setBackgroundColor(ResourcesManager.getColor(R.color.basically_color));
        adapterView.setTag(curTv);
    }

When the ListView option is clicked, the first will be taken out from () the objects in the adapterView getTag. What is it inside this object? I was on a set view options for background. The purpose of this is to determine if the option to restore the original has been selected to unselected style.

Then is the currently selected style, that parameter view to set the style they need. Do not forget to set the call is completed at adapterView.setTag () to save this view up to its style recovery operation.

 

Some students may be directly set show style ListView when selected by the layout styles xml xml, this method I have not tried, not within the scope of the discussion of this article.

 

1, a solution

 

Delay.

 

Delay is a relatively simple and crude solution. Its core idea is initialized after the ListView delayed for some time, and then take the initiative to perform at onItemClick () method to monitor implementation. Man of few words said, directly on the code:

        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
        lv.postDelayed(new Runnable() {
            @Override
            public void run() {
                int count = lv.getChildCount();
                if(count > 0) {
                    View v = lv.getChildAt(0);
                    onItemClick(lv, v, 0, 0);
                }
            }
        }, 2000);
        

Here set the Adapter and click on the option to listen directly after a delay of 2000 milliseconds to execute "select" operation in the ListView object. As for the long delay we have to hold their own.

After the end of the delay, for rigorous, have to first determine what the ListView has loaded a sufficient data item, the ListView comes getChildCount () in which it is very practical.

When a sufficient number of data items, one of the View that we need to take out as an argument onItemClick () method, can be defined by the style set by the code onItemClick () to implement the "Auto Select" the function.

 

2, the second solution

 

Callback.

 

The core idea is consistent with the previous callback delay, we are actively calling onItemClick in the case of conditions are met () to achieve. But this approach compared to the callback delay to much more precise, to delay a sense of visual lag can cause, but also the risk of failure, when the end of the delay data item has not finished loading, then we the purpose of reach. These two issues on the callback mode is not a problem.

 

Since the callback delay to excellent compared to so many, then it is natural to implement the above complex, but it is only somewhat.

 

First, we want the callback is selected by default in the future we need an option when initialization is complete. So you can usually defined in a custom Adapter in such a callback interface and provides a set listening mode, such as:

public class MyAdapter extends ArrayAdapter<String> {

    private boolean isNotified;
    private Callback callback;

    public FunSettingArrayAdapter(Context context, int resource, String[] j, Callback callback) {
        super(context, resource, j);
        this.callback = callback;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v =  ...

        if(!isNotified && position == 0){
            = to true isNotified; 
            IF (the callback = null!) { 
                callback.onLoaded (V); 
            } 
        }

         return V; 
    } 

    interface the Callback { 
        / ** 
         * ListView mainly used to set the function of automatically selected. 
         * * / 
        Void the onLoaded (View View); 
    } 
}

The above code is marked red key part of the enlarged portion. I demand here is to select the default item 1, therefore, the position is 0 callback is triggered.

 

The callback delay implementation approach and practice very much the same:

MyAdapter adapter = new MyAdapter(inflater.getContext(), R.layout.layout,
        ResourcesManager.getResources().getStringArray(R.array.args), new MyAdapter.Callback() {
    @Override
    public void onLoaded(final View view) {
        lv.postDelayed(new Runnable() {
            @Override
            public void run() {
                onItemClick(lv, view, 0, 0);
            }
        }, 200);
    }
});

It should be noted, in front of the Custom Adapter, we actually occurred in the callback getView () to return to the View before ListView, that in fact we receive callback notification is received than the corresponding ListView View earlier of . So we'd better do a small delay to wait for data to load ListView items we need completed so far. 200 ms here has been very adequate.

 


 

Guess you like

Origin www.cnblogs.com/chorm590/p/11611944.html