Android Fragment如何从自定义listview baseadapter导航到Fargment?

时间:2014-11-13 06:37:31

标签: android android-fragments android-listview listview-adapter

我想问一下如何从 Listview BaseAdapter 导航到片段? 我点击任何项目并从中获取位置和信息并发送给Fragment。我该怎么办?

我的BaseAdapter:

public class ListViewAdapter extends BaseAdapter {

// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();

public ListViewAdapter(Context context,
        ArrayList<HashMap<String, String>> arraylist) {
    this.context = context;
    data = arraylist;
    imageLoader = new ImageLoader(context);
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    // Declare Variables
    TextView country, rank;
    // TextView population;
    ImageView flag;

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View itemView = inflater.inflate(R.layout.listview_item, parent, false);
    // Get the position
    resultp = data.get(position);

    // Locate the TextViews in listview_item.xml
    rank = (TextView) itemView.findViewById(R.id.rank);
    country = (TextView) itemView.findViewById(R.id.country);
    // Locate the ImageView in listview_item.xml
    flag = (ImageView) itemView.findViewById(R.id.flag);

    // Capture position and set results to the TextViews
    distance.setText(resultp.get(FragmentPlaces.RANK));
    bar_name.setText(resultp.get(FragmentPlaces.COUNTRY));
    // population.setText(resultp.get(MainActivity.POPULATION));
    // Capture position and set results to the ImageView
    // Passes flag images URL into ImageLoader.class
    imageLoader.DisplayImage(resultp.get(FragmentPlaces.FLAG), logo_image);
    // Capture ListView item click
    itemView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // // Get the position
            resultp = data.get(position);

            Bundle bundle = new Bundle();
            bundle.putString("rank", resultp.get(FragmentPlaces.RANK));
            bundle.putString("country",
                    resultp.get(FragmentPlaces.COUNTRY));
            bundle.putString("flag", resultp.get(FragmentPlaces.FLAG));
            // set Fragmentclass Arguments
            FragmentSubVenues fragSubVenues = new FragmentSubVenues();
            fragSubVenues.setArguments(bundle);
            // FragmentTransaction ft =
            // getFragmentManager().beginTransaction();
            // ft.replace(R.id.activity_main_content_fragment,
            // fragSubVenues);
            // ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            // ft.addToBackStack(null);
            // ft.commit();
            // FragmentManager fragmentManager =
            // activity.getFragmentManager();

        }
    });
    return itemView;
}
}

我想在Fragment下面导航:

  public FragmentSubVenues() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_subview, null);
    Bundle bundle = this.getArguments();
    if(bundle != null){
        bar_name = bundle.getString("country");
        bar_name = bundle.getString("rank");
        bar_name = bundle.getString("flag");
    }

    TextView txtrank = (TextView) view.findViewById(R.id.rank);
    TextView txtcountry = (TextView) view.findViewById(R.id.country);
    ImageView imgflag = (ImageView) view.findViewById(R.id.flag);
    txtrank.setText(country);
    txtcountry.setText(rank);
    imageLoader.DisplayImage(flag, imgflag);

    return view;
}

}

1 个答案:

答案 0 :(得分:1)

您可以在适配器中定义接口(侦听器)并在您的活动或片段中实现侦听器。

请注意您可以直接为listview设置OnItemSelectedListener,而不是为每个列表项设置onClickListener。 (参见底部的N.B.注释)

如果您打算以当前的方式进行此操作,以下是基于您当前代码的示例代码:

public class ListViewAdapter extends BaseAdapter {

public interface OnItemSelectedListener {
    public void onSelected(String country, String rank, String flag);
}
private OnItemSelectedListener mListener;
// Declare Variables
Context context;
LayoutInflater inflater;
...

您需要提供一个方法来在适配器类中设置侦听器,这只是将侦听器实现分配给mListener:

public void setOnSelectedListener(OnItemSelectedListener l) {
    mListener = l;
}

在您尝试启动片段的原始代码中,调用侦听器的方法:

public View getView(final int position, View convertView, ViewGroup parent) {
....
itemView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
       if(mListener!=null) {
           resultp = data.get(position);
           mListener.onSelected(resultp.get(FragmentPlaces.COUNTRY),
               resultp.get(FragmentPlaces.RANK), 
               resultp.get(FragmentPlaces.FLAG));
       }
    ....

将您的片段开始代码移动到您的活动或主片段中,该片段实现定义的接口。

注意 :您没有使用传递给适配器的getView方法的convertView,这通常不适合listview / gridview。您可以在Google中搜索ListView Adapter convertView,你会发现很多介绍它的文章。

NB :您无需为列表项的每个视图设置OnClickListener。相反,您可以直接为ListView设置OnItemClickListener,并在那里切换片段。我认为这比你现在做的要好得多。这将为您节省更多额外代码。您可能不需要定义接口。

编辑:使用OnItemClickListener的示例

基本上你可以在onCreate(如果你在activity中使用它)或onCreateView(如果你在另一个片段中使用它)中设置如下的监听器

...
mAdapter = new ListViewAdapter(...);
ListView yourListView = (ListView)findViewById(R.id.your_list_view_id);
yourListView.setAdapter(mAdapter);
yourListView.setOnItemClickListener( new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HashMap<String, String> resultp = mAdapter.getItem(position);
        // Switch to the fragment you want with the information you can get from resultp
        ...
    }
});

switchFragment是启动新片段的方法,您可以编写该方法