반응형
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 |
package com.ktds.myarticledetail;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction().add(R.id.container,new ArticleListFragment()).commit();
}
}
|
cs |
acticity_main.xml
1
2
3
4
5
6
7
8
9 |
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
|
cs |
ArticleListFragMent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 |
package com.ktds.myarticledetail;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class ArticleListFragment extends Fragment {
private ListView articleList;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.article_list_fragment, container, false);
articleList = (ListView) rootView.findViewById(R.id.articleList);
List<String> list = new ArrayList<String>();
for(int i=0; i<50; i++){
list.add(i+"");
}
ArticleListAdapter articleListAdapter = new ArticleListAdapter(list, getActivity());
articleList.setAdapter(articleListAdapter);
return rootView;
}
}
|
cs |
article_list_fragment.xml
1
2
3
4
5
6
7
8
9
10
11
12 |
<?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">
<ListView
android:id="@+id/articleList"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout> |
cs |
ArticleDetailFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 |
package com.ktds.myarticledetail;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ArticleDetailFragment extends Fragment {
/*
public static ArticleDetailFragment = new
실무에서 사용하는 로직 구현하기 !! 사진 찍어 놨음..
*/
private TextView detail;
private String detatailId;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.article_detail_fragment, container, false);
detail = (TextView) rootView.findViewById(R.id.detail);
Bundle args = getArguments();
if(args != null){
detatailId = args.getString("id");
detail.setText("넘겨받은 값 : " +detatailId);
}
return rootView;
}
}
|
cs |
article_detail_fragment.xml
1
2
3
4
5
6
7
8
9
10
11
12
13 |
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
|
cs |
ArticleListAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 |
package com.ktds.myarticledetail;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by 206-001 on 2015-06-25.
*/
public class ArticleListAdapter extends BaseAdapter {
private List<String> items;
private Context context;
public ArticleListAdapter(List<String> items, Context context) {
this.items = items;
this.context = context;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = null;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.article_list_item, parent, false);
holder = new Holder();
holder.listItem = (TextView) convertView.findViewById(R.id.list_item);
}
else{
holder = (Holder) convertView.getTag();
}
final String content = String.valueOf(getItem(position));
holder.listItem.setText(content);
convertView.setTag(holder);
holder.listItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putString("id", content);
ArticleDetailFragment detail = new ArticleDetailFragment();
detail.setArguments(bundle);
FragmentTransaction transaction = ((FragmentActivity)context).getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, detail);
transaction.addToBackStack(null);
transaction.commit();
}
});
return convertView;
}
private class Holder{
public TextView listItem;
}
}
|
cs |
article_list_item.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
<?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:padding="16dp"
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout> |
cs |
반응형
'IT > Android' 카테고리의 다른 글
안드로이드 특강 (0) | 2015.06.29 |
---|---|
Fragment 전환 (0) | 2015.06.25 |
View Pager (0) | 2015.06.25 |
Networking - API 요청하기 (0) | 2015.06.24 |
Networking - 웹으로 요청하기 (0) | 2015.06.24 |