Как исправить искажение фрагмента при нажатой кнопке? - PullRequest
0 голосов
/ 23 мая 2018

У меня есть фрагмент с этим макетом: enter image description here

Если я нажимаю одну кнопку, я получаю список в Recyclerview, все отображается нормально, но при нажатии на спине у меняискажение кнопки: enter image description here

Вот xml первого фрагмента:

    <?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="match_parent"
    android:id="@+id/category_events_layout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Fragments.FragmentCategoryEvents"
    android:orientation="vertical"
    android:weightSum="10">
    <RelativeLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">
     <TextView
         android:id="@+id/textGrid"
         android:text="Scegli una categoria"
         android:textSize="25sp"
         android:layout_centerInParent="true"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
    </RelativeLayout>

    <GridLayout
        android:columnCount="2"
        android:rowCount="2"
        android:alignmentMode="alignMargins"
        android:columnOrderPreserved="false"
        android:layout_weight="8"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:padding="14dp">
        <!-- Riga 1 -->
        <!-- Colonna 1 -->

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        app:cardElevation="8dp"
        app:cardCornerRadius="8dp">
        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_margin="12dp"
            ></LinearLayout>

        <Button
            android:id="@+id/btn_auto"
            android:text="Auto"
            android:textSize="18sp"
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    </android.support.v7.widget.CardView>

        <!--  colonna 2 -->

        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp">
            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_margin="12dp"
                ></LinearLayout>

            <Button
                android:id="@+id/btn_moto"
                android:text="Moto"
                android:textSize="18sp"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />

        </android.support.v7.widget.CardView>

        <!-- Riga 2 -->

        <!-- Colonna 1 -->

        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp">
            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_margin="12dp"
                ></LinearLayout>

            <Button
                android:id="@+id/btn_corsa"
                android:text="Corsa"
                android:textSize="18sp"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />

        </android.support.v7.widget.CardView>

        <!--  colonna 2 -->

        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:cardElevation="8dp"
            app:cardCornerRadius="8dp">
            <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_margin="16dp"
                ></LinearLayout>

            <Button
                android:id="@+id/btn_bicicletta"
                android:text="Bicicletta"
                android:textSize="18sp"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />

        </android.support.v7.widget.CardView>
    </GridLayout>
</LinearLayout>

Это код фрагмента:

public class FragmentCategoryEvents extends Fragment {
    private Button btn_auto;
    private Button btn_moto;
    private Button btn_corsa;
    private Button btn_bicicletta;

    public FragmentCategoryEvents() {
        // Required empty public constructor
    }


    public static FragmentCategoryEvents newInstance() {
        FragmentCategoryEvents fragment = new FragmentCategoryEvents();
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_fragment_category_events, container, false);
        return view;
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState){
        super.onViewCreated(view, savedInstanceState);
        btn_auto = view.findViewById(R.id.btn_auto);
        btn_moto = view.findViewById(R.id.btn_moto);
        btn_corsa = view.findViewById(R.id.btn_corsa);
        btn_bicicletta = view.findViewById(R.id.btn_bicicletta);


        btn_auto.setOnClickListener(v -> getCategoryEvents("Auto"));
        btn_moto.setOnClickListener(v-> getCategoryEvents("Moto"));
        btn_corsa.setOnClickListener(v -> getCategoryEvents("Corsa"));
        btn_bicicletta.setOnClickListener(v -> getCategoryEvents("Bicicletta"));
    }

    private void getCategoryEvents(String category){
        FragmentShowCategory newFragment = new FragmentShowCategory();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        Bundle bundle = new Bundle();
        bundle.putString("category_key",category);
        newFragment.setArguments(bundle);
        transaction.replace(R.id.category_events_layout,newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }



    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

Вкл. Кнопка нажата У меня есть такой макет:

    <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipe_container_category"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/recycler_view_category"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.v4.widget.SwipeRefreshLayout>

И код:

public class FragmentShowCategory extends Fragment {

    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private CompositeSubscription mCompositeSubscription;

    public FragmentShowCategory() {
        // Required empty public constructor
    }

    public static FragmentShowCategory newInstance() {
        FragmentShowCategory fragment = new FragmentShowCategory();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment_show_category, container, false);
    }
    @SuppressLint("ResourceAsColor")
    @Override
    public void onViewCreated (View view, @Nullable Bundle savedInstanceState){
        mRecyclerView = view.findViewById(R.id.recycler_view_category);
        mRecyclerView.setAdapter(new RecyclerViewAdapter(null));
        mRecyclerView.setLayoutManager(null);
        mSwipeRefreshLayout = view.findViewById(R.id.swipe_container_category);
        Bundle bundle = getArguments();
        mCompositeSubscription = new CompositeSubscription();
        categoryEventsProcess(bundle.getString("category_key"));
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mSwipeRefreshLayout.post(new Runnable() {
                    @Override
                    public void run() {
                        mSwipeRefreshLayout.setRefreshing(true);
                        categoryEventsProcess(bundle.getString("category_key"));
                    }
                });
            }
        });

    }

    public void onBackPressed()
    {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        fm.popBackStack();
    }

    private void categoryEventsProcess(String category){
        mCompositeSubscription.add(NetworkUtil.getRetrofit().getEventsByCategory(category)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this::handleResponse,this::handleError));
    }

    private void handleResponse(ArrayList<Evento> eventos) {
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(eventos);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(adapter);
        mSwipeRefreshLayout.setRefreshing(false);

    }

    private void handleError(Throwable throwable) {

    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

Как я могу решить эту проблему?

1 Ответ

0 голосов
/ 23 мая 2018

Решено удаление атрибутов layout_columnWeight и layout_rowWeight и настройка параметров layout_width и layout_height в Cardview.

...