티스토리 뷰
CoordinatorLayout + ViewPager2 + TabLayout
build.gradle (Module: app)
dependencies {
// material design
implementation 'com.google.android.material:material:1.1.0'
// ViewPager2
implementation 'androidx.viewpager2:viewpager2:1.0.0'
// RecyclerView
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
recyclerview_item.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvName"
android:hint="James Kim"
android:textSize="20dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
RecyclerViewAdapter.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;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private List<Integer> dataSet;
public RecyclerViewAdapter(List<Integer> dataSet) {
this.dataSet = dataSet;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvName;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
this.tvName = itemView.findViewById(R.id.tvName);
}
public void bind(int position) {
this.tvName.setText((position+1) + " 혀가 길지 않은 개발자");
}
}
@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 dataSet.size();
}
}
viewpager_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:listitem="@layout/recyclerview_item" />
</FrameLayout>
ViewPagerFragmentRed.java
package com.jwsoft.javaproject;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class ViewPagerFragmentRed extends Fragment {
private RecyclerView recyclerView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.viewpager_fragment, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(Color.parseColor("#FFBBBB"));
recyclerView = view.findViewById(R.id.rvName);
List<Integer> dataSet = new ArrayList<>();
for (int i=1; i<=100; i++) {
dataSet.add(i);
}
RecyclerViewAdapter adapter = new RecyclerViewAdapter(dataSet);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(
new LinearLayoutManager(view.getContext(),
RecyclerView.VERTICAL,
false)
);
}
}
ViewPagerFragmentGreen.java
package com.jwsoft.javaproject;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class ViewPagerFragmentGreen extends Fragment {
private RecyclerView recyclerView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.viewpager_fragment, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(Color.parseColor("#BBFFBB"));
recyclerView = view.findViewById(R.id.rvName);
List<Integer> dataSet = new ArrayList<>();
for (int i=1; i<=100; i++) {
dataSet.add(i);
}
RecyclerViewAdapter adapter = new RecyclerViewAdapter(dataSet);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(
new LinearLayoutManager(view.getContext(),
RecyclerView.VERTICAL,
false)
);
}
}
ViewPagerFragmentBlue.java
package com.jwsoft.javaproject;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class ViewPagerFragmentBlue extends Fragment {
private RecyclerView recyclerView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.viewpager_fragment, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(Color.parseColor("#BBBBFF"));
recyclerView = view.findViewById(R.id.rvName);
List<Integer> dataSet = new ArrayList<>();
for (int i=1; i<=100; i++) {
dataSet.add(i);
}
RecyclerViewAdapter adapter = new RecyclerViewAdapter(dataSet);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(
new LinearLayoutManager(view.getContext(),
RecyclerView.VERTICAL,
false)
);
}
}
ViewPagerFragmentAdapter.java
package com.jwsoft.javaproject;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import java.util.ArrayList;
import java.util.List;
public class ViewPagerFragmentAdapter extends FragmentStateAdapter {
private final int TYPE_RED = 0;
private final int TYPE_GREEN = 1;
private final int TYPE_BLUE = 2;
private List<Integer> listType = new ArrayList<>();
public ViewPagerFragmentAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
listType.add(TYPE_RED);
listType.add(TYPE_GREEN);
listType.add(TYPE_BLUE);
}
@NonNull
@Override
public Fragment createFragment(int position) {
switch (position) {
case TYPE_RED:
return new ViewPagerFragmentRed();
case TYPE_GREEN:
return new ViewPagerFragmentGreen();
default:
return new ViewPagerFragmentBlue();
}
}
@Override
public int getItemCount() {
return listType.size();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolBar"
app:title="혀가 길지 않은 개발자"
app:titleTextColor="@android:color/white"
app:subtitle="CoordinatorLayout + ViewPager2 + TabLayout"
app:subtitleTextColor="@android:color/white"
app:layout_scrollFlags="scroll|enterAlways"
android:background="#FF7F00"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager2"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
package com.jwsoft.javaproject;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
public class MainActivity extends AppCompatActivity {
private ViewPager2 viewPager2;
private TabLayout tabLayout;
private AppBarLayout appBarLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager2 = findViewById(R.id.viewPager2);
tabLayout = findViewById(R.id.tabLayout);
appBarLayout = findViewById(R.id.appBarLayout);
ViewPagerFragmentAdapter adapter = new ViewPagerFragmentAdapter(this);
viewPager2.setAdapter(adapter);
viewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
switch (position) {
case 0:
tab.setText("RED");
break;
case 1:
tab.setText("GREEN");
break;
default:
tab.setText("BLUE");
break;
}
}
}).attach();
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
appBarLayout.setExpanded(true);
}
});
}
}
'Android > Java' 카테고리의 다른 글
[Java] Toolbar + include (0) | 2020.08.05 |
---|---|
[Java] ImageView 타원화 (0) | 2020.08.04 |
[Java] import com.google.gson.* (0) | 2020.07.31 |
[Java] import org.json.* (0) | 2020.07.30 |
[Java] Thread.join() (0) | 2020.07.27 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- fragment
- MVVM
- 안드로이드 #코틀린 #Android #Kotlin
- 코틀린
- 자바
- ViewPager2
- ArrayList
- Design Pattern
- Intent
- View
- CoordinatorLayout
- recyclerview
- DataBinding
- Android
- Livedata
- activity
- JSONObject
- XML
- TabLayout
- coroutine
- JSONArray
- 안드로이드
- Architecture Pattern
- handler
- ViewModel
- Vue.js #Vue.js + javascript
- java
- 혀가 길지 않은 개발자
- James Kim
- Kotlin
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함