예제 출처 : https://www.inflearn.com/course
예제 소스 : https://github.com/GaKaRi/gakari_android/commit/595d4bd0a74854d8b0d52ea185d5aac8e0b79f0f
실행화면
1. 먼저 로그인을 합니다.
2. 로그인에 성공 후 다음과 같이 리스트뷰로 공지사항이 올라오는 것을 볼 수 있습니다.
예제 구현 시작
res/drawable 에서 오른쪽 누르고 Vector Asset선택 후 다음과 같이 Clip Art를 선택해서 지정해줍니다.
res/drawable/ic_textsms_black_24dp.xml
아래 fillColor를 #FFFFFFFF 로 지정합니다.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M20,2L4,2c-1.1,0 -1.99,0.9 -1.99,2L2,22l4,-4h14c1.1,0 2,-0.9 2,-2L22,4c0,-1.1 -0.9,-2 -2,-2zM9,11L7,11L7,9h2v2zM13,11h-2L11,9h2v2zM17,11h-2L15,9h2v2z"/>
</vector>
res/layout/notice.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="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="notice"
android:layout_gravity="center"
android:padding="10dp"
android:textColor="#ffffff"
android:id="@+id/noticeText"
android:drawableStart="@drawable/ic_textsms_black_24dp"
android:drawableLeft="@drawable/ic_textsms_black_24dp"
android:background="@color/colorPrimary"
android:textStyle="bold"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="gakari"
android:layout_gravity="center"
android:padding="3dp"
android:layout_marginLeft="8dp"
android:textColor="#ffffff"
android:textColorHint="#ffffff"
android:id="@+id/nameText"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="2017-01-01"
android:layout_gravity="center"
android:padding="3dp"
android:textColor="#ffffff"
android:textColorHint="#ffffff"
android:id="@+id/dateText"
/>
</LinearLayout>
</LinearLayout>
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.kch.registration_v7.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/courseButton"
android:text="course list"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:textSize="18dp"
android:background="@color/colorPrimary"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/scheduleButton"
android:text="schedule"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:textSize="18dp"
android:background="@color/colorPrimary"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/statisticsButton"
android:text="statistics"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:textSize="18dp"
android:background="@color/colorPrimary"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/notice">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical"
android:background="@color/colorPrimary"
android:layout_marginTop="8dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:src="@drawable/univlogo"
android:scaleType="fitCenter"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Notice"
android:textColor="#ffffff"
android:textSize="25dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
/>
</LinearLayout>
//아래 부분이 추가됬습니다. 리스트뷰를 뿌려주기 위한 부분입니다.
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/noticeListView"
android:layout_margin="10dp"
android:dividerHeight="10dp"
android:divider="#ffffff"
>
</ListView>
</LinearLayout>
<RelativeLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"></RelativeLayout>
</FrameLayout>
</LinearLayout>
Notice.java
리스트뷰에 뿌려주는 데이터를 담기 위한 클래스입니다.
package com.example.kch.registration_v7;
public class Notice
String notice;
String name;
public Notice(String notice, String name, String date)
this.notice = notice;
this.name = name;
this.date = date;
String date;
public String getNotice()
return notice;
public void setNotice(String notice)
this.notice = notice;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getDate()
return date;
public void setDate(String date)
this.date = date;
NoticeListAdapter.java
리스트뷰에 데이터를 어떻게 뿌려줄지 구현하는 부분입니다.
package com.example.kch.registration_v7;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.List;
public class NoticeListAdapter extends BaseAdapter
private Context context;
private List<Notice> noticedList;
public NoticeListAdapter(Context context, List<Notice> noticedList)
this.context = context;
this.noticedList = noticedList;
@Override
public int getCount()
return noticedList.size();//리스트뷰의 총 갯수
@Override
public Object getItem(int position)
return noticedList.get(position);//해당 위치의 값을 리스트뷰에 뿌려줌
@Override
public long getItemId(int position)
return position;
//리스트뷰에서 실질적으로 뿌려주는 부분임
@Override
public View getView(int position, View convertView, ViewGroup parent)
View v = View.inflate(context, R.layout.notice, null);
TextView noticeText = (TextView)v.findViewById(R.id.noticeText);
TextView nameText = (TextView)v.findViewById(R.id.nameText);
TextView dateText = (TextView)v.findViewById(R.id.dateText);
noticeText.setText(noticedList.get(position).getNotice());
nameText.setText(noticedList.get(position).getName());
dateText.setText(noticedList.get(position).getDate());
v.setTag(noticedList.get(position).getNotice());
return v;
MainActivity.java
리스트뷰에 어떤 데이터를 뿌려줄지 연결하는 부분입니다.
package com.example.kch.registration_v7;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity
//7강때 추가된 부분
private ListView noticeListView;
private NoticeListAdapter adapter;
private List<Notice> noticedList;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//7강때 추가된 부분
noticeListView = (ListView)findViewById(R.id.noticeListView);
noticedList = new ArrayList<Notice>();
noticedList.add(new Notice("공지사항", "gakari", "2018-09-09"));
noticedList.add(new Notice("공지사항", "gakari", "2018-09-10"));
noticedList.add(new Notice("공지사항", "gakari", "2018-09-11"));
noticedList.add(new Notice("공지사항", "gakari", "2018-09-12"));
noticedList.add(new Notice("공지사항", "gakari", "2018-09-13"));
noticedList.add(new Notice("공지사항", "gakari", "2018-09-14"));
noticedList.add(new Notice("공지사항", "gakari", "2018-09-15"));
noticedList.add(new Notice("공지사항", "gakari", "2018-09-16"));
noticedList.add(new Notice("공지사항", "gakari", "2018-09-17"));
noticedList.add(new Notice("공지사항", "gakari", "2018-09-18"));
noticedList.add(new Notice("공지사항", "gakari", "2018-09-19"));
noticedList.add(new Notice("공지사항", "gakari", "2018-09-20"));
adapter = new NoticeListAdapter(getApplicationContext(), noticedList);
noticeListView.setAdapter(adapter);
final Button courseButton = (Button)findViewById(R.id.courseButton);
final Button statisticsButton = (Button)findViewById(R.id.statisticsButton);
final Button scheduleButton = (Button)findViewById(R.id.scheduleButton);
final LinearLayout notice = (LinearLayout)findViewById(R.id.notice);
courseButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
notice.setVisibility(View.GONE);//공지사항이 안보임
courseButton.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
statisticsButton.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
scheduleButton.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment, new CourseFragment());//프래그먼트를 바꿔줌
fragmentTransaction.commit();
);
statisticsButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
notice.setVisibility(View.GONE);//공지사항이 안보임
courseButton.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
statisticsButton.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
scheduleButton.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment, new StatisticsFragment());//프래그먼트를 바꿔줌
fragmentTransaction.commit();
);
scheduleButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
notice.setVisibility(View.GONE);//공지사항이 안보임
courseButton.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
statisticsButton.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
scheduleButton.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment, new ScheduleFragment());//프래그먼트를 바꿔줌
fragmentTransaction.commit();
);