当您单击要将城市分段的项目时,但他无法正常工作

时间:2017-04-07 21:03:28

标签: android-fragments

单击某个项目时,我想查看城市片段,但不起作用。 它适用于导航菜单,但是当您单击不起作用的项目时。

public class CityFragment extends Fragment implements CityAdapter.OnOverViewClick {

    //Views
    private RelativeLayout rl;
    private RecyclerView mRecyclerView;
    private View mLoadingView;

    private String cityString;

    //List
    private CityAdapter multipleItemAdapter;

    private ViewTreeObserver.OnGlobalLayoutListener recyclerListener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rl = (RelativeLayout) inflater.inflate(R.layout.fragment_entekhab_shahr, null);
        setHasOptionsMenu(true);
        mRecyclerView = (RecyclerView) rl.findViewById(R.id.rv_list);


        final StaggeredGridLayoutManager mLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);

        cityString = this.getArguments().getStringArray(MainActivity.FRAGMENT_DATA)[0];

        recyclerListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //Get the view width, and check if it could be valid
                int viewWidth = mRecyclerView.getMeasuredWidth();
                if (viewWidth <= 0) return;

                //Remove the VTO
                mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                //Calculate and update the span
                float cardViewWidth = getResources().getDimension(R.dimen.card_width_overview);
                int newSpanCount = Math.max(1, (int) Math.floor(viewWidth / cardViewWidth));
                mLayoutManager.setSpanCount(newSpanCount);
                mLayoutManager.requestLayout();
            }
        };
        mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(recyclerListener);

        mLoadingView = rl.findViewById(R.id.loading_view);

        //Load items
        loadItems();

        return rl;
    }

    public void loadItems() {
        new CityParser(cityString, getActivity(), new CityParser.CallBack() {
            @Override
            public void categoriesLoaded(ArrayList < NavItem > result, boolean failed) {
                if (failed) {
                    //If it failed; show an error if we're using a local file, or if we are online & using a remote overview.
                    if (!cityString.contains("http") || Helper.isOnlineShowDialog(getActivity()))
                        Toast.makeText(getActivity(), R.string.invalid_configuration, Toast.LENGTH_LONG).show();
                    return;
                }

                //Add all the new posts to the list and notify the adapter
                multipleItemAdapter = new CityAdapter(result, getContext(), CityFragment.this);
                mRecyclerView.setAdapter(multipleItemAdapter);

                loadCompleted();
            }
        }).execute();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(recyclerListener);
    }

    private void loadCompleted() {
        //Hide the loading view if it isn't already hidden
        if (mLoadingView.getVisibility() == View.VISIBLE)
            mLoadingView.setVisibility(View.GONE);
    }

    @Override
    public void onOverViewSelected(NavItem item) {
        HolderActivity.startActivity(getActivity(), item.getFragment(), item.getData());
    }
}

0 个答案:

没有答案