Last active
October 3, 2017 07:07
-
-
Save smyykb/c9afff4a824485924d033747ad78f88a to your computer and use it in GitHub Desktop.
RecylerView - adapter
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
Recyclerview Listview ve gridview'in yerine kullanıbilecek View Holder pattern'i kullanan yeni bir komponent. | |
Bir View'e ihtiyaç olduğunda layoutu inflate eden yeni bir ViewHolder objesi oluşur ve referansı saklanır liste her scroll olduğunda | |
bu objeyi kullanır. Bu nedenle adı recyclerView'miş :) | |
Diğer bir ozelliği LayoutManager, listedeki itemların dizilişlerini belirliyor. | |
1.LinearLayoutManager, klasik ListView | |
2.GridLayoutManager, GridView | |
3.StaggerGridLayoutManager kademeli,sıralı listview | |
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"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="#FFFFFF" | |
android:orientation="vertical" | |
android:padding="16dp"> | |
<TextView | |
android:id="@+id/title" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
tools:text="Title" /> | |
<TextView | |
android:id="@+id/content" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
tools:text="Content" /> | |
</LinearLayout> |
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"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recyclerView" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:scrollbars="vertical" /> | |
</LinearLayout> |
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
public class Message { | |
private String title; | |
private String content; | |
public Message(String title, String content) { | |
this.title = title; | |
this.content = content; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public String getContent() { | |
return content; | |
} | |
public void setContent(String content) { | |
this.content = 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
public class MessageListActivity extends AppCompatActivity { | |
@BindView(R.id.recyclerView) | |
RecyclerView recyclerView; | |
private List<Message> messageList; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.act_message_list); | |
ButterKnife.bind(this); | |
generateSomeDummyMessageList(); | |
setMessageList(); | |
} | |
private void generateSomeDummyMessageList() { | |
messageList = new ArrayList<>(); | |
messageList.add(new Message("Ada", "Lovelace")); | |
messageList.add(new Message("James", "Gosling")); | |
messageList.add(new Message("Dennis", "Ritchie")); | |
messageList.add(new Message("Guido", "van Rossum")); | |
messageList.add(new Message("Rasmus", " Lerdorf")); | |
messageList.add(new Message("Larry", "Wall")); | |
messageList.add(new Message("Brendan", " Eich")); | |
messageList.add(new Message("Yukihiro", "Matsumoto")); | |
messageList.add(new Message("John", "McCarthy")); | |
messageList.add(new Message("Niklaus", " Wirth")); | |
messageList.add(new Message("Bjarne", "Stroustrup")); | |
} | |
private void setMessageList() { | |
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); | |
recyclerView.setLayoutManager(layoutManager); | |
//LinearLayout with orientation | |
RecyclerView.LayoutManager layoutManager1 = new LinearLayoutManager(this, | |
LinearLayoutManager.HORIZONTAL, | |
false); | |
//GridLayout | |
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, | |
2,/*Number of grid columns*/ | |
GridLayoutManager.VERTICAL, | |
false); | |
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { | |
@Override | |
public int getSpanSize(int position) { | |
//Stagger every other row | |
return position % 3 == 0 ? 2 : 1; | |
} | |
}); | |
//StaggeredGridLayout | |
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager( | |
2,/*Number of grid columns*/ | |
StaggeredGridLayoutManager.VERTICAL); | |
//Divider | |
DividerItemDecoration mDividerItemDecoration = new DividerItemDecoration( | |
this, | |
DividerItemDecoration.VERTICAL); | |
mDividerItemDecoration.setDrawable(ContextCompat.getDrawable(this, R.drawable.list_divider_with_space)); | |
customerListView.addItemDecoration(mDividerItemDecoration); | |
MessageListAdapter adapter = new MessageListAdapter(messageList); | |
recyclerView.setAdapter(adapter); | |
} | |
} |
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
public abstract class BaseRecyclerViewAdapter<E extends BaseRecyclerViewHolder> extends RecyclerView.Adapter<E> { | |
} |
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
public class MessageListAdapter extends BaseRecyclerViewAdapter<MessageListHolder> { | |
private List<Message> messageList; | |
public MessageListAdapter(List<Message> messageList) { | |
this.messageList = messageList; | |
} | |
@Override | |
public MessageListHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
return new MessageListHolder(parent, R.layout.row_message_list); | |
} | |
@Override | |
public void onBindViewHolder(MessageListHolder holder, int position) { | |
holder.bindView(messageList.get(position)); | |
} | |
@Override | |
public int getItemCount() { | |
return messageList.size(); | |
} | |
} |
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
public abstract class BaseRecyclerViewHolder extends RecyclerView.ViewHolder { | |
protected BaseRecyclerViewHolder(ViewGroup parentView, @LayoutRes int layoutResId) { | |
this(LayoutInflater.from(parentView.getContext()).inflate(layoutResId, parentView, false)); | |
} | |
private BaseRecyclerViewHolder(View itemView) { | |
super(itemView); | |
ButterKnife.bind(this, itemView); | |
} | |
} |
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
public class MessageListHolder extends BaseRecyclerViewHolder { | |
@BindView(R.id.title) | |
TextView titleTextView; | |
@BindView(R.id.content) | |
TextView contentTextView; | |
protected MessageListHolder(ViewGroup parentView, @LayoutRes int layoutResId) { | |
super(parentView, layoutResId); | |
} | |
public void bindView(Message message){ | |
titleTextView.setText(message.getTitle()); | |
contentTextView.setText(message.getContent()); | |
} | |
} |
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"?> | |
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item android:height="1dp"> | |
<shape> | |
<solid android:color="#d9dcdf" /> | |
</shape> | |
</item> | |
<item | |
android:height="8dp" | |
android:top="1dp"> | |
<shape> | |
<solid android:color="@android:color/transparent" /> | |
</shape> | |
</item> | |
</layer-list> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment