ListView项目Onclick片段

时间:2018-02-19 09:37:16

标签: android listview android-fragments

美好的一天,我正在片段中制作列表视图项目onClick。每次单击其他项目时,它都会显示相同的片段活动。我应该怎么做才能使它与其他项目不同?我应该使用

  

if (postion==0)或案例?

List1TW

public List1TW() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_list1_tw, container, false);
        // Inflate the layout for this fragment
        String[] awayStrings = {
                "Chocolate Hills",
                "Banaue Rice Terraces",
                "Hinatuan Enchanted River",
                "Mount Mayon",
                "Puerto Princesa Underground River",
                "Lake Sebu",
                "Sumaguing Cave",
                "Tinuy-an Falls",
        };

        ListView lv = (ListView) view.findViewById(R.id.listView2);

        ArrayAdapter<String> lva = new ArrayAdapter<String>(
                getActivity(), android.R.layout.simple_list_item_1, awayStrings);
        lv.setAdapter(lva);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                TW1 fragment = new TW1();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container, fragment);
                transaction.addToBackStack(null);
                transaction.commit();

            }
        });
        return view;
    }

}

2 个答案:

答案 0 :(得分:0)

您可以使用position变量

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            if(position == 0){
            TW1 fragment = new TW1();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.fragment_container, fragment);
            transaction.addToBackStack(null);
            transaction.commit();
       }
   else if(){
      }
   //// or even switch
  switch(position){
     case 1:
     // do your work
      break;          
       }
         }
    });

答案 1 :(得分:0)

如果您想识别位置点击,可以在itemclickListener中使用Case over here。或者您可以使用bundle设置片段的位置,然后在Fragment中识别单击的位置。

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Bundle bundle = new Bundle();
                    bundle.putString("position", position);
                    TW1 fragment = new TW1();
                    fragment.setArgument(bundle);
                    FragmentTransaction transaction = getFragmentManager().beginTransaction();
                    transaction.replace(R.id.fragment_container, fragment);
                    transaction.addToBackStack(null);
                    transaction.commit();

                }
            });