gpt4 book ai didi

java - Fragment 的 ListView 扩展 ListFragment 正在重复自己。知道出了什么问题吗?

转载 作者:行者123 更新时间:2023-11-29 20:11:57 24 4
gpt4 key购买 nike

这是 Activity

public class Homepage extends FragmentActivity implements ChatFragment.OnFragmentInteractionListener{

SearchFragment searchFragment;
ChatFragment chatFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);

searchFragment = new SearchFragment();
chatFragment = new ChatFragment();


if(savedInstanceState == null){
getFragmentManager().beginTransaction().replace(R.id.homepageFragment,chatFragment).commit();
}
}

@Override
protected void onResume(){
Bundle bundle = getIntent().getExtras();
if(bundle != null){
//text.setText("user_id: " + bundle.get("user_id") + " ,username: " + bundle.get("username") + " ,password: " + bundle.get("password"));
}
super.onResume();
}


@Override
public void onChatFragmentInteraction(Uri uri){

}

public void openSearchFragment(View view){
if(!searchFragment.isAdded())
getFragmentManager().beginTransaction().replace(R.id.homepageFragment,searchFragment).commit();
}

public void openChatFragment(View view){
if(!chatFragment.isAdded())
getFragmentManager().beginTransaction().replace(R.id.homepageFragment,chatFragment).commit();
}

public void openProfile(View view){

}
}

这是 Activity 的布局

I like making my activities screen responsive.

<FrameLayout 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"
tools:context="the_activity_location"
android:orientation="vertical">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="100"
android:orientation="vertical">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:layout_weight="90"
android:orientation="vertical">

<fragment
android:id="@+id/homepageFragment"
android:name="com.example.summer.toothbrush.SearchFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:layout="@layout/fragment_search"/>

</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:backgroundTint="@color/whiteClr"
android:background="@color/whiteClr"
android:layout_weight="10"
android:orientation="horizontal"
android:weightSum="100">

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/search_img"
android:background="@null"
android:layout_weight="25"
android:layout_marginLeft="0dp"
android:layout_gravity="center"
android:onClick="openSearchFragment"/>

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/chat_img"
android:background="@null"
android:layout_weight="50"
android:layout_gravity="center"
android:onClick="openChatFragment"/>

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/profile_img"
android:background="@null"
android:layout_weight="25"
android:layout_gravity="center"
android:onClick="openProfile"/>

</LinearLayout>

</LinearLayout>

这是 fragment

public class SearchFragment extends ListFragment implements AdapterView.OnItemClickListener{

ArrayList<String> list;
ListViewAdapter myAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
View view = inflater.inflate(R.layout.fragment_search,container,false);

list = new ArrayList<String>();

list.add("I");
list.add("am");
list.add("Iron");

myAdapter = new ListViewAdapter(list,getActivity().getBaseContext());

setListAdapter(myAdapter);

return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

getListView().setOnItemClickListener(this);

}

public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
}
}

fragment 的布局在一个由 以“@android:id/list”作为 ID 的 ListView 和一个 TextView。

Don't worry about the adapter. it's working fine.. I tested it out on an activity. Works perfectly fine.

This is what it looks like now

最佳答案

这是正确的。您有两个 SearchFragment。一个在您的布局中声明为静态,另一个通过 FragmentTransaction 添加。 Fragment 在布局中声明为静态的不能使用编程事务来替换。只需更换

<fragment
android:id="@+id/homepageFragment"
android:name="com.example.summer.toothbrush.SearchFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:layout="@layout/fragment_search"/>

<FrameLayout
android:id="@+id/homepageFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

它会像你期望的那样工作

关于java - Fragment 的 ListView 扩展 ListFragment 正在重复自己。知道出了什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34635500/

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