家庭活动与导航项目上的片段重叠单击

时间:2019-05-26 16:21:25

标签: android android-fragments

enter image description here

单击导航抽屉项时,我的个人资料片段与首页(地图)片段重叠。

我的个人资料片段在顶部具有线性布局,并具有recyclerview。单击配置文件导航项时,它仅在顶部显示线性布局,在底部显示地图。

  

fragment_profile.xml

<RelativeLayout 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"
    tools:context=".ProfileFragment">

    <LinearLayout
        android:id="@+id/profile_header"
        android:layout_width="match_parent"
        android:layout_height="130dp"
        android:orientation="horizontal"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:gravity="top"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="?attr/colorPrimaryDark">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/circleView"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="40dp"
            android:paddingEnd="20dp"
            android:paddingStart="20dp"
            android:layout_weight="1"
            android:src="@drawable/ic_person_black"
            android:background="@drawable/circular_image_id_background"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="3"
            android:paddingStart="15dp"
            android:paddingEnd="15dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="40dp"
            android:paddingTop="10dp"
            android:background="@null">

            <TextView
                android:id="@+id/profile_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingTop="8dp"
                android:paddingEnd="10dp"
                android:paddingStart="10dp"
                android:background="@null"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textColor="#FFFFFF"
                android:textSize="22sp"/>

            <TextView
                android:id="@+id/country"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingTop="8dp"
                android:paddingEnd="10dp"
                android:paddingStart="10dp"
                android:background="@null"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textColor="#FFFFFF"
                android:textSize="22sp"/>

        </LinearLayout>

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/profile_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/profile_header"
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true"
        android:padding="20dp"
        android:orientation="vertical"
        android:scrollbars="none">

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

</RelativeLayout> 

activity_home.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_home"
        app:menu="@menu/activity_home_drawer" />
</android.support.v4.widget.DrawerLayout>

ProfileFragment.java 

    public class ProfileFragment extends Fragment {

    private static final String TAG = ProfileFragment.class.getSimpleName();
    private ImageView profilephoto;
    private TextView profileName;
    private TextView country;
    private TextView userStatus;
    private RecyclerViewAdapter mRecyclerViewAdapter;
    private RecyclerView recyclerView;
    private List<UserProfile> userProfile;
    private String id;
    private static final int REQUEST_READ_PERMISSION = 120;

    public ProfileFragment() {
    }

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

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
            ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container,
                false);

        Objects.requireNonNull(getActivity()).setTitle("My Profile");

        profileName = view.findViewById(R.id.profile_name);
        country = view.findViewById(R.id.country);
        profileName.setVisibility(View.GONE);
        country.setVisibility(View.GONE);

        profilephoto = view.findViewById(R.id.circleView);
        profilephoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Intent galleryIntent = new Intent(Intent.
                         ACTION_GET_CONTENT);
                galleryIntent.setType("image/*");
                startActivityForResult(galleryIntent, Helper.SELECT_PICTURE);
            }
        });

        recyclerView = view.findViewById(R.id.profile_list);
        LinearLayoutManager linearLayoutManager = new 
                 LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(new 
                SimpleDividerItemDecoration(getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());      

        userProfile = new ArrayList<>();
        mRecyclerViewAdapter =new RecyclerViewAdapter(getActivity(), 
                  userProfile);
        recyclerView.setAdapter(mRecyclerViewAdapter);

        ((FirebaseApplication)getActivity().getApplication()).getFirebaseAuth();
        id = ((FirebaseApplication)getActivity()
                .getApplication()).getFirebaseUserAuthenticatedId();

        FirebaseDatabaseHelper firebaseDatabaseHelper = new 
                                   FirebaseDatabaseHelper();
        firebaseDatabaseHelper.isUserKeyExist(id, getActivity(), recyclerView);
        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        System.out.println("user id has entered onActivityResult ");
        if (requestCode == Helper.SELECT_PICTURE) {
            if (data != null) {
                Uri selectedImageUri = data.getData();
                String imagePath = getPath(selectedImageUri);
                FirebaseStorageHelper storageHelper = new 
                          FirebaseStorageHelper(getActivity());

                if (ActivityCompat
                    .checkSelfPermission(Objects.requireNonNull(getActivity()), 
                     Manifest.permission.READ_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(getActivity(), new 
                           String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                               REQUEST_READ_PERMISSION);
                    return;
                }
                storageHelper.saveProfileImageToCloud(id, selectedImageUri, 
                        profilephoto);
            }
        }
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = Objects.requireNonNull(getActivity())
                               .getContentResolver()
                               .query(uri, projection, null, null, null);
        assert cursor != null;
        int column_index = cursor
                           .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(projection[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();
        return filePath;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] 
    permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_READ_PERMISSION) {
            if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
                Toast.makeText(getActivity(), "Sorry! You can't use this app 
                without granting this permission", Toast.LENGTH_LONG).show();
            }
        }
    }
}

Home.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        Objects.requireNonNull(mapFragment).getMapAsync(this);
        final DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, 
                R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        navigationView.setNavigationItemSelectedListener(this);

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_profile) {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.main_container_wrapper, new ProfileFragment());
            fragmentTransaction.commit();
        } else if (id == R.id.nav_settings) {

        } else if (id == R.id.nav_help) {

        } else if (id == R.id.nav_logout) {
            signOut();
        }

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

0 个答案:

没有答案