티스토리 뷰

Android/Java

[Java]  NestedScrollView

혀가 길지 않은 개발자 2020. 8. 10. 17:27

build.gradle (Module: app)

dependencies {
    // RecyclerView
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
}

 

 

 

 

 

 

recyclerview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="James Kim"
        android:textSize="26dp"
        android:textAlignment="center"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

recyclerview_item.xml

 

 

 

 

 

MyAdapter.java

package com.jwsoft.javaproject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void bind(int position) {
            tvName = itemView.findViewById(R.id.tvName);
            tvName.setText("James Kim" + position);
        }

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.recyclerview_item, parent, false);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.bind(position);
    }

    @Override
    public int getItemCount() {
        return 10;
    }
}

 

 

 

 

 

 

 

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">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:src="@drawable/ic_launcher_foreground"
                android:background="@drawable/ic_launcher_background"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="contents 1"
                android:textStyle="bold"
                android:textSize="30dp"
                android:layout_marginTop="30dp"/>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rvContentOne"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="30dp"
                tools:listitem="@layout/recyclerview_item"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="contents 2"
                android:textStyle="bold"
                android:textSize="30dp"
                android:layout_marginTop="30dp"/>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rvContentTwo"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="30dp"
                tools:listitem="@layout/recyclerview_item"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="contents 3"
                android:textStyle="bold"
                android:textSize="30dp"
                android:layout_marginTop="30dp"/>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rvContentThree"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="30dp"
                tools:listitem="@layout/recyclerview_item"/>

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

recyclerview_item.xml

 

 

 

 

 

MainActivity.java

package com.jwsoft.javaproject;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    RecyclerView rvOne;
    RecyclerView rvTwo;
    RecyclerView rvThree;

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

        rvOne = findViewById(R.id.rvContentOne);
        rvTwo = findViewById(R.id.rvContentTwo);
        rvThree = findViewById(R.id.rvContentThree);

        MyAdapter adapter = new MyAdapter();

        rvOne.setAdapter(adapter);
        rvOne.addItemDecoration(new DividerItemDecoration(this, RecyclerView.VERTICAL));
        rvOne.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));

        rvTwo.setAdapter(adapter);
        rvTwo.addItemDecoration(new DividerItemDecoration(this, RecyclerView.HORIZONTAL));
        rvTwo.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));

        rvThree.setAdapter(adapter);
        rvThree.addItemDecoration(new DividerItemDecoration(this, RecyclerView.VERTICAL));
        rvThree.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));

    }

}

실행 결과

 

 

 

'Android > Java' 카테고리의 다른 글

[Java]  SeekBar  (0) 2020.08.15
[Java]  Glide  (0) 2020.08.10
[Java]  ProgressBar  (0) 2020.08.07
[Java]  BuildConfig  (0) 2020.08.07
[Java]  enum  (0) 2020.08.07
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함