ListFragment上的Onclick项打开一个新的自定义ListFragment

时间:2016-09-05 06:57:51

标签: android listview fragment android-listfragment

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

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

1 个答案:

答案 0 :(得分: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()填写您的子类别arrylists。

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

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

onCreate() FragmentToBeReplacedWith中的Bundle bundle=getArguments(); subCategories=bundle.getStringArrayList("subcategories");

subCategories

现在使用ArrayList .contentContainer { position: relative; z-index: 2; margin: 0 auto; max-width: 720px; text-align: center; } .content__heading { margin-bottom: 24px; color: #272727; font-size: 44px; } .content__teaser { margin-bottom: 24px; color: #595959; font-size: 22px; } .content__cta { display: inline-block; padding: 12px 48px; color: #ff3c64; font-size: 22px; text-decoration: none; border: solid 4px #ff3c64; } .video { position: fixed; top: 50%; left: 50%; min-width: 100%; min-height: 100%; width: auto; height: auto; z-index: -100; transform: translateX(-50%) translateY(-50%); background-size: cover; transition: 1s opacity; } 中的值,无论您需要什么。 希望这会有所帮助。