Search code examples
javaandroidandroid-arrayadapterandroid-gridview

How to update invisible/off-screen views?


I've made a 15x15 GridView that contains 225 TextViews, numbered 0 to 224. I have access (via getChildAt(int position)) to all of the rows that fit on the screen at once, which is 180 TextViews or the first 12 rows. What I want to do is invoke a method (called playSoE()) that changes the background of all the TextViews to a different color. However changing the last 3 rows is a problem since they're off-screen. I've been looking up a way to get past this but all I'm getting is "it's impossible". If that's the case, then at least I want to the hidden TextViews to update once they are partially on the screen so the user won't notice the difference. Does anyone know how I would implement either of these?

SoE

As you can see, there's 120 TextViews that are complete/partially on screen while 60 on the right of the screen and 45 below the screen. For some reason, nesting the GridView into a HorizontalScrollView was all that was needed to access the TextViews on the right. I tried doing the same for ScrollView and the TextViews on the bottom, but nothing I tried has worked.

MainActivity.java:

public class MainActivity extends ActionBarActivity {

    private boolean mIsPlaying;
    private int primesLE;
    private GridView mGridView;
    private ArrayAdapter<TextView> mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mIsPlaying = false;

        ViewGroup.LayoutParams layoutParams;

        primesLE = 225;
        int numOfColumns = (int)Math.round(Math.sqrt((double) primesLE));
        int numOfRows = (int)Math.ceil((double)primesLE/(double)numOfColumns);

        View v1 = findViewById(R.id.title_horizontalScrollView);
        View v2 = v1.findViewById(R.id.relativelayout);
        mGridView = (GridView) v2.findViewById(R.id.mGridView);
        layoutParams = mGridView.getLayoutParams();
        layoutParams.width = 150*numOfColumns; //this is in pixels
        mGridView.setLayoutParams(layoutParams);
        mGridView.setNumColumns(numOfColumns);
        mAdapter = new ImageAdapter(this, android.R.layout.simple_list_item_1, primesLE);
        mGridView.setAdapter(mAdapter);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    private void playSoE(){

        Log.d("mAdapter.getCount()","" + mAdapter.getCount()); //225
        Log.d("mGridView.getCount()",""+mGridView.getCount()); //225
        Log.d("mGridView.getChildCount()",""+mGridView.getChildCount()); //180


        for(int i = 0; i < 180;i++){ //NPE from 180 or higher
            Log.d("Getting view number",""+i);
            mGridView.getChildAt(i).setBackgroundColor(Color.BLUE);
        }


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        Log.d("Menu","Button Pressed");
        if (id == R.id.action_settings) {
            return true;
        }

        else if (id == R.id.action_status){
            if(mIsPlaying) {
                mIsPlaying = false;
                item.setIcon(R.drawable.ic_action_play);
                item.setTitle("Play");
                playSoE();
            }
            else {
                mIsPlaying = true;
                item.setIcon(R.drawable.ic_action_pause);
                item.setTitle("Pause");
            }
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

ImageAdapter.java:

public class ImageAdapter extends ArrayAdapter {
    private Context mContext;
    private int mCount;

    public ImageAdapter(Context c, int resource, int count) {
        super(c, resource);
        mContext = c;
        mCount = count;
    }


    public int getCount() {
        return mCount;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView;

        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            textView = new TextView(mContext);
            textView.setGravity(Gravity.CENTER);
            textView.setLayoutParams(new GridView.LayoutParams(100, 100));

        } else {
            textView = (TextView) convertView;
        }

        textView.setBackgroundColor(Color.RED);
        textView.setText("" + position);
        return textView;
    }

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/title_horizontalScrollView"
        android:fillViewport="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity"
            android:id="@+id/relativelayout">

            <GridView
                android:id="@+id/mGridView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:columnWidth="90dp"
                android:verticalSpacing="10dp"
                android:horizontalSpacing="10dp"
                android:stretchMode="columnWidth"
                android:gravity="center"
            />

        </RelativeLayout>

    </HorizontalScrollView>

menu_main.xml:

<?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_status"
        android:icon="@drawable/ic_action_play"
        android:title="Play"
        app:showAsAction="always"/>
    <item android:id="@+id/action_stop"
        android:icon="@drawable/ic_action_stop"
        android:title="Stop"
        app:showAsAction="always"/>
    <item android:id="@+id/action_settings"
            android:title="@string/action_settings"
            app:showAsAction="never"  />

    </menu>

Solution

  • You got a very ugly solution there :D you should learn more about adapters and recycled views

    here's what you should do,

    // adapter class
    private boolean setBlueBackground = false;
    
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView;
    
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            textView = new TextView(mContext);
            textView.setGravity(Gravity.CENTER);
            textView.setLayoutParams(new GridView.LayoutParams(100, 100));
    
        } else {
            textView = (TextView) convertView;
        }
    
        if(setBlueBackground)
            textView.setBackgroundColor(Color.BLUE);
        else 
            textView.setBackgroundColor(Color.RED);
    
        textView.setText("" + position);
        return textView;
    }
    
    public void setBlueBg(boolean blue) {
        setBlueBackground = blue;
        notifyDataSetChanged();
    }
    

    now all you have to do to change colors is

    mAdapter.setBlueBg(true); // or false to make it red
    

    it will replace this

    for(int i = 0; i < 180;i++){ //NPE from 180 or higher
        Log.d("Getting view number",""+i);
        mGridView.getChildAt(i).setBackgroundColor(Color.BLUE);
    }
    

    there you go.