Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 设置卡片视图和滚动动画的背景_Android_Scroll - Fatal编程技术网

Android 设置卡片视图和滚动动画的背景

Android 设置卡片视图和滚动动画的背景,android,scroll,Android,Scroll,此外,我还试图设置我的应用程序中的回收器视图中使用的a卡视图的背景,为此我做了以下操作 卡片xml: <?xml version="1.0" encoding="utf-8"?> <androidx.cardview.widget.CardView android:layout_height="120dp" android:layout_width="200dp" android:layout_margin="5dp" xmlns:andro

此外,我还试图设置我的应用程序中的回收器视图中使用的a卡视图的背景,为此我做了以下操作

卡片xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView

    android:layout_height="120dp"

    android:layout_width="200dp"
    android:layout_margin="5dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="5dp">
    <LinearLayout
        android:id="@+id/home_card_subject"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <TextView
            android:textColor="#ffffff"
            android:id="@+id/home_card_subject_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/subject_text"
            android:textSize="30sp"
            android:gravity="bottom|center_horizontal"
            android:padding="5dp"
            android:layout_weight="70"/>
        <TextView
            android:textColor="#ffffff"
            android:id="@+id/home_card_credits"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="4 credits"
            android:gravity="center_horizontal"
            android:layout_weight="30"
           />


    </LinearLayout
        >
</androidx.cardview.widget.CardView>


我想要一种滚动效果,底部在顶部滚动,如下图所示,而中间部分在紫色部分滚动,而不是与紫色部分一起滚动。

您没有为cardview定义任何id,并为cardview提供线性布局参考,因此cardview变为空,并出现空指针异常。请将id设置为cardView并将该id引用给cardView。

您没有为cardView定义任何id,并将线性布局引用给cardView,以便cardView变为null并出现null指针异常。请将id设置为cardView,并将该id引用到cardView。

您获得的id是线性布局,而不是卡视图。您可以通过设置carview CardBackground色调来更改背景。我在cardview内部使用了一个线性布局,它覆盖了整个卡,所以我正在设置线性布局的颜色。请您再解释一下。所以为什么不更改cardview背景,而不是线性布局?\n我尝试了,但它给出了相同的错误CardViewSetCardBackgroundColor(getRandomColorCode())您得到的id是线性布局,而不是卡片视图。您可以通过设置carview CardBackground色调来更改背景。我在cardview内部使用了一个线性布局,它覆盖了整个卡,所以我正在设置线性布局的颜色。请您再解释一下。所以为什么不更改cardview背景,而不是线性布局?\n我尝试过,但它给出了相同的错误CardViewSetCardBackgroundColor(getRandomColorCode())我声明了一个名为cardwiew的线性布局,然后为线性布局找到了dviewbyd,我正在尝试设置整个线性布局的颜色,但它不起作用。请您再解释一下,使用此代码设置线性布局的背景色linear.setBackgroundColor(ContextCompat.getColor(activity,R.color.black)); 如果线性布局的线性通行证id i声明了一个名为cardview的线性布局,然后为线性布局找到了VIEWBYID,我正在尝试设置整个线性布局的颜色,但它不起作用。请您再解释一点,使用此代码设置线性布局的背景色。setBackgroundColor(ContextCompat.getColor(activity,R.color.black));其中线性布局的线性过程id
package com.example.app100.ui.home;


public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;
    LinearLayout cardView;
    private ArrayList<Subject> subject = new ArrayList<>();
    private DatabaseHelper databaseHelper;

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

        databaseHelper = new DatabaseHelper(getContext());
        populateArrayList();


    //homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);


        View root = inflater.inflate(R.layout.fragment_home, container, false);
        cardView =  root.findViewById(R.id.home_card_subject);
        /*final TextView textView = root.findViewById(R.id.text_home);
        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });*/
        Random rand = new Random();

        int rand_int1 = rand.nextInt(3);


        switch (rand_int1){
            case 0:
                cardView.setBackgroundResource(R.drawable.gradient_color1);
                break;
            case 1:
                cardView.setBackgroundResource(R.drawable.gradient_color2);
                break;
            case 2:
                cardView.setBackgroundResource(R.drawable.gradient_color3);
                break;
        }


        RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.home_recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext() , LinearLayoutManager.HORIZONTAL , false));
        recyclerView.setAdapter(new RecyclerAdapter(subject));

 //       recycle.setLayoutManager(new GridLayoutManager( getActivity(),2));
//          recycle.setAdapter(new RecyclerAdapter(subList));

        return root;
    }


    private void populateArrayList(){
        Log.d(TAG , "populating array list");

        Cursor data = databaseHelper.getdata();
        while(data.moveToNext()){
            Subject sub = new Subject(data.getString(0) , data.getInt(1), data.getInt(2),
                    data.getInt(3), data.getInt(4),data.getInt(5) , data.getInt(6));
            subject.add(sub);
        }
    }
}


2020-04-09 12:18:18.142 15575-15575/com.example.app100 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.app100, PID: 15575
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setBackgroundResource(int)' on a null object reference
        at com.example.app100.ui.home.HomeFragment.onCreateView(HomeFragment.java:65)