<구현화면>
1. activity_dog_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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DogMainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
2. 위 리스트뷰 안에 들어갈 레이아웃 생성 (activity_list_item.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".list_item">
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="100dp"
android:layout_weight="2"
android:background="@drawable/cat"
android:scaleType="centerInside" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="9"
android:orientation="vertical"
android:weightSum="10">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="134dp"
android:layout_weight="2"
android:gravity="center_vertical"
android:text="New Text"
android:layout_margin="10dp"
android:textSize="24dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="470dp"
android:layout_weight="6"
android:text="New Text"
android:layout_margin="10dp"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
* 이미지는 임시로 고양이 이미지 사용
3. 데이터를 받을 list_item.java 클래스 생성
public class list_item extends AppCompatActivity {
public String name;
public String desc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
}
public list_item(String name, String desc) {
this.name = name;
this.desc = desc;
}
public String getName()
{
return this.name;
}
public String getDesc()
{
return this.desc;
}
}
4. dog_main과 list_item 클래스를 연결해줄 ListViewAdapter.java 클래스 생성
public class ListViewAdapter extends BaseAdapter {
Context mContext = null;
LayoutInflater mLayoutInflater = null;
ArrayList<list_item> list;
public ListViewAdapter(Context context, ArrayList<list_item> listViewItemList) {
mContext = context;
list = listViewItemList;
mLayoutInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return list.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = mLayoutInflater.inflate(R.layout.activity_list_item, null);
TextView titleTextView = (TextView) view.findViewById(R.id.textView1) ;
TextView descTextView = (TextView) view.findViewById(R.id.textView2) ;
titleTextView.setText(list.get(position).getName());
descTextView.setText(list.get(position).getDesc());
return view;
}
}
5. DogMainActivity.java
this.InitializeData();
ListView listview = (ListView) findViewById(R.id.listView);
final ListViewAdapter adapter = new ListViewAdapter(this,DataList);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView parent, View v, int position, long id){
}
});
XmlPullParser_fsk fsk = new XmlPullParser_fsk();
private void InitializeData() {
DataList = new ArrayList<list_item>();
DataList.add(new list_item("test1","울랄라1"));
DataList.add(new list_item("tt2","룰루2"));
DataList.add(new list_item("test3","울랄라3"));
DataList.add(new list_item("tt4","울랄라4"));
DataList.add(new list_item("test5","룰루5"));
DataList.add(new list_item("test6","울랄라6"));
DataList.add(new list_item("test7","룰루7"));
DataList.add(new list_item("tt8","울랄라8"));
}
'Android > UI-UX 디자인' 카테고리의 다른 글
[Android/Layout]DB 데이터를 Fragment 내 RecyclerView에 뿌리기(MS-SQL) (0) | 2021.05.08 |
---|---|
[Android/Layout]Fragment에서 버튼을 생성하여 버튼 클릭 시 Activity로 intent하기 (0) | 2021.05.08 |
[Android/Layout]BottomNavigationView와 ViewPager2를 이용한 화면 구축 (0) | 2021.05.08 |
[Android/Layout]ListView Search 기능 만들기 (0) | 2021.02.08 |
[Android/Layout]ListView Click Event : 팝업창 생성 (0) | 2021.02.07 |