提交 132251fe 编写于 作者: T Takeshi Hagikura 提交者: Takeshi Hagikura

Implement a dialog to modify the flex item atrributes when an view holder is clicked (#165)

上级 5f20868b
......@@ -16,8 +16,6 @@
package com.google.android.flexbox;
import com.google.android.flexbox.FlexItem;
/**
* A listener that listens to the change of a flex item
*/
......
......@@ -17,6 +17,7 @@
package com.google.android.flexbox;
import android.view.View;
import android.view.ViewGroup;
/**
* Default implementation for the {@link FlexItemChangedListener}.
......@@ -32,6 +33,6 @@ public class FlexItemChangedListenerImpl implements FlexItemChangedListener {
@Override
public void onFlexItemChanged(FlexItem flexItem, int viewIndex) {
View view = mFlexContainer.getFlexItemAt(viewIndex);
view.setLayoutParams((FlexboxLayout.LayoutParams) flexItem);
view.setLayoutParams((ViewGroup.LayoutParams) flexItem);
}
}
......@@ -52,13 +52,13 @@ public class RecyclerViewFragment extends Fragment {
RecyclerView recyclerView = (RecyclerView) view.findViewById(
R.id.recyclerview);
final FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager();
final MainActivity activity = (MainActivity) getActivity();
recyclerView.setLayoutManager(flexboxLayoutManager);
final FlexItemAdapter adapter = new FlexItemAdapter();
final FlexItemAdapter adapter = new FlexItemAdapter(activity, flexboxLayoutManager);
recyclerView.setAdapter(adapter);
final MainActivity activity = (MainActivity) getActivity();
final FragmentHelper fragmentHelper = new FragmentHelper(activity, flexboxLayoutManager);
fragmentHelper.initializeViews();
FloatingActionButton addFab = (FloatingActionButton) activity.findViewById(
R.id.add_fab);
if (addFab != null) {
......@@ -70,7 +70,6 @@ public class RecyclerViewFragment extends Fragment {
ViewGroup.LayoutParams.WRAP_CONTENT);
fragmentHelper.setFlexItemAttributes(lp);
adapter.addItem(lp);
// TODO: Specify index?
adapter.notifyDataSetChanged();
}
});
......
......@@ -17,9 +17,12 @@
package com.google.android.flexbox.recyclerview;
import com.google.android.apps.flexbox.R;
import com.google.android.flexbox.FlexItemChangedListenerImpl;
import com.google.android.flexbox.FlexItemClickListener;
import com.google.android.flexbox.FlexboxLayoutManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
......@@ -32,14 +35,16 @@ import java.util.List;
*/
public class FlexItemAdapter extends RecyclerView.Adapter<FlexItemViewHolder> {
private List<RecyclerView.LayoutParams> mLayoutParams;
private AppCompatActivity mActivity;
public FlexItemAdapter() {
this(new ArrayList<RecyclerView.LayoutParams>());
}
private FlexboxLayoutManager mLayoutManager;
private List<RecyclerView.LayoutParams> mLayoutParams;
public FlexItemAdapter(List<RecyclerView.LayoutParams> flexItems) {
mLayoutParams = flexItems;
public FlexItemAdapter(AppCompatActivity activity, FlexboxLayoutManager layoutManager) {
mActivity = activity;
mLayoutManager = layoutManager;
mLayoutParams = new ArrayList<>();
}
@Override
......@@ -51,10 +56,10 @@ public class FlexItemAdapter extends RecyclerView.Adapter<FlexItemViewHolder> {
@Override
public void onBindViewHolder(FlexItemViewHolder holder, int position) {
holder.mTextView.setText(String.valueOf(position + 1));
holder.mTextView.setBackgroundResource(R.drawable.flex_item_background);
holder.mTextView.setGravity(Gravity.CENTER);
holder.mTextView.setLayoutParams(mLayoutParams.get(position));
int adapterPosition = holder.getAdapterPosition();
holder.itemView.setOnClickListener(new FlexItemClickListener(mActivity,
new FlexItemChangedListenerImpl(mLayoutManager), adapterPosition));
holder.bindTo(mLayoutParams.get(position));
}
public void addItem(RecyclerView.LayoutParams lp) {
......
......@@ -19,19 +19,27 @@ package com.google.android.flexbox.recyclerview;
import com.google.android.apps.flexbox.R;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
/**
* ViewHolder implementation for a flex item.
*/
public class FlexItemViewHolder extends RecyclerView.ViewHolder {
class FlexItemViewHolder extends RecyclerView.ViewHolder {
TextView mTextView;
private TextView mTextView;
public FlexItemViewHolder(View itemView) {
FlexItemViewHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.textview);
}
void bindTo(RecyclerView.LayoutParams layoutParams) {
int adapterPosition = getAdapterPosition();
mTextView.setText(String.valueOf(adapterPosition + 1));
mTextView.setBackgroundResource(R.drawable.flex_item_background);
mTextView.setGravity(Gravity.CENTER);
mTextView.setLayoutParams(layoutParams);
}
}
......@@ -461,10 +461,10 @@ public class FlexboxLayoutManager extends RecyclerView.LayoutManager implements
mAnchorInfo.reset();
updateAnchorInfoForLayout(state, mAnchorInfo);
// Unlike the FlexboxLayout, the order attribute is not supported, we don't calculated the
// Unlike the FlexboxLayout, the order attribute is not supported, we don't calculate the
// order attribute because preparing the order attribute requires all
// view holders to be inflated at least once, which is inefficient if the number of items
// is large
// in the adapter is large
resolveLayoutDirection();
updateLayoutStateToFillEnd(mAnchorInfo);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册