Фрагмент создан, но не виден на элементе. Нажмите на listView.Застрял здесь - PullRequest
0 голосов
/ 10 июня 2018

Вот мой код активности фрагмента

public class Show extends Fragment {
private int local;
private String[] names = {"Alexndra Daddario", "Anne Hathaway", "Dakota Johnson",
        "Emma Stone", "Emma Watson", "Emmy Rosum", "Jessica Alba",
        "Kristen Stewart", "Marian Cotillard", "Natalie Portman"};
private int[] res = {R.drawable.alexandra_daddario, R.drawable.anne_hathaway, R.drawable.dakota_johnson, R.drawable.emma_stone, R.drawable.emma_watson, R.drawable.emmy_rosum, R.drawable.jessica_alba, R.drawable.kristen_stewart, R.drawable.marion_cotillard, R.drawable.natalie_portman};

public Show() {
    Log.d("dbg", "constructor of fragment");
    // Empty constructor required
}

public static Show newInstance(int position) {
    Bundle bndl = new Bundle();
    bndl.putInt("POSITION", position);
    Show show = new Show();

    show.setArguments(bndl);
    Log.d("dbg", "new instance just called");
    return show;
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );

    Log.d("dbg", "onCreate of fragment called");

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    local = getArguments().getInt("POSITION");

    View rootView = inflater.inflate( R.layout.fragment_show_layout, container, false );
    ImageView iv = rootView.findViewById(R.id.display);
    TextView tv = rootView.findViewById(R.id.abhinetri_ka_naam);

    Drawable d = getResources().getDrawable(res[local]);
    iv.setImageDrawable(d);
    tv.setText(names[local]);
    rootView.postInvalidate();
    Log.d("dbg", "onCreateView of fragment just called");
    return rootView;
}

}

А вот код внутри прослушивателя кликов моего Listview.Здесь я создаю свой фрагмент, передавая позицию элемента, по которой щелкнули, и эта позиция будет аргументами этого фрагмента.Используя положение, я создаю виды.Это все, но в моем приложении ничего не происходит, когда я нажимаю на элемент.

lv.setOnItemClickListener( new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Show show = Show.newInstance(position);

            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction().add(0, show).commit()

            Log.d("dbg", "Item clicked");
        }
    } );

1 Ответ

0 голосов
/ 10 июня 2018
fm.beginTransaction().add(0, show).commit()

/**
     * Add a fragment to the activity state.  This fragment may optionally
     * also have its view (if {@link Fragment#onCreateView Fragment.onCreateView}
     * returns non-null) into a container view of the activity.
     * 
     * @param containerViewId Optional identifier of the container this fragment is
     * to be placed in.  If 0, it will not be placed in a container.
     * @param fragment The fragment to be added.  This fragment must not already
     * be added to the activity.
     * @param tag Optional tag name for the fragment, to later retrieve the
     * fragment with {@link FragmentManager#findFragmentByTag(String)
     * FragmentManager.findFragmentByTag(String)}.
     * 
     * @return Returns the same FragmentTransaction instance.
     */
    public abstract FragmentTransaction add(@IdRes int containerViewId, Fragment fragment,
            @Nullable String tag);

Вы передаете 0 в качестве вашего containerViewId.Так что он не помещается в контейнер.Передайте там свой идентификатор просмотра контейнера.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...