728x90
반응형
●fragment_board.xml
<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"
tools:context=".BoardFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="게시글 추가"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</FrameLayout>
FrameLaout 안에 LinearLayout을 vertical정렬로 설정하고
RecyclerView를 만든다 ( id도 설정한다 )
●board_list.xml
<?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="250dp"
android:orientation="vertical">
<TextView
android:id="@+id/board_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="board_no" />
<TextView
android:id="@+id/board_subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="board_subject"/>
<TextView
android:id="@+id/board_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="board_content"/>
</LinearLayout>
RecyclerView에 들어갈 item을 만든다
●BoardAdapter.java
public class BoardAdapter extends RecyclerView.Adapter<BoardAdapter.ViewHolder> {
private String[] localDataSet;
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public ViewHolder(View view) {
super(view);
// Define click listener for the ViewHolder's View
textView = (TextView) view.findViewById(R.id.textView);
}
}
public BoardAdapter(String[] dataSet) {
localDataSet = dataSet;
}
// Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
// Create a new view, which defines the UI of the list item
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.board_list, viewGroup, false);
return new ViewHolder(view);
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
// Get element from your dataset at this position and replace the
// contents of the view with that element
viewHolder.textView.setText(position);
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return localDataSet.length;
}
}
https://developer.android.com/guide/topics/ui/layout/recyclerview?hl=ko
참고해서 기본 RecyclerAdapter를 만들고
onCreateViewHolder에 layout을 방금 만들었던 board_list로 지정해준다
●BoardData.java
public String getBoard_no() {
return board_no;
}
public void setBoard_no(String board_no) {
this.board_no = board_no;
}
public String getBoard_subject() {
return board_subject;
}
public void setBoard_subject(String board_subject) {
this.board_subject = board_subject;
}
public String getBoard_content() {
return board_content;
}
public void setBoard_content(String board_content) {
this.board_content = board_content;
}
사용할 데이터를 새로운 파일에서 getter, setter만들어준다
●BoardFragment.java
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_board, container, false);
recyclerView = view.findViewById(R.id.recyclerview);
//리사이클러뷰의 레이아웃 매니저 설정
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
List<BoardData> boardDataList = new ArrayList<>();
BoardData boardData = new BoardData();
boardData.setBoard_no("1");
boardData.setBoard_subject("sub");
boardData.setBoard_content("cont");
boardDataList.add(boardData);
Log.d("board", String.valueOf(boardDataList));
adapter = new BoardAdapter(boardDataList);
recyclerView.setAdapter(adapter);
return view;
}
BoardFragment에서 adapter와 layoutManager를 꼭 RecyclerView안에서로 지정!
boardData에 임의로 값을 set해주고 boards에 add헀다
그 board를 adapter에 집어넣고
그 adapter를 recyclerview에 setAdapter해서 마무리!!
이제 Adapter로 ▼▼▼
●BoardAdapter,java
private List<BoardData> boardDataList;
public BoardAdapter(List<BoardData> boardDataset) {
boardDataList = boardDataset;
}
List형식으로 boardDataList로 바꾸고
@Override
public void onBindViewHolder(BoardViewHolder viewHolder, int position) {
BoardData boardData = boardDataList.get(position);
Log.d("boarddata", String.valueOf(boardData));
viewHolder.board_no.setText(boardData.getBoard_no());
viewHolder.board_subject.setText(boardData.getBoard_subject());
viewHolder.board_content.setText(boardData.getBoard_content());
}
boardDataList에서 get해서
각각 맞는 viewHolder의 TextView에서 setText한다
@Override
public int getItemCount() {
return boardDataList == null ? 0 : boardDataList.size();
}
ItemCount는 null이아닐때 boardList의 size만큼으로 변경한다
반응형
'Android' 카테고리의 다른 글
[Android/Java] 회원가입 사용할 JSP < - >Mysql 연동 (0) | 2022.01.28 |
---|---|
Adapter에서 ClickLisetener , Toast (0) | 2022.01.27 |
[Android/Java] BottomNavigation 클릭시 색상 (0) | 2022.01.25 |
[Android/java] php&mysql 사용해서 로그인 (0) | 2022.01.24 |
[Android/Java] I/System.out: (HTTPLog)-Static: isSBSettingEnabled false 에러 (0) | 2022.01.24 |