마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

listfragment 레이아웃적용이 안되요

0 추천
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {



    adapter = new ListViewAdapter() ;
    setListAdapter(adapter);

    JsonReadTask task = new JsonReadTask();
    try {
        String resultTask = task.execute(url).get();
        Log.e("msg : ",resultTask);
    } catch (InterruptedException e) {
        e.printStackTrace();
        Log.e("msg : ","error");
    } catch (ExecutionException e) {
        e.printStackTrace();
        Log.e("msg : ","error");
    }

    return super.onCreateView(inflater,container,savedInstanceState);
}
@Override
public Fragment getItem(int position) {
    if (position == 0) {
        return new HomeFragment();
    }
    else if(position == 1 ){
        return new TinfoFragment();
    }
    else if(position == 2){
        return new LabinfoFragment();
    }
    else if(position == 3){
        return new TnoteFragment();
    }
    else if(position == 4){

        return new StunoteFragment();
    }
    else if(position == 5){


        return new JobInfoFragment();
    }
    else {
        return new HelpFragment();
    }
}

맨 아래꺼가 fragmentPagerAdapter를 이용해서 화면에 fragment를 띄어주는 거구요

중간에 있는 소스가 StunoteFragment입니다.

StunoteFragment가 생성될때 나와야되는 레이아웃이 

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.jhkim.test2.Home"
    android:id="@+id/Stunote">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/stunotepad_listview"
        android:name="com.example.jhkim.test2.StunoteFragment"
        android:layout_marginBottom="100dp"
        />

</RelativeLayout>

이거 인데 아래에 여백을 줘도 여백이 적용이 안되네요...

뱅이 (620 포인트) 님이 2017년 9월 20일 질문

1개의 답변

+1 추천
 
채택된 답변
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.stunote_layout, container, false);

        return rootView;
    }

지금 원하는 의도대로 하려면 StunoteFragment 클래스에서 onCreateView에서 화면에 띄울 레이아웃을 지정해야 됩니다.

레이아웃xml에서는 <fragment ...../> 부분을 빼구요.

뷰페이져를 사용하는 경우 일반적인 프레그먼트를 액티비티에 띄우는 것과 흐름이 조금 다릅니다.

일반적1: activity -> setContentView -> FragmentManager.replace 

일반적2: activity -> setContentView -> (xml) <fragment ...프래그먼트 지정.../> -> 해당 Fragment호출

반면

뷰페이져: getItem -> Fragment return됨. -> Fragment의 onCreateView의 (xml)rootView 띄워줌.

(물론 여기서 (xml)rootView에 <fragment .../>가 포함되어 있다면 여기 있는 fragment를 다시 호출하고, 그런식으로 쓸수도 있습니다.)

 

*** 추가로 getItem()에서 해당 코드는 실행될 일이 없습니다.

    else {
        return new HelpFragment();
밀면 (630 포인트) 님이 2017년 9월 20일 답변
뱅이님이 2017년 10월 31일 채택됨
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.stunote, container, false);

        stuListView= (ListView) view.findViewById(R.id.stunotepad_listview);
        adapter =  new ListViewAdapter();
        stuListView.setAdapter(adapter);

        JsonReadTask task = new JsonReadTask();
        try {
            String resultTask = task.execute(url).get();
            Log.e("msg : ",resultTask);
        } catch (InterruptedException e) {
            e.printStackTrace();
            Log.e("msg : ","error");
        } catch (ExecutionException e) {
            e.printStackTrace();
            Log.e("msg : ","error");
        }
        adapter.notifyDataSetChanged();

        return view;

이렇게 바꾸고 fragment를 listview로 바꿔서 해봤는데 실행은 되는데 화면에 아무것도 안뜨네요ㅠㅠ 데이터는 log로 찍어봤는데 다 나옵니다...
onCreateView에서

   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.testlistview, container, false);

        stuListView = (ListView) view.findViewById(R.id.stunotepad_listview1);
        TextView textView = (TextView) view.findViewById(R.id.testText);
        textView.setText("ddddddddddd");

        adapter =  new ListViewAdapter();
        stuListView.setAdapter(adapter);
        adapter.addItem("0000","0000","0000","0000","0000","0000") ;
이런식으로 그냥 값을 넣었을때는 리스트가 보이는데
json으로 데이터를 가져와서 넣는건 안되네요 ㅠㅠ
해결했습니다 ㅠㅠ
json부분을 클래스로 새로 만들어서 하니까 됩니다...
감사합니다ㅠㅠ
...