2017-01-07 48 views
-1

我用CustomAdapter创建了一个ListView。一切都很完美,但是如何在点击某个项目时打开新的片段。请告诉我应该怎么做才能打开一个新的片段,它将通过具有图像和文本字段来描述listitem。 类文件:带有CustomAdapter的OnClick ListView

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适配器:

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; 
} 
} 
+0

可以再安装一个[OnItemClickListener](https://developer.android .com/reference/android/widget/AdapterView.OnItemClickListener.html)到您的ListView – akash93

回答

1

根片段:----

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; 
    } 

} 

根片段的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> 

您的片段

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; 
     } 
     } 
+0

发生错误: 无法解析方法'getBaseContext()' –

+0

确定其正常工作! 我改变了getBaseContext()getContext(),因为这个类是一个片段,所以可能是getBaseContext()不起作用的片段 –

+0

但我该如何从这个片段移动到另一个片段 –

1
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(); 

     }); 

在你把这个代码可能会帮助到另一个片段。

+0

什么是content_frame –

0

试试这个代码在你的活动片段:

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(); 

    } 
}); 
在新片段

,你可以使用这个值。

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