从活动到片段的通信

时间:2017-06-09 13:16:39

标签: java android android-activity fragment

当点击listview的图像和textview时,我需要从一个活动的列表视图转到一个片段。

我有一个适配器:

public class AdapterResultList extends BaseAdapter {

    Context contexto;
    List<DirectoryData> ListaDirectorio;
    Fragment fragment = null;

    public AdapterResultList(Context contexto, List<DirectoryData> listaDirectorio) {
        this.contexto = contexto;
        ListaDirectorio = listaDirectorio;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View vista = convertView;
        LayoutInflater inflate = LayoutInflater.from(contexto);
        vista = inflate.inflate(R.layout.details_list_directory,null);

        TextView professionalName = (TextView) vista.findViewById(R.id.doctors_name);

        ImageView imagen1 = (ImageView) vista.findViewById(R.id.subcategory_image);               
        professionalName.setText(ListaDirectorio.get(position).getProfessionalName().toString());        
                           imagen1.setImageResource(ListaDirectorio.get(position).getImagen1());


        return vista;
    }
}

在活动中:

public class ResultSearch extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result_search);

    listaProf = (ListView) findViewById(R.id.list_profesionals);

            ListaDr = new ArrayList<DirectoryData>();
            ListaDr.add(new DirectoryData("Dr. Pedro Perez", R.drawable.successright));
            ListaDr.add(new DirectoryData("Dr. Juan Rodriguez", R.drawable.searchbanner));

            AdapterResultList miadaptador = new AdapterResultList(getApplicationContext(), ListaDr);
            listaProf.setAdapter(miadaptador);
    }
}

点击imageview或textview时,我只需转到DoctorsDetailsFragment。我不需要发送数据。

2 个答案:

答案 0 :(得分:0)

1. ObservableList<Branch> lst = FXCollections.observableArrayList(myMap.keySet()); myMap.addListener(new MapChangeListener<? extends Branch, ? extends Boolean>() { @Override public void onChanged(Change<? extends Branch, ? extends Boolean> change) { boolean removed = change.wasRemoved(); boolean added = change.wasAdded(); Branch key = change.getKey(); if (removed && !added) { lst.remove(key); } if (added && !removed) { lst.add(key); } } }); 适配器方法和使用Override OnDataBind onClickListener或图片....搜索的内部方法,您将获得大量代码。

2.从TextViewactivity ....只需使用另一个虚拟fragment,其中将加载实际的activity

从另一个人那里呼叫fragment时,照常开始。

答案 1 :(得分:0)

您可以使用setArguments将参数传递给Fragment。使用以下方法

Fragment fragment = new StaticContent();
Bundle bundle = new Bundle();
bundle.putString("Key", "Value");
fragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).addToBackStack(null).commitAllowingStateLoss();
fragmentManager.executePendingTransactions();