Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / FriendActivity.java
package com.example.nemophila;

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

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class FriendActivity extends AppCompatActivity {
    private FriendActivityAdapter adapter;

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

        // 初期化
        List<FriendDataModel> friendsDataset = new ArrayList<>();  // 1行分の情報を持つDataModelを格納するリスト
        RecyclerView rv = findViewById(R.id.friendsList);  // recyclerViewを取得
        adapter = new FriendActivityAdapter(friendsDataset);  // adapterを取得

        //一行ずつを縦に(LinearLayout)表示するLayoutManagerを宣言
        LinearLayoutManager llm = new LinearLayoutManager(this);

        // 詳細不明(おまじない)
        RecyclerView.ItemDecoration itemDecoration =
                new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        rv.addItemDecoration(itemDecoration);
        rv.setHasFixedSize(true);
        rv.setLayoutManager(llm);

        // マイページ画面に画面遷移する
        Button backButton = findViewById(R.id.backMyPage);
        backButton.setOnClickListener(v -> {
            Intent intent = new Intent(getApplication(), MyPageActivity.class);
            startActivity(intent);
        });
        
    }

    // ----------- RecyclerView用の内部クラス ------------//
    // Adapter
    public class FriendActivityAdapter extends RecyclerView.Adapter<FriendsViewHolder> {
        private List<FriendDataModel> list;

        public FriendActivityAdapter(List<FriendDataModel> list) {
            this.list = list;
        }

        public void setList(List<FriendDataModel> list) {
            this.list = list;
        }

        @NonNull
        @Override
        public FriendsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int ViewType) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_friends_activity, parent,false);
            FriendsViewHolder vh = new FriendsViewHolder(inflate);
            return vh;
        }

        @Override
        public void onBindViewHolder(@NonNull FriendsViewHolder holder, int position) {
            // 後からアイコンの登録をできるようにする
            holder.nameView.setText(list.get(position).getName());
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    // ViewHolder
    public class FriendsViewHolder extends RecyclerView.ViewHolder {
        public ImageView iconView;
        public TextView nameView;
        public Button deleteButton;

        public FriendsViewHolder(@NonNull View itemView) {
            super(itemView);
            iconView = itemView.findViewById(R.id.friendIcon);
            nameView = itemView.findViewById(R.id.friendName);
            deleteButton = itemView.findViewById(R.id.friendDelete);
        }
    }

    // DataModel
    public static class FriendDataModel {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}