我正在关注这个youtube教程来创建一个配方应用程序(使用ListViews导航抽屉和碎片) Youtube tutorial 并成功实施了这一点。比如单击导航抽屉菜单时,它将显示带有listview(Veg,Non-veg)的新片段。当点击每个项目(Veg)时,它将以吐司(Veg)显示列表项目名称。
但我的应用概念是something like this
所以我需要在点击每个listview项目时用listview打开一个新的不同片段。怎么做?
在我的代码中,当我单击每个列表项时,它将显示相同的片段。我需要为每个项目显示不同的片段。 请检查我的代码。帮助我
这是Home.java(片段)
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Home extends Fragment {
private ListView lv;
private static String[] spacecrafts = {"Non-Veg", "Veg"};
public static Home newInstance()
{
Home home =new Home();
return home;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView=inflater.inflate(R.layout.home, null);
lv = (ListView) rootView.findViewById(R.id.home_lv);
lv.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, spacecrafts));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
//Toast.makeText(getActivity(), spacecrafts[i], Toast.LENGTH_SHORT).show();
Home.this.getFragmentManager().beginTransaction().replace(R.id.containerID, Veg.newInstance()).commit();
}
});
return rootView;
}
@Override
public String toString(){
return "Home";
}
}
这是Veg.java Fragment
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Veg extends Fragment {
private ListView lv;
private static String[] vegcat = {"Vegetables", "Fruits"};
public static Veg newInstance()
{
Veg veg =new Veg();
return veg;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView=inflater.inflate(R.layout.veg, null);
lv = (ListView) rootView.findViewById(R.id.veg_lv);
lv.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, vegcat));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
Toast.makeText(getActivity(), vegcat[i], Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
这是veg.xml布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/veg_lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
答案 0 :(得分:0)
创建三个单独的片段
1.VegFragment
2.NonVegFragment
3.Description Fragment
现在在您的主片段列表视图中,单击用您的另一个片段替换home片段(veg或nonveg列表)。 单击listview中的veg或nonveg菜单项时,将current替换为具有描述数据的Description片段(描述片段)
替换片段的代码如下:
VegFragment finalFragment = new VegFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.flContainer, finalFragment);
fragmentTransaction.commit();
答案 1 :(得分:-1)
这是您获得ListViewItemClick
事件的代码。
所以无论你想达到什么目的,你都可以在这里做到。
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l){
// Do your checks here based on the position of item click.
}
});
为了对新片段进行充气,以下代码(您已经使用过的代码)将为您提供帮助。
Home.this.getFragmentManager().beginTransaction().replace(R.id.containerID, YOUR_FRAGMENT_INSTANCE).commit();