简体   繁体   English

使用CustomAdapter的OnClick ListView

[英]OnClick ListView with CustomAdapter

I have made a ListView with CustomAdapter. 我已经用CustomAdapter制作了一个ListView。 Everything is perfect, but how do i open a new fragment when an item is clicked. 一切都很完美,但是当单击一个项目时如何打开一个新片段。 Please tell me what should i do to open a new fragment which will be having the description of listitem by having an image and a text field. 请告诉我我应该怎么做才能打开一个新的片段,该片段将具有图像和文本字段,从而具有对listitem的描述。 The Class File: 类文件:

package com.basil.victor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.io.IOException;
import java.io.InputStream;

public class Events extends Fragment {

private ListView listEvent;

String eventname[]={
    "Name",
    "of",
    "the",
    "events",
    "are",
    "present",
    "here"
};

String eventlogoname[]={
    "Logo",
    "name",
    "of",
    "events",
    "are",
    "present",
    "here"
};

Drawable[] arr=new Drawable[7];

String eventsubtitle []={
    "Subtitles",
    "of",
    "the",
    "events",
    "are",
    "present",
    "here"
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_events, null);



for(int i=0;i<7;i++) {
    try {
        InputStream stream = getContext().getAssets().open(eventlogoname[i] + ".jpg");
        Drawable el = Drawable.createFromStream(stream, null);
        arr[i] = el;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

EventList adapter = new
        EventList(getActivity(), eventname, arr, eventsubtitle);
//ListView lv = (ListView)rootView.
listEvent=(ListView)view.findViewById(R.id.listEvent);
listEvent.setAdapter(adapter);


return view;
}
}

CustomListView adapter: CustomListView适配器:

package com.basil.victor;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class EventList extends ArrayAdapter<String>{

private final Activity context;
private final String[] title;
private final Drawable[] banner;
private final String[] subtitle;
public EventList(Activity context,
              String[] title, Drawable[] banner, String[] subtitle) {
super(context, R.layout.list_single, title);
this.context = context;
this.title = title;
this.banner = banner;
this.subtitle = subtitle;

}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.event_row, null, true);

TextView txtTitle = (TextView) rowView.findViewById(R.id.event_title);
ImageView imageView = (ImageView) rowView.findViewById(R.id.event_banner);
TextView subTitle = (TextView) rowView.findViewById(R.id.event_subtitle);


txtTitle.setText(title[position]);
imageView.setImageDrawable(banner[position]);
subTitle.setText(subtitle[position]);


return rowView;
}
}

Root Fragment :---- 根片段:----

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class RootFragment extends Fragment {

    private static final String TAG = "RootFragment";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        /* Inflate the layout for this fragment */
        View view = inflater.inflate(R.layout.root_fragment, container, false);

        FragmentTransaction transaction = getFragmentManager()
                .beginTransaction();
        /*
         * When this container fragment is created, we fill it with our first
         * "real" fragment
         */
        transaction.replace(R.id.root_frame, new Events());

        transaction.commit();

        return view;
    }

} 

root fragment xml :- 根片段xml:-

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root_frame" >

</FrameLayout>

Your fragment 你的片段

  package com.basil.victor;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ListView;

    import java.io.IOException;
    import java.io.InputStream;

    public class Events extends Fragment {

    private ListView listEvent;

    String eventname[]={
        "Name",
        "of",
        "the",
        "events",
        "are",
        "present",
        "here"
    };

    String eventlogoname[]={
        "Logo",
        "name",
        "of",
        "events",
        "are",
        "present",
        "here"
    };

    Drawable[] arr=new Drawable[7];

    String eventsubtitle []={
        "Subtitles",
        "of",
        "the",
        "events",
        "are",
        "present",
        "here"
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_events, null);



    for(int i=0;i<7;i++) {
        try {
            InputStream stream = getContext().getAssets().open(eventlogoname[i] + ".jpg");
            Drawable el = Drawable.createFromStream(stream, null);
            arr[i] = el;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    EventList adapter = new
            EventList(getActivity(), eventname, arr, eventsubtitle);
    //ListView lv = (ListView)rootView.
    listEvent=(ListView)view.findViewById(R.id.listEvent);
    listEvent.setAdapter(adapter);


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

                 FragmentTransaction trans = getFragmentManager()
                        .beginTransaction();
                /*
                 * IMPORTANT: We use the "root frame" defined in
                 * "root_fragment.xml" as the reference to replace fragment
                 */
                trans.replace(R.id.root_frame, new SecondFragment());

                /*
                 * IMPORTANT: The following lines allow us to add the fragment
                 * to the stack and return to it later, by pressing back
                 */
                trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                trans.addToBackStack(null);

                trans.commit();
                    }
                });

        return view;
        }
        }
 listEvent.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
       Fragment_03 f3 = new Fragment_03();
       FragmentManager fragmentManager =((FragmentActivity)getContext()).getSupportFragmentManager();
       fragmentManager.beginTransaction()
      .replace(R.id.content_frame, new Fragment_03())
       .commit();

        });

in you put this code may be help to go to another fragment. 在您放置此代码可能有助于转到另一个片段。

try this code in your Events Fragment: 在您的事件片段中尝试以下代码:

 listEvent.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                            .beginTransaction();
            Fragment profileFragment = new Profile();//the fragment you want to show
            Bundle bundle = new Bundle();
            bundle.putString("TITLE", parent.getItemAtPosition(position););
            profileFragment.setArguments(bundle);
            fragmentTransaction
                .replace(R.id.content_frame, profileFragment);//R.id.content_frame is the layout you want to replace
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

    }
});

in new Fragment you can get the value using: 在新的Fragment中,您可以使用以下方法获取值:

Bundle bundle = getArguments();
String title = bundle.getString("TITLE") ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM