2016-09-05 5 views
0

특정 항목이있는 목록 조각을 작성했습니다. 각 항목을 클릭하면 해당 항목에 대해 새 listfragment를 조각으로 열어야합니다. 따라서 내 listfragment 코드를 게시하는 중입니다 ....ListClragment의 Onclick 항목이 새 사용자 정의 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(); 
    ; 
} 
} 
+0

현재 직면하고있는 문제는 무엇입니까? – tpk

+0

다음 목록에가 보는 방법을 말해주세요 – gStephin

+0

나는 당신이 원하는 것을 분명히 이해하지 못했습니다. – tpk

답변

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 

FragmentToBeReplacedWithonCreate()에서

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

할 세계의 ArrayList를 만들 FragmentToBeReplacedWith에서 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(); 

내부에이 코드를 넣어

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

이제 ArrayList subCategories의 값을 사용하십시오. 희망이 도움이됩니다.

관련 문제