如何在listview里面的listview中实现onClicklistener移动到下一个片段

时间:2018-03-15 16:49:33

标签: java android android-layout listview android-fragments

以下是我的代码,我使用包含文本和图片的listview填充我的片段。 我想知道,当从listview项目中单击项目时,如何移动到下一个片段。 例如,如果单击第一个项目将打开片段A,如果单击第二个项目将打开片段B。

我使用以下作为解决方案,但它不起作用。 enter image description here

任何帮助都将得到很高的评价。



package com.nepalpolice.cdp;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by Sagar on 2017/09/23.
 */


public class club extends Fragment  {
    // Array of strings storing country names
    String[] countries = new String[]{
            "India",
            "Pakistan",
            "Sri Lanka",
            "China",
            "Bangladesh",
            "Nepal",
            "Afghanistan",
            "North Korea",
            "South Korea",
            "Japan"
    };

    // Array of integers points to images stored in /res/drawable-ldpi/
    int[] flags = new int[]{
            R.drawable.eka,
            R.drawable.kat,
            R.drawable.rat,
            R.drawable.set,
            R.drawable.ann,
            R.drawable.kar,
            R.drawable.suk,
            R.drawable.sap,
            R.drawable.him,
            R.drawable.gor
    };

    // Array of strings to store currencies
    String[] currency = new String[]{
            "Indian Rupee",
            "Pakistani Rupee",
            "Sri Lankan Rupee",
            "Renminbi",
            "Bangladeshi Taka",
            "Nepalese Rupee",
            "Afghani",
            "North Korean Won",
            "South Korean Won",
            "Japanese Yen"
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_club, container, false);
// Each row in the list stores country name, currency and flag
        List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

        for (int i = 0; i < 10; i++) {
            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("txt", "Country : " + countries[i]);
            hm.put("cur", "Currency : " + currency[i]);
            hm.put("flag", Integer.toString(flags[i]));
            aList.add(hm);
        }

        // Keys used in Hashmap
        String[] from = {"flag", "txt", "cur"};

        // Ids of views in listview_layout
        int[] to = {R.id.flag, R.id.txt, R.id.cur};

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getActivity(), aList, R.layout.listview_layout, from, to);
        // Getting a reference to listview of main.xml layout file
        ListView listView = (ListView) view.findViewById(R.id.listview);
        // Setting the adapter to the listView
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(itemClickListener);
        return view;
    }

// Setting the adapter to the listView


    // Item Click Listener for the listview
    OnItemClickListener itemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View container, int position, long id) {
            // Getting the Container Layout of the ListView
            if(position == 1/*or any other position*/){
                Fragment fragment = new notices();
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.fragment_frame, fragment).addToBackStack(null).commit();
            }
            else if(position == 2){


            } // etc...
        }
    };
}
&#13;
&#13;
&#13;

我试图添加公共类俱乐部扩展Fragment实现OnItemClickListener但它抛出我的错误,类必须声明为抽象..

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

试试这个

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
      //Your click code here. 
    }
});

OR

listView.setOnItemClickListener(itemClickListener);

因为您已经在班级中定义了 itemClickListener