从适配器访问片段

时间:2014-06-23 17:50:55

标签: java android fragment adapter

我的想法是我有一个列表视图,其中每个项目都是一个产品,当我点击该项目时,我需要从列表视图的适配器内的点击转到另一个片段。

我的适配器是:

package info.android.adapter;

import info.android.ProductFragment;
import info.android.R;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ProductOffersListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<info.android.model.ProductsOffers> navProOffers;

    public ProductOffersListAdapter(Context context, ArrayList<info.android.model.ProductsOffers> navProOffers) {
        this.context = context;
        this.navProOffers = navProOffers;
    }

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

    @Override
    public info.android.model.ProductsOffers getItem(int position) {        
        return navProOffers.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.fragment_row, null);
        }

        convertView.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                          // I NEED CLALL HERE FRAGMENT_PRODUCT.XML                             
              }
            });

        ImageView img = (ImageView) convertView.findViewById(R.id.image); 

        TextView txtCodigo = (TextView) convertView.findViewById(R.id.txtCodigo);
        TextView txtCiudad = (TextView) convertView.findViewById(R.id.txtCiudad);               
        TextView txtVF = (TextView) convertView.findViewById(R.id.txtVF);

        String sVF =  Double.toString(getItem(position).ValorFinal) + "€";  

        txtVF.setText(sVF);
        txtCiudad.setText(getItem(position).ciudad);
        txtCodigo.setText(getItem(position).codigo);

        convertView.setTag(getItem(position).codigo);

        img.setImageBitmap(getItem(position).Imagen);

        return convertView;
    }
}

你必须假设所有代码都在运行。

如何进入适配器内的产品片段?

3 个答案:

答案 0 :(得分:5)

您需要在构造函数中传递它。 例如:

public class ProductOffersListAdapter extends BaseAdapter
{
 private Context context;
 private ArrayList<info.android.model.ProductsOffers> navProOffers;
 Fragment myFragment;

 public ProductOffersListAdapter(Context context, ArrayList<info.android.model.ProductsOffers> navProOffers, Fragment myFragment) 
 {
    this.context = context;
    this.myFragment = myFragment;
    this.navProOffers = navProOffers;
 }

...

答案 1 :(得分:1)

我采取的措施是:

Intent i = new Intent(v.getContext(), MainActivity.class);                      
i.putExtra("fragment_value", "2");
v.getContext().startActivity(i);

在主类我得到参数作为值,并使用开关我在main中设置片段我感兴趣。

答案 2 :(得分:0)

public class ProductOffersListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<info.android.model.ProductsOffers> navProOffers;

    public ProductOffersListAdapter(Context context, ArrayList<info.android.model.ProductsOffers> navProOffers) {
        this.context = context;
        this.navProOffers = navProOffers;
    }

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

    @Override
    public info.android.model.ProductsOffers getItem(int position) {        
        return navProOffers.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.fragment_row, null);
        }

        convertView.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                          // I NEED CLALL HERE FRAGMENT_PRODUCT.XML   
             // TO call fragment  use this 
          AppCompatActivity appCompatActivity = (AppCompatActivity) v.getcontext();
    FragmentManager fragmentManager = appCompatActivity.getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(frameLayout.getId(),fragment);
        fragmentTransaction.commit();



                          
              }
            });

        ImageView img = (ImageView) convertView.findViewById(R.id.image); 

        TextView txtCodigo = (TextView) convertView.findViewById(R.id.txtCodigo);
        TextView txtCiudad = (TextView) convertView.findViewById(R.id.txtCiudad);               
        TextView txtVF = (TextView) convertView.findViewById(R.id.txtVF);

        String sVF =  Double.toString(getItem(position).ValorFinal) + "€";  

        txtVF.setText(sVF);
        txtCiudad.setText(getItem(position).ciudad);
        txtCodigo.setText(getItem(position).codigo);

        convertView.setTag(getItem(position).codigo);

        img.setImageBitmap(getItem(position).Imagen);

        return convertView;
    }
}