【问题标题】:Onclick item on ListFragment open a new custom ListFragmentListFragment 上的 Onclick 项目打开一个新的自定义 ListFragment
【发布时间】:2016-09-05 06:57:50
【问题描述】:

我一直在处理包含某些项目的列表片段。单击每个项目时,我需要针对片段中的该项目打开一个新的列表片段。所以我发布了我的列表片段代码....

public class SellListFragment extends ListFragment implements OnItemClickListener {

String[] menutitles;
TypedArray menuIcons;

SellCustomAdapter adapter;
private List<RowItem> rowItems;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    return inflater.inflate(R.layout.list_fragment, null, false);
}

@Deprecated
@Override
public void onActivityCreated(Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    menutitles = getResources().getStringArray(R.array.titles);
    menuIcons = getResources().obtainTypedArray(R.array.icons);

    rowItems = new ArrayList<RowItem>();

    for (int i = 0; i < menutitles.length; i++) {
        RowItem items = new RowItem(menutitles[i], menuIcons.getResourceId(i, -1));
        rowItems.add(items);
    }

    adapter = new SellCustomAdapter(getActivity(), rowItems);
    setListAdapter(adapter);
    getListView().setOnItemClickListener(this);

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {
  //  ((CategorySelectedListener)getActivity()).categorySelected(position);
    //Toast.makeText(getActivity(), menutitles[position], Toast.LENGTH_SHORT).show();
   ;
 }
}

【问题讨论】:

  • 您面临的问题是什么?
  • 请告诉我如何去下一个列表片段
  • 我没有很清楚你想要什么。
  • 你想用其他片段替换现有片段吗?
  • 我的第一个片段有一个类别列表,点击每个项目我需要另一个片段中的子类别

标签: android listview fragment android-listfragment


【解决方案1】:

在您当前的 SellListFragment 在类级别创建这些数组列表。

ArrayList<ArrayList<String>> subCategoriesList=new ArrayList<>();     //Arraylist which will contain all the subCategories.
ArrayList<String> subCategoriesForCategory1=new ArrayList<>();        //ArrayList of sub categories for category 1.         
ArrayList<String> SubCategoriesForCategory2=new ArrayList<>();        //Repeat for as many categories you have.

onCreate() 中填写您的子类别数组。

subCategoriesForCategory1.add("SubCat1");
subCategoriesForCategory1.add("SubCat2");     //Repeat as per your needs

subCategoriesForCategory2.add("SubCat1");
subCategoriesForCategory2.add("SubCat2");     //Repeat as per your needs

subCategoriesList.add(subCategoriesForCategory1);         //add to main arrayList
subCategoriesList.add(subCategoriesForCategory2);         //Repeat

将此代码放入onItemClick()

FragmentManager fm=getActvity().getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
FragmentToBeReplacedWith fragmentObj=new FragmentToBeReplacedWith();
Bundle bundle=new Bundle();
bundle.putStringArrayList("subcategories",subCategoriesList.get(position));     //Repeat for as many values you want to pass. Explore for what suits your needs.
fragmentObj.setArguments(bundle);
ft.replace(R.id.id_of_fragment_container,fragmentObj);
ft.addToBackStack("setSomeUniqueName");  //Optional and nullable
ft.commit();

FragmentToBeReplacedWith创建一个全局数组列表

ArrayList<String> subCategories=new ArrayList<>();

FragmentToBeReplacedWithonCreate() 中做

Bundle bundle=getArguments();
subCategories=bundle.getStringArrayList("subcategories");

现在在任何需要的地方使用 ArrayList subCategories 中的值。 希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-31
    相关资源
    最近更新 更多