我想在Homefragment.java类的recyclerview中添加水平滚动的卡片视图

时间:2019-11-18 06:20:08

标签: java android android-fragments android-recyclerview android-cardview

这是我的MainActivity.java类

package com.example.bottomnavigationdrawer;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView=findViewById(R.id.bottomNavigationView);
        NavController navController= Navigation.findNavController(this,R.id.nav_host_fragment);
        NavigationUI.setupWithNavController(bottomNavigationView,navController);
    }
}

这是我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <com.google.android.material.bottomnavigation.BottomNavigationView

        android:id="@+id/bottomNavigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:itemTextColor="#00FAF7F7"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是我的HomeFragment.java类:

    package com.example.bottomnavigationdrawer;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.smarteist.autoimageslider.SliderView;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {




    public HomeFragment() {

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {





        View view = inflater.inflate(R.layout.fragment_home, container, false);
        SliderView imageView = (SliderView) view.findViewById(R.id.imageSlider);
        return view;
}}

这是我的fragment_home.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="3dp"
    android:paddingLeft="3dp"
    android:paddingRight="3dp"
    android:paddingTop="3dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".HomeFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:gravity="center"
        android:textSize="70sp"
        android:textColor="#FAF8F8"
        android:background="#25212E"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Home" />
</RelativeLayout>

我是Android的新手,因此我进行了很多搜索。但是我得到的每个教程都是通过主要活动,模型,适配器来完成的。但是我必须在主要活动的家庭片段上完成它。

1 个答案:

答案 0 :(得分:0)

实际上我不明白你的问题。如果您想了解recyclerview和卡片视图以及底部导航视图,请遵循此教程:

这很容易。如果要设置水平滚动,请使用LinearLayoutManager

LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView myRecyclerView= (RecyclerView) findViewById(R.id.my_recycler_view);
myRecyclerView.setLayoutManager(layoutManager);