RecyclerView getChildAt(position) returns null
<ul>
<li>I implement Recyclerview in my app and I need on its item click update another listview that's why I've implemented onClick event in activity. It works fine.</li>
<li>But I also needed to High Light RecyclerView selected view and other are remain normal.</li>
<li>For that I used RecyclerView getChild method for making all view normal and then selected view Highlight.</li>
<li>I realize that it works for only Visible views of RecyclerView.
But I needed to make normal all views of Recyclerview and if i used getChildAt with larger position then total visible count it returns Null Pointer exception.

<ul>
<li>Log that I get</li>
</ul></li>
</ul>

<blockquote>
  <p>E/Item Pos: 29</p>
  
  <p>E/Item Rv: 15</p>
  
  <p>E/Child Adapter: 30</p>
</blockquote>

<h1>MainActivity.java</h1>

<pre><code>public class MainActivity extends Activity {
RecyclerView reView;
ArrayList&lt;RvModel&gt; rvModels;
revAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    reView = (RecyclerView) findViewById(R.id.reView);
    reView.setLayoutManager(new GridLayoutManager(MainActivity.this, 3));

    rvModels = new ArrayList&lt;&gt;();
    adapter = new revAdapter(MainActivity.this, rvModels);
    reView.setAdapter(adapter);
    prepareData();
    reView.addOnItemTouchListener(new RecyclerItemClickListener(MainActivity.this, reView, new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Log.e("Item Pos", "" + position);
            Log.e("Item Rv", "" + reView.getChildCount());
            Log.e("Child Adapter", "" + reView.getAdapter().getItemCount());

            for (int i = 0; i &lt; reView.getChildCount(); i++) {
                View v = reView.getChildAt(i);
                TextView txtText = (TextView) v.findViewById(R.id.txtText);

                txtText.setBackgroundColor(Color.WHITE);
                txtText.setTextColor(Color.BLUE);
            }
            TextView txtText = (TextView) view.findViewById(R.id.txtText);
            txtText.setBackgroundColor(Color.BLUE);
            txtText.setTextColor(Color.WHITE);
        }

        @Override
        public void onLongItemClick(View view, int position) {

        }
    }));
}

private void prepareData() {
    rvModels.clear();
    for (int i = 0; i &lt; 30; i++) {
        RvModel rvModel = new RvModel();
        rvModel.setTitle("Item : " + i);
        rvModels.add(rvModel);
    }
    adapter.notifyDataSetChanged();
  }
}
</code></pre>

<h1>revAdapter.java</h1>

<pre><code>public class revAdapter extends RecyclerView.Adapter&lt;revAdapter.ViewHolder&gt; {
Context context;
ArrayList&lt;RvModel&gt; rvModels;
LayoutInflater inflater;

public revAdapter(Context context, ArrayList&lt;RvModel&gt; rvModels) {
    this.context = context;
    this.rvModels = rvModels;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public class ViewHolder extends RecyclerView.ViewHolder {
    ImageView ivIcon;
    TextView txtText;

    public ViewHolder(View itemView) {
        super(itemView);
        ivIcon = (ImageView) itemView.findViewById(R.id.ivIcon);
        txtText = (TextView) itemView.findViewById(R.id.txtText);
    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = inflater.inflate(R.layout.rv_raw, parent, false);
    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    RvModel rvModel = rvModels.get(position);
    holder.txtText.setText(rvModel.getTitle());
}

@Override
public int getItemCount() {
    return rvModels.size();
}

@Override
public int getItemViewType(int position) {
    return position;
  }
}
</code></pre>

<h1>rv_raw.xml</h1>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"&gt;

&lt;LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp"&gt;

    &lt;ImageView
        android:id="@+id/ivIcon"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher" /&gt;

    &lt;TextView
        android:id="@+id/txtText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" /&gt;
&lt;/LinearLayout&gt;
</code></pre>

<p></p>