Created
April 24, 2017 11:42
-
-
Save Palatis/e13ac0f322472576c6b2a7c73d280428 to your computer and use it in GitHub Desktop.
RecyclerView recycle test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v7.widget.RecyclerView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/recycler" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
app:layoutManager="LinearLayoutManager" | |
tools:context="com.fitivision.iot.p2p.ui.TheActivity"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<ImageView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/image" | |
android:padding="12dp" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:src="@mipmap/ic_launcher"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<TextView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/text" | |
android:padding="12dp" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fitivision.iot.p2p.ui; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import com.fitivision.iot.p2p.R; | |
public class Main2Activity extends AppCompatActivity { | |
private static final String TAG = "RVTestActivity"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_test); | |
RecyclerView rv = (RecyclerView) findViewById(R.id.recycler); | |
rv.setAdapter(new TheAdapter()); | |
} | |
public static class TheAdapter extends RecyclerView.Adapter<TheAdapter.ViewHolder> { | |
@Override | |
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
if (viewType == R.layout.item_image) | |
return new ImageViewHolder(LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false)); | |
else | |
return new TextViewHolder(LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false)); | |
} | |
@Override | |
public void onBindViewHolder(ViewHolder holder, int position) { | |
holder.bind(position); | |
} | |
@Override | |
public int getItemCount() { | |
return 100; | |
} | |
@Override | |
public int getItemViewType(int position) { | |
if (position == 3) | |
return R.layout.item_image; | |
else | |
return R.layout.item_text; | |
} | |
@Override | |
public void onViewRecycled(ViewHolder holder) { | |
super.onViewRecycled(holder); | |
holder.recycle(); | |
} | |
public static abstract class ViewHolder extends RecyclerView.ViewHolder { | |
public ViewHolder(View itemView) { | |
super(itemView); | |
} | |
public abstract void bind(int position); | |
public void recycle() { | |
Log.d(TAG, getClass().getSimpleName() + ".recycle(): " + getAdapterPosition()); | |
} | |
} | |
public static class TextViewHolder extends ViewHolder { | |
private final TextView text; | |
public TextViewHolder(View itemView) { | |
super(itemView); | |
text = (TextView) itemView.findViewById(R.id.text); | |
} | |
@Override | |
public void bind(int position) { | |
text.setText("pos = " + position); | |
} | |
} | |
public static class ImageViewHolder extends ViewHolder { | |
private final ImageView image; | |
public ImageViewHolder(View itemView) { | |
super(itemView); | |
image = (ImageView) itemView.findViewById(R.id.image); | |
} | |
@Override | |
public void bind(int position) { | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment