/*
* Copyright (C) 2014 Lucas Rocha
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.marshalchen.common.demoofui.twowayview;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import static android.support.v7.widget.RecyclerView.SCROLL_STATE_IDLE;
import static android.support.v7.widget.RecyclerView.SCROLL_STATE_DRAGGING;
import static android.support.v7.widget.RecyclerView.SCROLL_STATE_SETTLING;
import com.marshalchen.common.demoofui.R;
import com.marshalchen.common.uimodule.twowayview.widget.TwoWayView;
import com.marshalchen.common.uimodule.twowayview.ItemClickSupport;
import com.marshalchen.common.uimodule.twowayview.ItemClickSupport.OnItemClickListener;
import com.marshalchen.common.uimodule.twowayview.ItemClickSupport.OnItemLongClickListener;
import com.marshalchen.common.uimodule.twowayview.widget.DividerItemDecoration;
public class LayoutFragment extends Fragment {
private static final String ARG_LAYOUT_ID = "layout_id";
private TwoWayView mRecyclerView;
private TextView mPositionText;
private TextView mCountText;
private TextView mStateText;
private Toast mToast;
private int mLayoutId;
public static LayoutFragment newInstance(int layoutId) {
LayoutFragment fragment = new LayoutFragment();
Bundle args = new Bundle();
args.putInt(ARG_LAYOUT_ID, layoutId);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLayoutId = getArguments().getInt(ARG_LAYOUT_ID);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(mLayoutId, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final Activity activity = getActivity();
mToast = Toast.makeText(activity, "", Toast.LENGTH_SHORT);
mToast.setGravity(Gravity.CENTER, 0, 0);
mRecyclerView = (TwoWayView) view.findViewById(R.id.list);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLongClickable(true);
mPositionText = (TextView) view.getRootView().findViewById(R.id.position);
mCountText = (TextView) view.getRootView().findViewById(R.id.count);
mStateText = (TextView) view.getRootView().findViewById(R.id.state);
updateState(SCROLL_STATE_IDLE);
final ItemClickSupport itemClick = ItemClickSupport.addTo(mRecyclerView);
itemClick.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(RecyclerView parent, View child, int position, long id) {
mToast.setText("Item clicked: " + position);
mToast.show();
}
});
itemClick.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(RecyclerView parent, View child, int position, long id) {
mToast.setText("Item long pressed: " + position);
mToast.show();
return true;
}
});
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {
updateState(scrollState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int i, int i2) {
mPositionText.setText("First: " + mRecyclerView.getFirstVisiblePosition());
mCountText.setText("Count: " + mRecyclerView.getChildCount());
}
});
final Drawable divider = getResources().getDrawable(R.drawable.two_way_view_divider);
mRecyclerView.addItemDecoration(new DividerItemDecoration(divider));
mRecyclerView.setAdapter(new LayoutAdapter(activity, mRecyclerView, mLayoutId));
}
private void updateState(int scrollState) {
String stateName = "Undefined";
switch(scrollState) {
case SCROLL_STATE_IDLE:
stateName = "Idle";
break;
case SCROLL_STATE_DRAGGING:
stateName = "Dragging";
break;
case SCROLL_STATE_SETTLING:
stateName = "Flinging";
break;
}
mStateText.setText(stateName);
}
public int getLayoutId() {
return getArguments().getInt(ARG_LAYOUT_ID);
}
}