gpt4 book ai didi

android - fragment 已创建但在 listView 的 itemClick 上不可见。卡在这里

转载 作者:行者123 更新时间:2023-11-29 02:28:41 25 4
gpt4 key购买 nike

这是我的 fragment Activity 代码

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 的点击监听器中的代码。这是我创建 fragment 的地方,通过传递单击项目的位置,该位置将成为该 fragment 的参数。使用位置我正在创建 View 。就是这样,但在我的应用程序中,当我点击一个项目时没有任何反应。

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");
}
} );

最佳答案

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 传递。所以它没有放在容器中。在那里传递您的容器 View ID。

关于android - fragment 已创建但在 listView 的 itemClick 上不可见。卡在这里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50780354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com