Skip to content

Commit

Permalink
Style updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Jawnnypoo committed Apr 27, 2017
1 parent 1015739 commit 8df52b8
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.widget.Adapter;

import com.commit451.adapterlayout.AdapterLayoutDelegate;
import com.wefika.flowlayout.FlowLayout;

/**
* {@link com.wefika.flowlayout.FlowLayout} with {@link Adapter} support.
* {@link com.wefika.flowlayout.FlowLayout} with {@link android.support.v7.widget.RecyclerView.Adapter} support.
*/
public class AdapterFlowLayout extends FlowLayout {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,31 +155,30 @@ private void recreateViews() {
}
int i;
for (i = 0; i < mAdapter.getItemCount(); i++) {

int viewType = mAdapter.getItemViewType(i);
//This means the view could already exist
if (i < mViewGroup.getChildCount()) {
View child = mViewGroup.getChildAt(i);
Integer savedViewType = (Integer) child.getTag(R.id.adapter_layout_list_view_type);
RecyclerView.ViewHolder savedViewHolder = (RecyclerView.ViewHolder) child.getTag(R.id.adapter_layout_list_holder);

if (savedViewType != null && savedViewType == viewType && savedViewHolder != null) {
//perfect, it exists and is the right type, so just bind it
mAdapter.onBindViewHolder(savedViewHolder, i);
int viewType = mAdapter.getItemViewType(i);
//This means the view could already exist
if (i < mViewGroup.getChildCount()) {
View child = mViewGroup.getChildAt(i);
Integer savedViewType = (Integer) child.getTag(R.id.adapter_layout_list_view_type);
RecyclerView.ViewHolder savedViewHolder = (RecyclerView.ViewHolder) child.getTag(R.id.adapter_layout_list_holder);

if (savedViewType != null && savedViewType == viewType && savedViewHolder != null) {
//perfect, it exists and is the right type, so just bind it
mAdapter.onBindViewHolder(savedViewHolder, i);
} else {
//it already existed, but something was wrong. So remove it and recreate it
addViewAt(viewType, i);
mViewGroup.removeView(child);
}
} else {
//it already existed, but something was wrong. So remove it and recreate it
addViewAt(viewType, i);
mViewGroup.removeView(child);
//Creating a brand new view
addViewAt(viewType, i);
}
} else {
//Creating a brand new view
addViewAt(viewType, i);
}
}

//Outside the bounds of the dataset, so remove it
if (i < mViewGroup.getChildCount()) {
mViewGroup.removeViews(i, mViewGroup.getChildCount() - i);
mViewGroup.removeViews(i, mViewGroup.getChildCount() - i);
}
}
}
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ dependencies {
testCompile 'junit:junit:4.12'
compile "com.android.support:appcompat-v7:$supportLibVersion"
compile "com.android.support:design:$supportLibVersion"
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
compile project(':adapterlayout')
compile project(':adapterflowlayout')
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</intent-filter>
</activity>

<activity android:name=".CustomAdapterLayoutActivity"/>
<activity android:name=".AdapterFlowLayoutActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CustomAdapterLayoutActivity extends AppCompatActivity {
public class AdapterFlowLayoutActivity extends AppCompatActivity {

@Bind(R.id.toolbar)
@BindView(R.id.toolbar)
Toolbar toolbar;
@Bind(R.id.adapter_layout)
@BindView(R.id.adapter_layout)
AdapterFlowLayout adapterFlowLayout;

CheeseAdapter adapter;

private CheeseAdapter.Listener listener = new CheeseAdapter.Listener() {
@Override
public void onItemClicked(Cheese cheese) {
Toast.makeText(CustomAdapterLayoutActivity.this, cheese.getName() + " clicked", Toast.LENGTH_SHORT)
Toast.makeText(AdapterFlowLayoutActivity.this, cheese.name + " clicked", Toast.LENGTH_SHORT)
.show();
}
};
Expand Down
16 changes: 2 additions & 14 deletions app/src/main/java/com/commit451/adapterlayout/sample/Cheese.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,11 @@
*/
public class Cheese {

int drawable;
String name;
public int drawable;
public String name;

public Cheese(int drawable, String name) {
this.drawable = drawable;
this.name = name;
}

public int getDrawable() {
return drawable;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public void removeLast() {
public void changeMiddle() {
if (!values.isEmpty()) {
int index = values.size()/2;
values.get(index).setName("Swiss");
values.get(index).name = "Swiss";
notifyItemChanged(index);
}
}

public void changeAll() {
if (!values.isEmpty()) {
for (Cheese cheese : values) {
cheese.setName("Swiss");
cheese.name = "Swiss";
}
notifyItemRangeChanged(0, values.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public CheeseViewHolder(View view) {
}

public void bind(Cheese cheese) {
title.setText(cheese.getName());
title.setText(cheese.name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@

import com.commit451.adapterlayout.AdapterLinearLayout;

import butterknife.Bind;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

@Bind(R.id.root)
@BindView(R.id.root)
ViewGroup root;
@Bind(R.id.toolbar)
@BindView(R.id.toolbar)
Toolbar toolbar;
@Bind(R.id.adapter_layout)
@BindView(R.id.adapter_layout)
AdapterLinearLayout adapterLinearLayout;
@Bind(R.id.animate_layout_changes)
@BindView(R.id.animate_layout_changes)
CheckBox checkBoxAnimateLayoutChanges;

CheeseAdapter adapter;

private CheeseAdapter.Listener mListener = new CheeseAdapter.Listener() {
private CheeseAdapter.Listener listener = new CheeseAdapter.Listener() {
@Override
public void onItemClicked(Cheese cheese) {
Snackbar.make(root, cheese.getName() + " clicked", Snackbar.LENGTH_SHORT)
Snackbar.make(root, cheese.name + " clicked", Snackbar.LENGTH_SHORT)
.show();
}
};
Expand Down Expand Up @@ -63,9 +63,16 @@ void onChangeAll() {
adapter.changeAll();
}

@OnClick(R.id.set_to_5)
void onSetTo5Clicked() {
adapter.setData(Cheeses.getRandomCheeses(5));
}

@OnClick(R.id.new_adapter)
void onNewAdapterClicked() {
adapter.setData(Cheeses.getRandomCheeses(5));
adapterLinearLayout.setAdapter(null);
adapter = new CheeseAdapter(listener);
adapterLinearLayout.setAdapter(adapter);
}

@Override
Expand All @@ -79,19 +86,14 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_custom_adapter_layout:
startActivity(new Intent(MainActivity.this, CustomAdapterLayoutActivity.class));
case R.id.action_adapter_flow_layout:
startActivity(new Intent(MainActivity.this, AdapterFlowLayoutActivity.class));
return true;
}
return false;
}
});
adapter = new CheeseAdapter(new CheeseAdapter.Listener() {
@Override
public void onItemClicked(Cheese cheese) {

}
});
adapter = new CheeseAdapter(listener);
adapterLinearLayout.setAdapter(adapter);

checkBoxAnimateLayoutChanges.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
Expand All @@ -106,7 +108,5 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
checkBoxAnimateLayoutChanges.setChecked(true);

adapter.setData(Cheeses.getRandomCheeses(10));
}
}
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@
android:layout_height="wrap_content"
android:text="Change All" />

<Button
android:id="@+id/set_to_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set to 5" />

<Button
android:id="@+id/new_adapter"
android:layout_width="wrap_content"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/action_custom_adapter_layout"
android:id="@+id/action_adapter_flow_layout"
app:showAsAction="never"
android:title="Custom AdapterLayout" />
android:title="AdapterFlowLayout" />

</menu>

0 comments on commit 8df52b8

Please sign in to comment.