Last active
January 27, 2017 08:13
-
-
Save uhfath/368804ce8fe08274e019bcaeab501783 to your computer and use it in GitHub Desktop.
Simple item decoration for RecyclerView footer
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
package com.company.user.recyclerviewdecorationtest; | |
import android.graphics.Rect; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.support.v7.widget.DividerItemDecoration; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import java.util.Arrays; | |
import java.util.Random; | |
public class MainActivity extends AppCompatActivity { | |
public static String REMOVE_METHOD = ""; | |
public static class ViewHolder extends RecyclerView.ViewHolder { | |
private int currentPosition; | |
public ViewHolder(View itemView) { | |
super(itemView); | |
} | |
public int getCurrentPosition() { | |
return currentPosition; | |
} | |
public void setCurrentPosition(int currentPosition) { | |
this.currentPosition = currentPosition; | |
} | |
} | |
public class FooterDecoration extends RecyclerView.ItemDecoration { | |
@Override | |
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { | |
int position_1 = parent.getChildAdapterPosition(view); | |
int count_1 = parent.getLayoutManager().getItemCount(); | |
int position_2 = parent.getChildLayoutPosition(view); | |
int count_2 = state.getItemCount(); | |
ViewHolder viewHolder = (ViewHolder) parent.getChildViewHolder(view); | |
int position_3 = viewHolder.getCurrentPosition(); | |
int count_3 = testList.length; | |
int position = | |
REMOVE_METHOD.equals("REMOVE_1") ? position_1 : | |
REMOVE_METHOD.equals("REMOVE_2") ? position_2 : | |
position_3; | |
int count = | |
REMOVE_METHOD.equals("REMOVE_1") ? count_1 : | |
REMOVE_METHOD.equals("REMOVE_2") ? count_2 : | |
count_3; | |
Log.d("TEST", String.format("UPDATE: POS_1(%d), CNT_1(%d); POS_2(%d), CNT_2(%d); POS_3(%d), CNT_3(%d)", position_1, count_1, position_2, count_2, position_3, count_3)); | |
if (position >= count - 1) { | |
outRect.set(0, 0, 0, 100); | |
} | |
} | |
} | |
private String[] testList; | |
private RecyclerView.Adapter<ViewHolder> modelAdapter; | |
private static final String alphabet = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz"; | |
private static String[] generateTestData() { | |
Random random = new Random(); | |
int count = 10;// + random.nextInt(100); | |
String[] dataList = new String[count]; | |
for (int i = 0; i < count; i++) { | |
int length = 10 + random.nextInt(100); | |
StringBuilder stringBuilder = new StringBuilder(length); | |
stringBuilder.append(i); | |
stringBuilder.append('\n'); | |
for (int j = 0; j < length; j++) { | |
stringBuilder.append(alphabet.charAt(random.nextInt(alphabet.length()))); | |
} | |
dataList[i] = stringBuilder.toString(); | |
} | |
return dataList; | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
testList = generateTestData(); | |
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); | |
recyclerView.setHasFixedSize(true); | |
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); | |
recyclerView.setLayoutManager(layoutManager); | |
DividerItemDecoration itemDecoration = new DividerItemDecoration(this, layoutManager.getOrientation()); | |
recyclerView.addItemDecoration(itemDecoration); | |
FooterDecoration footerDecoration = new FooterDecoration(); | |
recyclerView.addItemDecoration(footerDecoration); | |
modelAdapter = new RecyclerView.Adapter<ViewHolder>() { | |
@Override | |
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View root = getLayoutInflater().inflate(R.layout.list_item, parent, false); | |
return new ViewHolder(root); | |
} | |
@Override | |
public void onBindViewHolder(ViewHolder holder, int position) { | |
TextView textView = (TextView) holder.itemView.findViewById(R.id.text_view); | |
textView.setText(testList[position]); | |
holder.setCurrentPosition(position); | |
} | |
@Override | |
public int getItemCount() { | |
return testList.length; | |
} | |
}; | |
recyclerView.setAdapter(modelAdapter); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
menu.add("NEW").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); | |
menu.add("REMOVE_1").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); | |
menu.add("REMOVE_2").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); | |
menu.add("REMOVE_3").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); | |
return super.onCreateOptionsMenu(menu); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
if (item.getTitle().equals("NEW")) { | |
testList = generateTestData(); | |
modelAdapter.notifyDataSetChanged(); | |
return true; | |
} | |
if (item.getTitle().toString().contains("REMOVE")) { | |
REMOVE_METHOD = item.getTitle().toString(); | |
int removeIndex = testList.length - 1; | |
Log.d("TEST", String.format("REMOVE: POS(%d), CNT(%d)", removeIndex, testList.length)); | |
testList = Arrays.copyOfRange(testList, 0, removeIndex); | |
modelAdapter.notifyItemRemoved(removeIndex); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment