Created
January 21, 2017 14:14
-
-
Save travisdachi/0e9bf790eeda61da275f5d40f47af8a0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class MainActivity extends AppCompatActivity { | |
StringAdapter adapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
adapter = new StringAdapter( | |
Arrays.asList("Harry Potter", "Ron Weasley"), | |
new Function4<View, Integer, StringHolder, String, Unit>() { | |
@Override | |
public Unit invoke(View view, Integer integer, StringHolder stringHolder, String s) { | |
if (view == stringHolder.text1) { | |
Toast.makeText(MainActivity.this, "text1", Toast.LENGTH_SHORT).show(); | |
} else if (view == stringHolder.text2) { | |
Toast.makeText(MainActivity.this, "text4", Toast.LENGTH_SHORT).show(); | |
} else { | |
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show(); | |
} | |
return null; | |
} | |
}, | |
new Function2<List<? extends String>, List<? extends String>, DiffUtil.DiffResult>() { | |
@Override | |
public DiffUtil.DiffResult invoke(final List<? extends String> strings, final List<? extends String> strings2) { | |
return DiffUtil.calculateDiff(new DiffUtil.Callback() { | |
@Override | |
public int getOldListSize() { | |
return strings.size(); | |
} | |
@Override | |
public int getNewListSize() { | |
return strings2.size(); | |
} | |
@Override | |
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) { | |
return strings.get(oldItemPosition).equals(strings2.get(newItemPosition)); | |
} | |
@Override | |
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) { | |
return areItemsTheSame(oldItemPosition, newItemPosition); | |
} | |
}); | |
} | |
} | |
); | |
RecyclerView rv = (RecyclerView) findViewById(R.id.rv); | |
rv.setLayoutManager(new LinearLayoutManager(this)); | |
rv.setAdapter(adapter); | |
new Handler().postDelayed( | |
new Runnable() { | |
@Override | |
public void run() { | |
List<String> strings = Arrays.asList("Harry Potter", "Ron Weasley", "Hermione Granger", "Rubeus Hagrid"); | |
Collections.sort(strings); | |
adapter.setList(strings); | |
} | |
}, | |
5000L); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment