java 필요한 부분만 가져왔다.
List<MemberInfo> followerList = new Vector<>();
...
ListAdapter listAdapter = new FollowListAdapter(getContext(),R.layout.follow_list_layout, followerList, server, "follower",loginEmail);
new MaterialAlertDialogBuilder(getContext())
.setTitle("팔로워")
.setAdapter(listAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//클릭 이벤트 작성
}
}).show();
- followerList에는 서버에 요청하여 받은 데이터가 저장되어 있다. (동적으로 팔로우 리스트를 보여줄 수 있다.)
- MemberInfo는 이미지 url, 이메일이 들어있는 class 이다. (getter, setter 가 작성되어 있음)
MaterialAlertDialogBuilder에 리스트뷰를 적용하고 싶으면 ListAdapter를 사용하면 된다.
(안드로이드 프로젝트할 때 가장 많이 봤던 사이트 중 하나는 Material Design 사이트이다.
다양한 AlertDialog 디자인을 제공한다. 아래 링크 참조)
material.io/components/dialogs/android
Material Design
Build beautiful, usable products faster. Material Design is an adaptable system—backed by open-source code—that helps teams build high quality digital experiences.
material.io
- ListAdapter에 적용할 layout을 작성한다.
- 프로필 사진엔 CircleImageView 라이브러리를 적용했다.
implementation 'de.hdodenhof:circleimageview:3.0.1'
링크 참조 : github.com/hdodenhof/CircleImageView
hdodenhof/CircleImageView
A circular ImageView for Android. Contribute to hdodenhof/CircleImageView development by creating an account on GitHub.
github.com
follow_list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/follow_img"
android:layout_width="40dp"
android:layout_height="40dp"/>
<TextView
android:id="@+id/follow_id"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
FollowListAdapter.java - 데이터 받아오는 부분은 제외했다.
public class FollowListAdapter extends ArrayAdapter {
private Context context;
private int resource;
private String server,loginEmail,sep;
private FollowListAdapter adapter;
public FollowListAdapter(@NonNull Context context, int resource, @NonNull List objects, String server, String sep, String loginEmail) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
this.server = server;
this.sep = sep;
this.loginEmail = loginEmail;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if(convertView==null)
convertView=View.inflate(context, resource,null);
TextView follow_id = convertView.findViewById(R.id.follow_id);
CircleImageView follow_img = convertView.findViewById(R.id.follow_img);
Picasso.get().load(server+"upload/"+followList.get(position).getProfileImage()).placeholder(R.drawable.gray).into(follow_img);
follow_id.setText(followList.get(position).getEmail());
return convertView;
}
}
- ArrayAdapter를 상속 받는다.
- 인자 생성자에는 필요한 데이터를 구성한다.
- getView()에는 layout에 데이터를 세팅한다.
서버에 데이터를 요청해서 받는 부분이 빠지니까 굉장히 코드가 짧아졌다.
'공부기록 > 안드로이드' 카테고리의 다른 글
[안드로이드] Fragment to Fragment 데이터 쉽게 전달하기(자료형이 List 일 때) (0) | 2021.01.27 |
---|---|
[JSOUP사용법] 웹(HTML) JSOUP으로 파싱하기 (웹-안드로이드 연동/네이티브앱) (0) | 2021.01.27 |
안드로이드 RecyclerView를 이용해 이미지 게시판 만들기(인스타그램 따라해보기) (0) | 2021.01.25 |
안드로이드 프래그먼트 안에 프래그먼트 - 인스타그램처럼 만들기 (0) | 2021.01.25 |
안드로이드에서 유튜브 재생하기 구현(api 미사용) (0) | 2021.01.24 |