2

OnItemClickListener Fragments and Configuration

 3 years ago
source link: https://www.codesd.com/item/onitemclicklistener-fragments-and-configuration.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

OnItemClickListener Fragments and Configuration

advertisements

I have a project with Fragments. On the main Fragment I have a list with items (pic related), which is generated by using a custom adapter and I think I need to use the OnItemClickListener to make clicking on items going to another activity. Below is a fragment of my HomeFragment class, and here is where I want to create an OnItemClickListener, is it possible? How can I do that?

public class HomeFragment extends Fragment implements OnClickListener {
    public HomeFragment() {
        // Required empty public constructor
    }
    View rootView;

    @Override
    public void onCreate(final Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

    }

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

        rootView = inflater.inflate(R.layout.fragment_home, container, false);

        RowBean RowBean_data[] = new RowBean[]{
                new RowBean(R.drawable.kamil, "Kamil "),
                new RowBean(R.drawable.bartlomiej, "Bartlomiej "),
                new RowBean(R.drawable.krzysztof, "Krzysztof ")
        };

        CustomAdapter adapter = new CustomAdapter(getActivity().getApplicationContext(), R.layout.list_style, RowBean_data);
        ListView lista = (ListView) rootView.findViewById(R.id.lista);
        lista.setAdapter(adapter);

        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {        super.onAttach(activity);    }

    @Override    public void onDetach() {        super.onDetach();    }

    @Override    public void onClick(View v) {      Toast.makeText(getActivity(), "Works!", Toast.LENGTH_SHORT).show();    }

}




Add following listener before return rootView; statement in your onCreateView method

 lista.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                 //do stuff
                 Intent intent=new Intent(CurrentActivity.this,AnotherActivty.class);
                 startActivity(intent);
            }
        });




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK