如何修复后退按钮失真的片段?

时间:2018-05-23 11:19:17

标签: android android-fragments onbackpressed

我有一个包含此布局的片段: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 :(得分:0)

解决了在Cardview中删除layout_columnWeight和layout_rowWeight属性以及设置layout_width和layout_height的问题。