2017-01-07 73 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") ;