how to set setContentView in fragment

北城余情 提交于 2019-12-02 10:04:12

On a fragment you don't call setContentView explicitly, you return the view after inflating it, as you are. So instead of calling setContentView consider adding the view aboutPage to rootView or one of its children views.

For example, say your layout R.layout.fragment_navigation contains a LinearLayout (or any other ViewGroup for that matter) with an ID of content. You would do this, before your return statement:

LinearLayout content = (LinearLayout) rootView.findViewById(R.id.content);
content.addView(aboutPage); //<-- Instead of setContentView(aboutPage)

You'll have to adjust this to your layout, I don't know what's inside it.

Full example

fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/container">
</RelativeLayout>

CustomFragment.java

public class FragmentExample extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment, container, false);
        Element versionElement = new Element();
        versionElement.setTitle("Version 6.2");

        Element adsElement = new Element();
        adsElement.setTitle("Advertise with us");

        View aboutPage = new AboutPage(getActivity())
                .isRTL(false)
                .addItem(versionElement)
                .addItem(adsElement)
                .addGroup("Connect with us")
                .addEmail("elmehdi.sakout@gmail.com")
                .addFacebook("the.medy")
                .addTwitter("medyo80")
                .addYoutube("UCdPQtdWIsg7_pi4mrRu46vA")
                .addPlayStore("com.ideashower.readitlater.pro")
                .addInstagram("medyo80")
                .addGitHub("medyo")
                .create();

        viewGroup.addView(aboutPage);
        return viewGroup;
    }
}

If you need just aboutPage than change the return statement to return aboutPage;

setContentView() is for Activities, for Fragments you have to return the inflated layout on the onCreateView() method like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.article_view, container, false);
}

I hope it helps.

in Android X you simply can first import

import androidx.fragment.app.Fragment

and then in constructor pass your layout id like R.layout.fragment_navigation and then just override onViewCreated methode (it,s safer too)and than you just implement onCreateView(). Example:

import android.view.View
import android.widget.*
import androidx.fragment.app.Fragment

class MahrInCoinFragment : Fragment(R.layout.fragment_navigation) {

private lateinit var mCoin: Coin

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    // Load coin saved data or use default value
    val cost: Int = Hawk.get(Constant.COIN_COST_KEY, Constant.DEFAULT_COIN_PRICE)
    val date: String = Hawk.get(Constant.COIN_DATE_KEY, Constant.DEFAULT_COIN_DATE)
    updateCoin(Coin().apply { this.cost = cost; this.date = date })

    // Handle listeners
    view.findViewById<Button>(R.id.btn_calculate).setOnClickListener(this)
    view.findViewById<ImageButton>(R.id.btn_update).setOnClickListener(this)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!